Solved

Run javascript function

Posted on 2008-10-30
8
1,073 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

751 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