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

Using a Customized Messagebox in ASP.NET

, 17 Aug 2013
Rate this:
Please Sign up or sign in to vote.
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:

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

        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

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

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

About the Author

Prince Tegaton
Software Developer Hallmarkit Business Solutions
Nigeria Nigeria
I am a young boy who love doing anything related to the ICT world.
I love programming in .Net Framework (Software/Dynamic Web). I use VB, ASP.Net VB, HTML, Graphics design and some cool tricks on Windows..
I have been involved in some software project development and numerous web application being the Lead in Most.
Am Tegaton - Proudly Nigerian
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionMy vote of 5! Pinmemberjalbitz24-Apr-14 4:14 
QuestionMy vote of #5 PinmemberBillWoodruff17-Feb-14 0:17 
GeneralMy vote of 5 PinmemberJOE MIS15-Dec-13 16:46 

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 | Mobile
Web04 | 2.8.140709.1 | Last Updated 18 Aug 2013
Article Copyright 2013 by Prince Tegaton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid