Click here to Skip to main content
15,606,617 members
Articles / Web Development / ASP.NET
Posted 17 Aug 2013


12 bookmarked

Using a Customized Messagebox in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
17 Aug 2013CPOL2 min read
Building a Custom User Control for Messagebox


Most of us might have noticed how bad the ASP.NET message box is when prompting a message. Sometimes, the message box will be hidden in the taskbar and you will be waiting for the page to complete a postback. Some actions cannot be performed also like copying the text content from the message box and setting the position, etc. This customized message box will correct some of those limitations.


This Message box is built using ASP.NET VB and AjaxControlToolkit ver4 (April release).

Add a reference of Ajax ControlTookit to the website.

Using the Code

A custom user control does the job, making some variable public so that it can easily be accessed from any class. Below is the code for each phase:

Image 1

Image 2

The Message Box has a method which accepts 3 parameters:

  1. Message - The prompt for users
  2. Mode - Choose a style of display
  3. Title - Sets the title of the message box

Anytime the method is called, you can pass in the values for each parameter.

Message Box Mode

There are four (4) public variables which allow the user to easily select a mode for the message. They are made OPTIONAL and PUBLIC so that they can appear as Intellisense when calling it.

  1. IconInfo - displays the information icon
  2. IconExc - displays the Exclamation icon
  3. IconQues - displays the Question dialog for confirmation with YES/NO option
  4. IconError - displays the error icon and sets title of message to "An error has occurred!"

You will have to reference the user control to your web form or web.config and also add it as a namespace for quick reference.

User Control

Partial Class MessageBox
    Inherits System.Web.UI.UserControl

    Public Shared IconInfo, IconExc, IconQues, IconError As Object

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        IconInfo = "Show Information Icon"
        IconExc = "Show Warning Icon"
        IconQues = "Show Question icon and buttons"
        IconError = "Show Error Icon"
    End Sub

    Public Sub Pop(ByVal Message As String, Optional ByVal Mode As Object = "IconInfo",
                   Optional ByVal Title As String = "ToolTrak Message")

        lblMsg.Text = Message

        If Mode = IconInfo Then
            image.ImageUrl = "~/imgs/info.png"
            no.Visible = False
            ok.Text = "Ok"
            lblTitle.Text = "ToolTrak Message"
        ElseIf Mode = IconExc Then
            image.ImageUrl = "~/imgs/exc.png"
            no.Visible = False
            ok.Text = "Ok"
            lblTitle.Text = "ToolTrak Message"
        ElseIf Mode = IconQues Then
            image.ImageUrl = "~/imgs/ques.png"
            no.Visible = True
            ok.Text = "Yes"
            lblTitle.Text = "Confirm Action.."
        ElseIf Mode = IconError Then
            image.ImageUrl = "~/imgs/error.png"
            no.Visible = False
            ok.Text = "Ok"
            lblTitle.Text = "An Error has Occurred"
        End If

        If Title <> "" Then
            lblTitle.Text = Title
        End If

    End Sub

    Protected Sub no_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles no.Click
    End Sub

End Class

Using the Control in Pages

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim MsgStyle As String

    Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

        If cboStyle.SelectedIndex = 0 Then
            MsgStyle = "IconInfo"
        ElseIf cboStyle.SelectedIndex = 1 Then
            MsgStyle = "IconExc"
        ElseIf cboStyle.SelectedIndex = 2 Then
            MsgStyle = "IconQues"
        ElseIf cboStyle.SelectedIndex = 3 Then
            MsgStyle = "IconError"
        End If

        PopMsg.Pop(txtMessage.Text, MsgStyle, txtTitle.Text)
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        PopMsg.Pop("An error occurred! _
        Pls check and resolve!", IconError, "Error")
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
        PopMsg.Pop("I love CodeProject! _
        Really a developers website..", IconInfo, "Wow!")
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button5.Click
        PopMsg.Pop("Do you want to continue registration?", _
        IconQues, "Confirm")
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button4.Click
        PopMsg.Pop("Complete all fields in the registration form!", _
        IconExc, "InComplete Data")
    End Sub
End Class

Web Configuration

<add namespace="MessageBox"/>
<add tagPrefix="msg" tagName="PopMsg" src="~/MessageBox.ascx"/>

Points of Interest

You can copy the message from the message box, format message with HTML tags and set box position.
Just download and enjoy the concept..


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

Comments and Discussions

QuestionReturn Confirmation Pin
kadubarbalho12-Aug-15 17:04
kadubarbalho12-Aug-15 17:04 
AnswerRe: Return Confirmation Pin
Member 135272259-Apr-18 4:47
Member 135272259-Apr-18 4:47 
Questionreturn value for 'Yes/No' Pin
Member 1046865414-Apr-15 16:06
Member 1046865414-Apr-15 16:06 
GeneralMy vote of 5 Pin
Nirav Prabtani12-Jul-14 3:02
professionalNirav Prabtani12-Jul-14 3:02 
QuestionMy vote of 5! Pin
jalbitz24-Apr-14 4:14
jalbitz24-Apr-14 4:14 
QuestionMy vote of #5 Pin
BillWoodruff17-Feb-14 0:17
professionalBillWoodruff17-Feb-14 0:17 
GeneralMy vote of 5 Pin
JOE Heart Under Blade15-Dec-13 16:46
JOE Heart Under Blade15-Dec-13 16:46 

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

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