Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Run javascript function

Posted on 2008-10-30
8
Medium Priority
?
1,099 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:grogo21
8 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22844350
what event and object do you want it to run off?

is it the body tag you want it to run off off?
0
 

Author Comment

by:grogo21
ID: 22844484
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
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 900 total points
ID: 22844600
since you're doing a partial postback, you don't actually have access to the whole page to do a Page.RegisterStartupScript

try using the ScriptManager to RegisterStartupScript

ex.
ScriptManager1.RegisterStartupScript(...etc
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Assisted Solution

by:silemone
silemone earned 900 total points
ID: 22844658
Here is a way to do it:  This uses PageRequestManager which finds which object in an UpdatePanel is calling a postback...you can do things before the postback like test conditions...but what you want to do is do something at the end Request function


<script type="text/javascript">        
          var prm = Sys.WebForms.PageRequestManager.getInstance();
          prm.add_initializeRequest(InitializeRequest);
          prm.add_endRequest(EndRequest);        
          var executingItem = null;
         
           function InitializeRequest(sender, args) 
           {
            var postBackElement = args.get_postBackElement();            
            if(postBackElement != null)
            {  
             
             if (postBackElement == (document.getElementById("<%=Button.ClientID %>")))
             {
                  
             }
            }
            
          }
 
          function EndRequest(sender, args) 
          {                
                       if (postBackElement == (document.getElementById("<%=Button.ClientID %>")))
             {
                     //call a javascript method here at the end of postback
             }
            
 
          }
          

Open in new window

0
 
LVL 37

Expert Comment

by:samtran0331
ID: 22844665
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)
0
 
LVL 21

Expert Comment

by:silemone
ID: 22844700
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

0
 
LVL 21

Expert Comment

by:silemone
ID: 22844827
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.
0
 
LVL 23

Assisted Solution

by:Saqib Khan
Saqib Khan earned 200 total points
ID: 22844983
another easy way of doing is.. whereever you want to execute the javaScript Function within page add an Literal Control. and when you press the button set the text of Control to execute the function.
 
Literal.text = "<script>myfunction()</script>"
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question