|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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 StartedIn 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.aspxOpen the code behind page for default.aspx and examine its content. The class is very simple and contains only the 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 In the use of this code within a viable application, the The primary point of interest is the 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 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 SummaryThe 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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||