Code Sample: ASP.Net Message Control

Relaying information snippets and error messages is one of the most common tasks between web application developers. The most basic control this can be done with is the Literal. That’s great for an internal application or a school project, but in the real world information has to be both visually appealing and convey what the message is about using an icon or other form of illustration.

I will now take you briefly through a pretty simple control that has saved hours of my time. First, you may want to download the sample code, images, and style sheet at the end of this article.

Using MessageControl
The code here is pretty simple. If you already have a helper or control library you reference from most of your web projects, you may want to add this file to it. Once that is done, just register the assembly on a page (or in your web.config), implement the css classes, add images to your images folder, and start using it. For example:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="MessageControlTest._Default" %>
<%@ Register Assembly="Controls" TagPrefix="CC" Namespace="Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <link href="style.css" rel="stylesheet" type="text/css" />
        <title></title>
    </head>
    <body><form id="form1" runat="server"><div>
            Our test MessageControl lives below:
            <CC:MessageControl ID="msgInfo" runat="server" IconType="Info" 
                MessageText="Testing the Message Control" />
            <CC:MessageControl ID="msgWarn" runat="server" IconType="Warn" 
                MessageText="Testing the Message Control" />
            <CC:MessageControl ID="msgError" runat="server" IconType="Error" 
                MessageText="Testing the Message Control" />
    </div></form></body>
</html>

Yields the following:

image

Download the Solution

You can download a zip file of the sample solution by clicking the link below.

MessageControl.zip

Leave a Reply