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.
Figure 1: Sample Web Application
Figure 2: Sample Message Box Displaying Error Information
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)
Dim lbl As New Label()
lbl.Text = "<script language=" "window.alert('" + msg + "')</script>"
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() & " :: " & _
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
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.