Click here to Skip to main content
6,306,412 members and growing! (20,368 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Beginner License: The Code Project Open License (CPOL)

Web Message Box for ASP.Net

By petersgyoung

Web Message Box for ASP.Net to send back server message to users
VB (VB 7.x, VB 8.0, VB 9.0), VBScript, Javascript, .NET (.NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5), ASP.NET, Dev, QA
Posted:9 Feb 2008
Views:11,605
Bookmarked:6 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 0.82 Rating: 1.17 out of 5
4 votes, 80.0%
1
1 vote, 20.0%
2

3

4

5

Introduction

Message box is a very useful control in Window-based application. For web-based application, you have similar control at client-side. However, when you want to convey server-side message to the user, there is no ready-for-use Message Box control. This article introduces a simple way to write a Message Box to convey server-side message back to user.

Using the code

Firstly, I add a Module to an ASP.Net web site:

Imports Microsoft.VisualBasic

Public Module Module1

    Public Function Msg(ByVal str As String, ByVal supportVB As Boolean, ByVal style As MsgBoxStyle) As String

        If supportVB Then

            str = str.Replace("""", "'")
            Return "<script language=""vbscript"" type=""text/vbscript"" >MsgBox """ & str & """," & CInt(style).ToString & ", ""Message Box""</script>"

        Else
            str = str.Replace("'", """")
            Return "<script>window.alert('" & str & "')</script>"        
       End If
    End Function

    Public Function Msg(ByVal str As String) As String
        Return Msg(str, False, MsgBoxStyle.Exclamation)
    End Function

End Module

The above function check whether browser support vbScript. If it supports, I use vbScript. It is because the message box of vbScript is more powerful than javascript. It allows you to control the display icon of your message. You may not want exclamation mark for your success message.

In order to use my Message Box in this web site, I also add a Label control which I call lblMessage at the bottom of the web page. The EnableViewState of lblMessage is set to False:

<label id="lblMessage" enableviewstate="False" runat="server" />

You can now use the Message Box in the web page as follows:

Try
    'your server action
    lblMessage.Text = Msg("Success Message", Request.Browser.VBScript, MsgBoxStyle.Information)
Catch ex As Exception
    lblMessage.Text = Msg(ex.Message)
 End Try

Points of Interest

Please note that you must set EnableViewState of lblMessage to false. It is because ASP.net default it to true. If you set it to true, the message will be kept for next postback. This means that the message will continue even if you corrected some error.

Code

You can download my code at http://download.biswaretech.com/resources.aspx?file=web_Msgbox.zip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

petersgyoung


Member
I am an IT consultant and software developer based in Hong Kong.

Please click here to see my blog and my company at biswaretech.com

Occupation: Founder
Company: Bisware Technology Limited
Location: Hong Kong Hong Kong

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Feb 2008
Editor:
Copyright 2008 by petersgyoung
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project