Click here to Skip to main content
15,921,169 members
Articles / Web Development / ASP.NET

An Easy Approach to Displaying a Message Box in ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.65/5 (12 votes)
16 Oct 20064 min read 91.2K   826   33   10
This article describes a quick and easy way to display message boxes within an ASP.NET 2.0 project.


This article describes a quick and easy way to display message boxes within an ASP.NET 2.0 project. The approach demonstrated may be used to dynamically generate and display error and status messages to the user.

This article includes a sample web application demonstrating the approach in use.

Image 1

Figure 1: Sample Web Application

Image 2

Figure 2: Sample Message Box Displaying Error Information

Getting Started

In order to begin, unzip the downloaded files and open the project provided. Within the project you will find a simple ASP.NET 2.0 application written in VB.NET; the web application contains a single web page (Default.aspx). The web page serves no purpose other than to demonstrate the use of the message box subroutine.

Create a virtual directory in IIS and point it at this web application, then open up the project in Visual Studio 2005.

The Code: Default.aspx

Open the code behind page for default.aspx and examine its content. The class is very simple and contains only the MessageBox subroutine and a few control event handlers built to exercise that subroutine.

In examining the code, you will note that the class does not import any libraries and contains only a minor bit of very basic code. For the purposes of this demonstration I have included five button event handlers and each of these handlers does something different with the MessageBox subroutine; these buttons will demonstrate sending a string directly to the subroutine, passing an error message to the subroutine, passing a string builder’s content to the subroutine, and passing the date and time to the subroutine.

In the use of this code within a viable application, the MessageBox subroutine would be a good way to display error and status messages to the user following a post back. Since this approach does not require the developer to know in advance what the content of the message is going to be, it is well suited to displaying any sort of dynamically generated text.

The primary point of interest is the MessageBox subroutine and its content is as follows:

Private Sub MessageBox(ByVal msg As String)
    ' define a javascript alertbox containing
    ' the string passed in as argument

    ' create a new label
    Dim lbl As New Label()

    ' add the javascript to fire an alertbox to the label and
    ' add the string argument passed to the subroutine as the
    ' message payload for the alertbox
    lbl.Text = "<script language="'javascript'">" & Environment.NewLine & _
               "window.alert('" + msg + "')</script>"

    ' add the label to the page to display the alertbox
End Sub

In examining the code you will note that the subroutine accepts a string as an argument and that the string passed to the subroutine is then passed directly to a JavaScript alert box for display. In order to display the alert, the subroutine creates a new instance of a label and passes the appropriate JavaScript command to the label’s text property. Labels will of course render any script or html commands passed to the control’s text property and because of this, when the label is added to the page in the final line of the subroutine, the alert box will immediately fire and the message passed in will be displayed to the user.

As this occurs in post back, the labels will not continue to accumulate in the page but rather only a single label will be added following each call to the MessageBox subroutine and at any given time only a single label will exist in the page source. This may be validated by examining the web page’s source whilst the application is running in the browser.

The application exercises the subroutine with a series of five button event handlers; examine the code behind page to see all of these handlers in use. I will only show a single handler in this document and that handler is the error message example; its code is as follows:

Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        Dim r As Integer
        Dim x As Integer = 100
        Dim y As Integer = 0
        r = x / y
    Catch ex As Exception
        MessageBox("Error:  " & ex.Message.ToString() & "  :: " & _
    End Try
End Sub

In examining this code note that a Try Catch block has been added and within the Try section, a divide by zero error is created. The Catch block uses the MessageBox subroutine to display a formatted error message containing a piece of text, the error message, and the error stack trace. When this button event handler fires, the application will attempt to perform the calculation in the Try block; this attempt will throw an exception, and when the exception is thrown, the related error message is displayed to the end user through the MessageBox subroutine.


The examples provided within this document and the related web application are used to show one approach to building a subroutine that may be used to display dynamic message content to the user. There are many other ways to generate and display alert boxes to the user, however, this is a convenient way to accomplish the task.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralThanks! Pin
Member 1117525213-May-15 5:08
Member 1117525213-May-15 5:08 
GeneralMy vote of 2 Pin
Andres Cichero9-Nov-10 4:46
Andres Cichero9-Nov-10 4:46 
GeneralMessage box not popup when ajaxified Pin
hyke19-Feb-10 4:51
hyke19-Feb-10 4:51 
QuestionHow to show message in a updata panel (i mean in ajax) Pin
linu4-Jun-08 21:47
linu4-Jun-08 21:47 
AnswerRe: How to show message in a updata panel (i mean in ajax) Pin
dvaldivieso24-Jun-08 17:10
dvaldivieso24-Jun-08 17:10 
GeneralNice Article Thanks for the idea. Pin
jeevansd21-May-08 1:09
professionaljeevansd21-May-08 1:09 
QuestionMessageBox not popup Pin
bbl_friend6-Apr-07 6:04
bbl_friend6-Apr-07 6:04 
AnswerRe: MessageBox not popup Pin
salysle6-Apr-07 22:11
salysle6-Apr-07 22:11 
Try ex.Message.ToString()
QuestionHow to implement it in App_Code??? Pin
rekoms131-Oct-06 7:15
rekoms131-Oct-06 7:15 
AnswerRe: How to implement it in App_Code??? Pin
salysle31-Oct-06 7:35
salysle31-Oct-06 7:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.