Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Using a Customized Messagebox in ASP.NET

5.00/5 (7 votes)
17 Aug 2013CPOL2 min read 50.7K   3.5K  
Building a Custom User Control for Messagebox

Introduction

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.

Background

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

VB.NET
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

        PopUp.Show()
    End Sub

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

End Class

Using the Control in Pages

VB.NET
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

ASP.NET
<system.web>
<pages>
<namespaces>
<add namespace="MessageBox"/>
</namespaces>
<controls>
<add tagPrefix="msg" tagName="PopMsg" src="~/MessageBox.ascx"/>
</controls>
</pages>
</system.web> 

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..

License

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