Click here to Skip to main content
15,348,882 members
Articles / Web Development / ASP.NET
Posted 9 Feb 2008


14 bookmarked

Web Message Box for ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.46/5 (6 votes)
9 Feb 2008CPOL1 min read
A message box for ASP.NET to send back server messages to users.


Message box is a very useful control in Window-based applications. For web-based applications, you have a similar control on client-side. However, when you want to convey a server-side message to the user, there is no ready-to-use message box control. This article introduces a simple way to write a message box to convey server-side message back to the user.

Using the code

Firstly, I add a Module to the ASP.NET website:

Imports Microsoft.VisualBasic

Public Module Module1

    Public Function Msg(ByVal str As String, ByVal supportVB As Boolean, _
                        ByVal style As MsgBoxStyle) As String

        If supportVB Then

            str = str.Replace("""", "'")
            Return "<script language=""vbscript"" type=""text/vbscript"" >MsgBox """ & _
                   str & """," & CInt(style).ToString & ", ""Message Box""</script>"

            str = str.Replace("'", """")
            Return "<script>window.alert('" & str & "')</script>"        
       End If
    End Function

    Public Function Msg(ByVal str As String) As String
        Return Msg(str, False, MsgBoxStyle.Exclamation)
    End Function

End Module

The above function checks whether the browser supports VBScript. If so, I use VBScript. The message box of VBScript is more powerful than that of JavaScript. It allows you to control the display icon of your message. You may not want an exclamation mark for your success message.

In order to use my message box in the web site, I also add a Label control which I call lblMessage at the bottom of the web page. The EnableViewState of lblMessage is set to False:

<label id="lblMessage" enableviewstate="False" runat="server" />

You can now use the message box in a web page as follows:

Image 1

    'your server action
    lblMessage.Text = Msg("Success Message", _
                      Request.Browser.VBScript, MsgBoxStyle.Information)
Catch ex As Exception
    lblMessage.Text = Msg(ex.Message)
End Try

Points of interest

Please note that you must set EnableViewState of lblMessage to False. ASP.NET defaults it to True. If you set it to True, the message will be kept for the next postback. This means that the message will continue even if you correct an error.


You can download the latest version of my code at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Founder Bisware Technology Limited
Hong Kong Hong Kong
I am an IT consultant and software developer based in Hong Kong.

Please click here to see my blog and my company at

Comments and Discussions

-- There are no messages in this forum --