Ketone Cops

August 6, 2008

Creating javascript alerts in ASP.NET with UpdatePanel (or without)

Filed under: Ajax,ASP.NET 2.0,Javascript,programming,UpdatePanel — delroger @ 8:33 pm

Just thought I’d share a couple of simple methods for creating javascript alert messages from an ASP.NET webpage, both when you are using AJAX (and an update panel) or just from a standard page.

In a standard page, you can easily create an alert message in the code-behind like this: 

ClientScript.RegisterClientScriptBlock(Me.GetType(), “yourkeyname”, “alert(‘hello’);”, True)

The parameters that are passed in are the System.Type (the Page type in this case), an arbitrary name for your script ‘key’, the javascript itself as a string, and a boolean indicating whether to add the script tags – i.e., <script type=”javascript”></script> – which you might as well use since it shortens the script itself.

Likewise, creating an alert when you are using an UpdatePanel is slightly different, but simpler than you might think – you just need to make sure the System.Type is the UpdatePanel type, and use the ScriptManager rather than the ClientScript like so:

ScriptManager.RegisterStartupScript(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), “yourkeyname”, “alert(‘hello’);”, True)

This is much the same as before except you are passing in the ID of your UpdatePanel along with the other parameters.

Now, since this is code that you are likely to want to re-use throughout an application, it makes sense to create a Class file for it with a couple of generic methods, like this:

Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Web.UI
Public Class Messages
    Public Shared Sub CreateMessageAlertInUpdatePanel(ByVal up As UpdatePanel, ByVal strMessage As String)
        Dim strScript As String = “alert(‘” & strMessage & “‘);”
        Dim guidKey As Guid = Guid.NewGuid()
        ScriptManager.RegisterStartupScript(up, up.GetType(), guidKey.ToString(), strScript, True)
    End Sub
    Public Shared Sub CreateMessageAlert(ByVal strMessage As String)
        Dim guidKey As Guid = Guid.NewGuid()
        Dim pg As Page = HttpContext.Current.Handler
        Dim strScript As String = “alert(‘” & strMessage & “‘);”
        pg.ClientScript.RegisterStartupScript(pg.GetType(), guidKey.ToString(), strScript, True)
    End Sub
End Class

(We’re just using the Guid for the script key to ensure a new key each time we create an alert)

And that’s it! You can now create alert messages from any of your webpages simply by using:

CreateMessageAlertInUpdatePanel(Me.UpdatePanel1, “hello”)
or

CreateMessageAlert(“hi”)

Advertisements

18 Comments »

  1. Thanks a Lot friend!!!

    Comment by Maurício Santos — September 10, 2008 @ 3:44 pm

  2. That works really well for me. Thank you.

    Comment by Doug Baker — September 19, 2008 @ 2:34 pm

  3. Thanks a lot buddy!!! It’s very usefull. keep posting…

    Comment by Ashish Lingwal — January 15, 2009 @ 10:23 am

  4. Thanks for posting, this was very helpful.

    Comment by Chip — January 27, 2009 @ 2:51 pm

  5. Thanx

    Comment by Usama Wahab Khan — February 18, 2009 @ 11:22 am

  6. Very clear code but I can’t get it to work!!! I have my UpdatePanel inside of a TabContainer, does this change the ScriptManager code? The alert doesn’t appear… Please help if you have the time. Thanks!!!

    Comment by Joyce S — May 15, 2009 @ 5:03 pm

  7. Hi Joyce, it should work OK even in a TabContainer. If you’re copying-and-pasting, make sure that the single quotes for the alert haven’t become angled quotes (so they’re not these: `).

    Comment by delroger — May 27, 2009 @ 5:46 pm

  8. Thanks So much it is profitable for me

    Comment by Basma Seif — October 13, 2009 @ 8:48 am

  9. Muchas Gracias, it worked flawlessly!

    Comment by Ricardo — January 14, 2010 @ 12:39 pm

  10. very good ! thanks

    Comment by adi — March 12, 2010 @ 6:52 pm

  11. Very good. Thanks !!!

    Comment by Pablo — May 12, 2010 @ 4:34 pm

  12. thank you so much dear.
    i faced to trouble with update panel.
    great solution
    thanks…..

    Comment by sameer — November 12, 2010 @ 9:22 am

  13. […] Just thought I'd share a couple of simple methods for creating javascript alert messages from an ASP.NET webpage, both when you are using AJAX (and an update panel) or just from a standard page. In a standard page, you can easily create an alert message in the code-behind like this:  ClientScript.RegisterClientScriptBlock(Me.GetType(), "yourkeyname", "alert('hello');", True) The parameters that are passed in are the System.Type (the Page type in … Read More […]

    Pingback by Creating javascript alerts in ASP.NET with UpdatePanel (or without) (via Ketone Cops) | MadManMonty's Blog — January 6, 2011 @ 3:58 pm

  14. Great Help! Thank you very much dear!

    Comment by Alavi — February 2, 2011 @ 10:21 am

  15. This is really helpful for me.Thanks so much!!!

    Comment by Wai Zin — June 6, 2012 @ 7:32 am

  16. Just what i needed 🙂

    Comment by Søren Dalby — July 12, 2012 @ 11:27 am

  17. Great article, very well explained, thank you!

    Comment by LGuimaraes — October 31, 2012 @ 12:31 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: