grogo21
asked on
Run javascript function
Hello, I have a javascript function updateRatings() on a page.
How can I run that fucntion from code behind? It tried :
Page.RegisterStartupScript ("FocusScr ipt2", "<script type='text/javascript'>upd ateRatings ();</scrip t>")
but it didn't work.
Thanks
How can I run that fucntion from code behind? It tried :
Page.RegisterStartupScript
but it didn't work.
Thanks
ASKER
I have a button inside an update panel. After the partial page postback I want to run the javascript function. I had the following line:
Page.RegisterStartupScript ("FocusScr ipt2", "<script type='text/javascript'>upd ateRatings ();</scrip t>")
in the buttons click event in the code behind but the javascript function doesnt run.
Thanks
Page.RegisterStartupScript
in the buttons click event in the code behind but the javascript function doesnt run.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
should add that there are more parameters when you use the script manager to register startup script...
Me.ScriptManager1.Register StartupScr ipt(Me.Pag e, Me.Page.GetType(), "FocusScript2", "<script type='text/javascript'>upd ateRatings ();</scrip t>", False)
Me.ScriptManager1.Register
One below should work fine for you...
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args)
{
postBackElement = args.get_postBackElement();
if(postBackElement != null)
{
if (postBackElement == (document.getElementById("<%=Button.ClientID %>")))
{
}
}
}
function EndRequest(sender, args)
{
if (postBackElement == (document.getElementById("<%=Button.ClientID %>")))
{
updateRatings();
}
}
</script>
so to explain the code: You are using PageRequestManager, something that's actually good to learn if working with UpdatePanels...
first 3 lines: var prm....prm.add_endRequest --> when working with UpdatePanel, the page can be broken into a few events...ones we are worried about not iw initializationRequest and EndRequest...I created two functions, and assigned them with this line: prm.add_initializeRequest( Initialize Request);. ..etc...if i wanted initialization function to be called snoopy then i would have said prm.add_initializeRequest( Snoopy);
in the initializeRequest function all i did was
1) get what is causing the postback and store in a global variable postBackElement(notice i didn't put var in front of postBackElement to make it's scope global.).
2) I test if the postBackElement to see if it was cause by a certain button...I used <%=Button.ClientID%>")
because I'm using masterpages and with masterpages, elements within it id's are changed to include ctl00_....so I use server tags to get object easier.
in EndRequest, after postback, I check my global postBackElement and see if it was by a certain button and if so, i call the method you wanted called.
This is cool because it's javascript, plus you can stop cancel postbacks at any point if you need to based on some condition.
first 3 lines: var prm....prm.add_endRequest --> when working with UpdatePanel, the page can be broken into a few events...ones we are worried about not iw initializationRequest and EndRequest...I created two functions, and assigned them with this line: prm.add_initializeRequest(
in the initializeRequest function all i did was
1) get what is causing the postback and store in a global variable postBackElement(notice i didn't put var in front of postBackElement to make it's scope global.).
2) I test if the postBackElement to see if it was cause by a certain button...I used <%=Button.ClientID%>")
because I'm using masterpages and with masterpages, elements within it id's are changed to include ctl00_....so I use server tags to get object easier.
in EndRequest, after postback, I check my global postBackElement and see if it was by a certain button and if so, i call the method you wanted called.
This is cool because it's javascript, plus you can stop cancel postbacks at any point if you need to based on some condition.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
is it the body tag you want it to run off off?