Link to home
Start Free TrialLog in
Avatar of grogo21
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("FocusScript2", "<script type='text/javascript'>updateRatings();</script>")
but it didn't work.

Thanks

Avatar of silemone
silemone
Flag of United States of America image

what event and object do you want it to run off?

is it the body tag you want it to run off off?
Avatar of grogo21
grogo21

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("FocusScript2", "<script type='text/javascript'>updateRatings();</script>")
in the buttons click event in the code behind but the javascript function doesnt run.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
should add that there are more parameters when you use the script manager to register startup script...

Me.ScriptManager1.RegisterStartupScript(Me.Page, Me.Page.GetType(), "FocusScript2", "<script type='text/javascript'>updateRatings();</script>", False)
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>
    

Open in new window

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(InitializeRequest);...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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial