ASP.Net Client Side Popup Box

One of the big problems with an asp.net application over say a windows application is that when you really want to bring something to the users attention, it can be difficult to pop up a modal dialog box to tell the user what is going on. You cannot use the generic messagebox function because on a server side event this will pop up a message on the server which is not very useful.

So there are of course ways around this problem, by using javascript on button click events etc, but what if you want to display a messagebox from some server side code that is not initiated from a user action such as a button click. Well as it turns out and I recently discovered this is very easy to do with asp.net 2, by using the CientScript.RegisterStartupScript event. Below is an example.

Private Sub SomeSubRoutine()

   If SomeVariable <> SomethingElse Then

      Dim strMessage As String
      strMessage = "Warning, this process generated an error"
      Page.ClientScript.RegisterStartupScript(Me.GetType(), _
        "alert", "<script type="text/javascript"></script>")

   End If

End Sub

So there you go, pretty simple really. Now any server side process on a web page can generate a message box for the user if you require it. You don’t necessarily need to declare the fill Page.ClientScript.RegisterStartupScript, on an aspx page you can get away with just ClientScript.RegisterStartupScript. But if you use the full name it will work anywhere including user controls.

Hope this is of some help to someone out there.

Similar Posts

« »