ClientScript.RegisterStartupScript doesn’t work with ASP.NET AJAX

I frequently place an UpdatePanel around a paged GridView with inline editing.   During OnRowUpdating, the object is saved.   If the save was not successful, I like to show an alert box with the error message.  

I recently migrated from the Atlas May CTP to ASP.NET 2.0 AJAX.   The following code worked with the May CTP, but, from my research, has not worked since July CTP:

Page.ClientScript.RegisterStartupScript(typeof(string), “alertScript”, “alert(‘Error Message’);”, true);

However, our pals at Microsoft have given us something just as good.   I found it by mistake while trying to put in a hack involving checking the value of ScriptManager.GetCurrent(Page).IsInAsyncPostBack.  Here is the AJAX-friendly equivalent:

ScriptManager.RegisterStartupScript(this.updatePanel, typeof(string), “alertScript”, “alert(‘Error Message’);”, true);

Hope this helps someone.  There are many blog posts, with many more comments, with people compaining about this.

25 thoughts on “ClientScript.RegisterStartupScript doesn’t work with ASP.NET AJAX”

  1. Update:

    I have altogether stopped using Page.ClientScript.RegisterStartupScript. For my AJAX apps, I now use my own ClientSide class to wrap ScriptManager.RegisterStartupScript. In the original post I showed using this.updatePanel as the first parameter for ScriptManager.RegisterStartupScript. In my ClientSide class I now have a property called page that gets System.Web.HttpContext.Current.Handler as System.Web.UI.Page to be used as the first parameter in ScriptManager.RegisterStartupScript. This way I always call ClientSide.RegisterScript or ClientSide.Alert from all of my pages, AJAX or not.

  2. I was looking for a solution for showing an alert box in my ajax app. in case of error. I found your article, but still cannot figure out how to do this. Could you please provide an example. Thank you

  3. My ASP.NET Ajax page has a LinkButton under the UpdatePanel. When clicking the link button, a new window page is opened up. Here is the event handler when clicking the link button:

    protected void lbutton_Click(object sender, EventArgs e)
    ScriptManager.RegisterStartupScript(this, GetType(), "open", "‘’, ‘newWindow’, ‘height=100, width=100, top=10, left=10, resizable=yes’);", true);

    A new window cannot be opened. If just using ?;?, it cannot be opened either. Both of them have this error: There is no source code available for the current location. Any comments about this error? Thanks.


  4. Thanks for posting this! I just added a couple UpdatePanels to a site and all of a sudden my popups stopped working (using ClientScript.REgister…). This got them going again.

  5. Took me some time to figure out this was actualy the problem, i first tought it was the editindex of my gridview that didn’t trigger the javascript expand function but it was the evil ajax :>

    ty for mentioning this 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.