Click here to Skip to main content
Licence CPOL
First Posted 27 Jul 2007
Views 11,209
Downloads 126
Bookmarked 8 times

Dynamic Message Display

By | 27 Jul 2007 | Article
Display messages dynamically with consistency across applications.

Introduction

This article describes how to display different types of messages such as warning, information, success, or error in a catchy and descriptive manner similar to Windows applications. Also, the look and feel of messages should be consistent across applications. This article's DisplayMessage control is loaded dynamiclly when a message is passed to load it.

Why needed?

Normally, web developers go for a label to display success or error messages.

  1. How to make the look and feel consistent for messages across applications?
  2. Is it possible to display an image along with a message?
  3. Can the message and image be changed dynamically?

This article answers the above questions clearly.

How to use it?

Add the DisplayMessage.cs, LoadFixer.cs, and displaymessage.aspx files to your application. Place a PlaceHolder control in the master page. Name to it. The same name should be used in your LoadFixer class to find the control.

//
// In master page
<tr>
    <td width="100%" height="10" align="center" valign="top" >
        <asp:PlaceHolder ID="MessagePlaceHolder" Visible="false" 
                     runat="server"></asp:PlaceHolder>
    </td>
</tr>
//

In the page file where you need to call the success message or error message, do this:

Success:
//
LoadFixer.AddPageMessage(this, "Success", "Test the success dispaly", 
   DisplayMessage.DisplayMessageType.GreenSuccess);
//
Error:
//
LoadFixer.AddPageMessage(this, "Error", "Test the error dispaly", 
          DisplayMessage.DisplayMessageType.RedError);
//

What does DisplayMessage do?

DisplayMessage is a user control which is loaded whenever a message is send to it. The sent message, heading, and image type gets assigned to the DisplayMessage UserControl and the message board gets created. Then it gets loaded and bound to the respective place.

How does it work?

DisplayMessage class

It holds all the Label controls for the message text and heading, and the image type for the message. In the page load event, the appropriate value is assigned to the control and it gets loaded.

//
protected void Page_Load(System.Object sender, System.EventArgs e) 
{
    try
    {
        string strMessage = string.Empty;
        //'check to see if a url
        //'was passed in for an icon
        if (!string.IsNullOrEmpty( IconImage))
        {
            strMessage += this.Text;
            //lblHeading.CssClass = "SubHead";
            //lblMessage.CssClass = "Normal";
            imgIcon.ImageUrl = IconImage;
            imgIcon.Visible = true;
        }
        else
        {
            switch (IconType)
            {
                case DisplayMessageType.GreenSuccess:
                {
                    strMessage += this.Text;
                    //lblHeading.CssClass = "SubHead";
                    //lblMessage.CssClass = "Normal";
                    lblHeading.ForeColor =Color.Green ;
                    lblHeading.Font.Bold = true;
                    lblMessage.ForeColor = Color.Green;
                    imgIcon.ImageUrl = SuccessImgPath;
                    imgIcon.Visible = true;
                    break;
                }
                case DisplayMessageType.YellowWarning:
                {
                    strMessage += this.Text;
                    imgIcon.ImageUrl = WrningImgPath;
                    imgIcon.Visible = true;
                    break;
                }
                case DisplayMessageType.RedError:
                {
                    strMessage += this.Text;
                    lblHeading.ForeColor = Color.Red;
                    lblHeading.Font.Bold = true;
                    lblMessage.ForeColor = Color.Red;
                    imgIcon.ImageUrl = ErrorImgPath;
                    imgIcon.Visible = true;
                    break;
                }
                case DisplayMessageType.WhiteInformation:
                {
                    strMessage += this.Text;
                    imgIcon.ImageUrl = InfrmationImgPath;
                    imgIcon.Visible = true;
                    break;
                }
            }
        }
        lblMessage.Text = strMessage;
        if (!string.IsNullOrEmpty(Heading))
        {
            lblHeading.Visible = true;
            lblHeading.Text = Heading + "<br/>";
        }
    }
#endregion
//

LoadFixer class

This class has two overloaded methods: GetDisplayMessageControl and AddPageMessage.

GetDisplayMessageControl

  • Loads the DisplayMessage User Control,
  • Assigns the passed image type, message heading, and text to the appropriate controls, and
  • Returns the DisplayMessage control.
//
#region GetDisplayMessageControl
/// <summary>
/// Loads the Message control and bind the values to it.
/// </summary>
/// <param name="Heading">Heading to be displayed</param>
/// <param name="Message">Message to be displayed</param>
/// <param name="objDisplayMessageType">Icon to be displayed</param>
/// <returns>Message control (usercontrol) object</returns>
private static DisplayMessage GetDisplayMessageControl(String Heading, 
        String Message, DisplayMessage.DisplayMessageType objDisplayMessageType)
{
    //'Use this to get a message control
    //user control for message display
    DisplayMessage objDisplayMessage = new DisplayMessage() ;
    LoadFixer objLoadFixer = new LoadFixer();
    objDisplayMessage = (DisplayMessage)objLoadFixer.LoadControl(MessageControlPath);
    //binds the values
    objDisplayMessage.Heading = Heading;
    objDisplayMessage.Text = Message;
    objDisplayMessage.IconType = objDisplayMessageType;
    return objDisplayMessage;
}
#endregion
//

AddPageMessage

  • Checks for the existence of the message place holder control in the master page of the passed page control.
  • If exists, then checks for non-emptiness of the message.
  • Then it calls GetDisplayMessageControl() to get the DisplayMessage control.
  • Lastly, it adds the message control to the place holder in the master page.
  • The page gets displayed with the message.
//
#region AddPageMessage

/// <summary>
/// Fix /Add the MessageControl to the MessagePlaceHolder with added message
/// </summary>
/// <param name="objUserControl">page called from</param>
/// <param name="Heading">Heading to be displayed</param>
/// <param name="Message">Message to be displayed</param>
/// <param name="objDisplayMessageType">Icon to be displayed</param>
public static void AddPageMessage(Page objUserControl, String Heading, 
       String Message, DisplayMessage.DisplayMessageType objDisplayMessageType)
{
    if (objUserControl != null)
    {
        if (!string.IsNullOrEmpty(Message))
        {
            //find the placeholder of the parent master page
            PlaceHolder MessagePlaceHolder = (PlaceHolder)
               objUserControl.Master.Master.FindControl(MessageHolderControlName);
            if (MessagePlaceHolder != null)
            {
                MessagePlaceHolder.Visible = true;
                DisplayMessage objDisplayMessage = new DisplayMessage();
                //loads the DisplayMessage user control
                objDisplayMessage = GetDisplayMessageControl(Heading, 
                                       Message, objDisplayMessageType);

                //binds the control to placeholder
                MessagePlaceHolder.Controls.Add(objDisplayMessage);
            }
        }
    }
}

#endregion
//
Sample output:

Screenshot - Success.gif

Screenshot - Information.gif

Conclusion

This article will help in displaying messages in your site with consistency.

License

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

About the Author

Anandhi K

Architect

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood work Pinmemberrilov10:09 27 Jul '07  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 27 Jul 2007
Article Copyright 2007 by Anandhi K
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid