Click here to Skip to main content
Click here to Skip to main content

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

, 16 Oct 2006
Rate this:
Please Sign up or sign in to vote.
This article describes a quick and easy way to display message boxes within an ASP.NET 2.0 project.

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.

License

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

Share

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 PinmemberAndres Cichero9-Nov-10 5:46 
GeneralMessage box not popup when ajaxified Pinmemberhyke19-Feb-10 5:51 
QuestionHow to show message in a updata panel (i mean in ajax) Pinmemberlinu4-Jun-08 22:47 
AnswerRe: How to show message in a updata panel (i mean in ajax) Pinmemberdvaldivieso24-Jun-08 18:10 
GeneralNice Article Thanks for the idea. Pinmemberjeevansd21-May-08 2:09 
QuestionMessageBox not popup Pinmemberbbl_friend6-Apr-07 7:04 
AnswerRe: MessageBox not popup Pinmembersalysle6-Apr-07 23:11 
QuestionHow to implement it in App_Code??? Pinmemberrekoms131-Oct-06 8:15 
AnswerRe: How to implement it in App_Code??? Pinmembersalysle31-Oct-06 8:35 
Hello:
 
Email me the whole code block you are using; I think I see an issue here but I want to look at what you have.
 
Thanks,
Scott Lysle

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 16 Oct 2006
Article Copyright 2006 by salysle
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid