Introduction:
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
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
Page.Controls.Add(lbl)
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
Try
Dim r As
Integer
Dim x As
Integer = 100
Dim y As
Integer = 0
r = x / y
MessageBox(r.ToString())
Catch ex As
Exception
MessageBox("Error:
" & ex.Message.ToString() & " :: " &
_
ex.StackTrace())
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.
Summary:
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.