Solved

Run javascript function

Posted on 2008-10-30
8
1,055 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 225 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 21

Assisted Solution

by:silemone
silemone earned 225 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 50 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

810 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