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.
- How to make the look and feel consistent for messages across applications?
- Is it possible to display an image along with a message?
- 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;
if (!string.IsNullOrEmpty( IconImage))
{
strMessage += this.Text;
imgIcon.ImageUrl = IconImage;
imgIcon.Visible = true;
}
else
{
switch (IconType)
{
case DisplayMessageType.GreenSuccess:
{
strMessage += this.Text;
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
private static DisplayMessage GetDisplayMessageControl(String Heading,
String Message, DisplayMessage.DisplayMessageType objDisplayMessageType)
{
DisplayMessage objDisplayMessage = new DisplayMessage() ;
LoadFixer objLoadFixer = new LoadFixer();
objDisplayMessage = (DisplayMessage)objLoadFixer.LoadControl(MessageControlPath);
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
public static void AddPageMessage(Page objUserControl, String Heading,
String Message, DisplayMessage.DisplayMessageType objDisplayMessageType)
{
if (objUserControl != null)
{
if (!string.IsNullOrEmpty(Message))
{
PlaceHolder MessagePlaceHolder = (PlaceHolder)
objUserControl.Master.Master.FindControl(MessageHolderControlName);
if (MessagePlaceHolder != null)
{
MessagePlaceHolder.Visible = true;
DisplayMessage objDisplayMessage = new DisplayMessage();
objDisplayMessage = GetDisplayMessageControl(Heading,
Message, objDisplayMessageType);
MessagePlaceHolder.Controls.Add(objDisplayMessage);
}
}
}
}
#endregion
Sample output:


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