Solved

Run javascript function

Posted on 2008-10-30
8
1,046 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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:adilkhan
adilkhan 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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 …
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…

896 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

13 Experts available now in Live!

Get 1:1 Help Now