Feb 102007

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 Responses to “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. Thanks a lot.

    I was looking for the same info 🙂

  3. Thanks, I had the same problem and i’ve wasted a lot of time!

  4. 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

  5. Thanks

    I was having the same problem :O)

  6. Thanks Scott…I wish I would’ve looked here first!

  7. You are the boss.!!!
    Thanks Scott

  8. I cant tell you how much this has helped me.
    Thank you so much 😉

  9. Thanks a lot, It really helps me so much.

  10. 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", "window.open(‘http://www.yahoo.com’, ‘newWindow’, ‘height=100, width=100, top=10, left=10, resizable=yes’);", true);

    A new window cannot be opened. If just using ?window.open();?, 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.


  11. Thank Scott!!

  12. 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.

  13. Thanks for posting this. I was looking forward for this.

  14. This was giving me headaches for ages, luckily I ran into your explanation here via Google. Mucho Gracias.


  15. Many Thanks for this Scott. I wasted over a day trying to find a solution before stumbling upon your comments.

  16. Thank you very much! ^^

  17. 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 😉

  18. You are a legend!

    I’ve been looking for this for ages. Thanks Scott, this helps me a lot!!!

  19. Yeah, I had been wracking my brains over this for a while now. Glad I stumbled across this. Thanks for the info!

  20. Thanks!!! it helps me a lot

  21. Thanks , It is really helpfull ,
    it helps me a lot


  22. Thanks, I was searching for this. It solved my problem.

  23. Merci beaucoup, c’est exactement ce que je cherchais.

    Thanks a lot!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>