RegisterStartupScript in AJAX Pages
Today I was modifying an ASP.net user control (a rating control) which register a script at startup to initialize itself. As I was expecting, when I placed that control in the ASP.Net UpdatePanel (for AJAX) the initialization script didn’t run after the AJAX callback and thus the control didn’t worked.
While looking at the code, I found that it was using Page.ClientScript.RegisterStartupScript to run the initialization script. I already know that for the AJAX callback the function to register startup script is ScriptManager.RegisterStartupScript. But now the problem was that I wanted to keep the original functionality so that it keep running on the page even which don’t have the ASP.Net ScriptManager control. So I found a one way of checking if the request is AJAX request on the following link:
http://gotjeep.net/Blogs/CommentView,guid,4be2f278-12e4-40d5-b154-0e8ecaf18fac.aspx
So I change the existing RegisterStartupScript function with following conditional code:
if (ScriptManager.GetCurrent(base.Page).IsInAsyncPostBack)
ScriptManager.RegisterStartupScript(this, this.GetType(), “Rating_Script_” + this.ClientID.ToString() , script.ToString(), false);
else
this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, script.ToString());
With this change done, the control now initialize properly on the page startup and after the AJAX callback. Job Done.
Tags: AJAX, Callback, RegisterStartupScript, Script
This entry was posted
on Saturday, March 1st, 2008 at 5:23 pm and is filed under ASP.Net.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Beautiful! that saved my day!
Many thanks.
We couldn’t get our script to register until we found this page. We were using the this object as the first parameter instead of Page. Thanks so very much.
This article has just saved my day! Many thanks.
Great!! that saved my day!
my 1st child will be named after your name Akbar!!
Itmgmg,
No, you don’t need anything else to make it work. What .NET framework version you are using and what is the exact error you are getting?
Good task. I am finding this one. But, I am still facing problem. RegisterStartupScript does’t work on my page. Any configuration on ScriptManager control?
Nice one. Saved me some time too.
Thanks man, saved my day!
You might want to change this:
if( (ScriptManager.GetCurrent(base.Page)!=null && ScriptManager.GetCurrent(base.Page).IsInAsyncPostBack))
to prevent errors on pages that don’t have a scriptmanager registered.
Perfect! Thank you.
Hi, Akbar very nice code. I was just in search of like this code., thanx for the code.
============================
http://eanirudh.wordpress.com
=============================
Thanks! This saved me! I was trying to get this to work with a 3rd Party control that had embedded on it a custom user control that I created. This was the only thing I found that could get it to work for me. I was trying to pop open a new window from my custom control and set up a script so my main page would reload when I closed the window.
The only modification I needed to make was that I needed to pass a reference of the source aspx page to this code block and replace ScriptManager.RegisterStartupScript(this, …) with ScriptManager.RegisterStartupScript(_sourcePage, …)