Solved

Run javascript function

Posted on 2008-10-30
8
1,035 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Assisted Solution

by:silemone
silemone earned 225 total points
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
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
Comment Utility
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
Comment Utility
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:adilkhan
adilkhan earned 50 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now