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

Create your own custom error Web Control

, 14 Aug 2007
Rate this:
Please Sign up or sign in to vote.
This articles explains how to create a custom error Web Control whose properties can be changed at run time.

Introduction

This article demonstrates a simple way to create web controls that can be shared across multiple web applications and whose properties can be changed at run time based upon specific business conditions/rules.

Background

A custom error control has been designed to contain the following properties which can be adjusted and customized on each application/page:

  • ContainerWidth - The width of the table
  • ContainerHeight - The height of the table
  • ContainerPosition - Alignment of the table (Left/Right/Center)
  • ContainerBorder - The border size of the table
  • HeaderBackColor - Back color of the header
  • HeaderTextColor - Text color of the header
  • HeaderTextSize - Size of the text in the header
  • HeaderText - The text that would be displayed in the header
  • MessageBackColor - Back color of the message area
  • MessageTextColor - Text color of the message
  • MessageTextSize - Size of the text in the message area
  • Message - The text that would be displayed in the message area
  • MessageType - Category that would override and place the control in three pre-defined formats (Informational/Warning/Err)

Using the code

The control is designed in the ErrorControl class by inheriting from System.Web.UI.WebControls.WebControl and setting additional properties as listed in the background section above. In the same class, the code below overrides the Render method, and formats it in the appropriate manner based upon the values of the properties set by the referencing application at run time:

protected override void Render(HtmlTextWriter output)
{
    switch(type)
    {
        case "Informational":
        {
            ContainerWidth = 200;
            ContainerHeight = 100;
            ContainerBorder = 1;
            ContainerPosition = "Center";
            HeaderBackColor = "#006699";
            HeaderTextColor = "White";
            HeaderTextSize = 20;
            HeaderText = "Information!!";
            MessageBackColor = "ThreeDFace";
            MessageTextColor = "Black";
            MessageTextSize = 10;
            Message = "Message!!";
            break;
        }
        case "Warning":
        {
            ContainerWidth = 200;
            ContainerHeight = 100;
            ContainerBorder = 1;
            ContainerPosition = "Center";
            HeaderBackColor = "#ff6600";
            HeaderTextColor = "White";
            HeaderTextSize = 20;
            HeaderText = "Warning!!";
            MessageBackColor = "ThreeDFace";
            MessageTextColor = "Black";
            MessageTextSize = 10;
            Message = "Message!!";
            break;
        }
        case "Err":
        {
            ContainerWidth = 200;
            ContainerHeight = 100;
            ContainerBorder = 1;
            ContainerPosition = "Center";
            HeaderBackColor = "#ff0000";
            HeaderTextColor = "White";
            HeaderTextSize = 20;
            HeaderText = "Error!!";
            MessageBackColor = "ThreeDFace";
            MessageTextColor = "Black";
            MessageTextSize = 10;
            Message = "Message!!";
            break;
        }
    }
    output.Write("<table width=" + ContainerWidth + " height=" + 
                 ContainerHeight + " border=" + ContainerBorder + 
                 " align=" + ContainerPosition + " CellPadding=10>");
    output.Write("<tr height=20%><td bgcolor=" + HeaderBackColor + 
                 "><font color=" + HeaderTextColor + " size=" + 
                 HeaderTextSize + "><b><center>" + 
                 HeaderText + "</center></b></font></td></tr>");
    output.Write("<tr height=80%><td bgcolor=" + MessageBackColor + 
                 "><font color=" + MessageTextColor + " size=" + 
                 MessageTextSize + "><b>" + Message + 
                 "</b></font></td></tr></table>"); 
}

Create a simple web application, and add a reference to CustomErrorControl.dll. Add the control to the toolbox while in the design mode. Change the properties at runtime by modifying the WebForm1_PreRender event as follows:

private void WebForm1_PreRender(object sender, EventArgs e)
{
    ErrorContol1 = new CustomErrorControl.ErrorContol();
    ErrorContol1.MessageType = "Err";
    this.Controls.Add(ErrorContol1);
}

Conclusion

This article provides a basic understanding of the creation of a customizable web control. It will be helpful to display custom error messages in custom formats based upon certain business requirements.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Debdatta Dey
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 14 Aug 2007
Article Copyright 2007 by Debdatta Dey
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid