Home > ASP.Net, JavaScript > Implementing JavaScript Alert on Server Side

Implementing JavaScript Alert on Server Side

Ever so often, an application must communicate with its user when it is working. The most basic of methods is to display a popup message, usually when a task is complete or some error has occurred. In windows form programming, displaying a popup message box to the user is really easy. Unfortunately, this is not so simple when doing web programming, specifically ASP.NET. There is no native method to initiate a message box in ASP.NET.

 

What We Want

JavaScript Popup Message

As such, I decided to take advantage of JavaScript's Alert function which mimics the functionality of the windows form message box – only in your web browser. My implementation allows for the calling of the Javascript Alert function on the server side.

 

Usage

 

Alert("Hello World!")

 

Implementation

 

Public Shared Sub Alert(ByVal message As String)
     Dim strScript As String = ""
     Dim strScriptKey As String

     strScriptKey = "Javascript_Alert_" & System.Guid.NewGuid.ToString()

     strScript &= "alert('" & message & "');"

     Dim p As System.Web.UI.Page

     p = CType(System.Web.HttpContext.Current.Handler, System.Web.UI.Page)

     If Not p Is Nothing Then
           If (Not p.ClientScript.IsStartupScriptRegistered(strScriptKey)) Then

                Dim p_scriptmgr As System.Web.UI.ScriptManager

                p_scriptmgr = System.Web.UI.ScriptManager.GetCurrent(p)

                If p_scriptmgr Is Nothing Then
                      p.ClientScript.RegisterStartupScript(p.GetType, strScriptKey, strScript, True)
                Else
                      System.Web.UI.ScriptManager.RegisterClientScriptBlock(p, p.GetType, strScriptKey, strScript, True)
                End If

            End If
     End If
End Sub
 

 

How it works

 

The function utilizes ASP.NET's RegisterStartupScript and RegisterClientScriptBlock functions to insert a call to the JavaScript Alert function, and uses whatever was provided for the message parameter as the Alert functions message. I wont go over the whole function exhaustively, but there are some key areas I would like to point out.

Firstly, I decided to generate a script key which is required by both the RegisterStartupScript and RegisterClientScriptBlock functions. It was very important that every call to my Alert function generated a unique key so as to ensure that all my Alert function inserted into the page would be uniquely identified by ASP.NET. If I didn't do this, then a call to my Alert function, say two times in succession would only result in only one popup message being displayed.

I created this function so that it can be stored in a separate code file. In order to display the popup message on a page, I had to get the page object corresponding to that page since I had to call the Page.ClientScript.RegisterStartupScript function to insert my JavaScript. To do this, I had to parse System.Web.HttpContext.Current.Handler object to a System.Web.UI.Page type. This allowed me to access the calling page's Page object.

Another issue I ran into was that my popup message was not being called when it is initiated by a control inside an Ajax Update Panel. After some research, i found out that I needed to register my JavaScript with the ScriptManager object that would be on the page. I first had to check if the script manager existed on the page. To do this, I used the System.Web.UI.ScriptManager.GetCurrent function and provided my page object I had captured above as an argument for the function. The function returned a ScriptManager object. Testing whether it was Nothing was all it took to ascertain whether there was  script manager on the page. If one existed, I simply registered my script with the script manager, otherwise i registered it with the page.

 

Conclusion

 

I have used this function in all my ASP.NET projects and it is really handy in showing quick messages to the user. I know that u have various alternatives like Impromptu for JQuery, but nothing beats an old fashioned message box to me. After all, there is no need for an outside JavaScript library and it is guaranteed to perform faster than any JavaScript imitation. Also, I believe that having the ability to call this on the server side is super convenient. I hope you think so too. Please share this article. Thanks.

  1. November 17th, 2011 at 12:02 | #1

    Hey dude! Where can I get more resources about this?

  1. No trackbacks yet.