Friday, July 30, 2010

How to handle errors and other messages in asp.net pages (A centralized approach)

Demo:
Idea is to have a single method in the PageBase called ShowMessage(...) and whenever we need to need to display message we can have pages inherit from PageBase and add PlaceHoder to the page and call this.ShowMessage(...)

BasePage And Message Type Enumaration:

public enum MessageType { Error, Information, Success }
 
public class PageBase : Page
{
    public void ShowMessage(PlaceHolder phMessage, string message, MessageType type)
    {
        if (phMessage != null)
        {
            Panel panel = new Panel() { CssClass = string.Format("{0}Message", 
                type.ToString()) };
            Label lblMessage = new Label() { Text = message, CssClass = "MessageText" };
            panel.Controls.Add(lblMessage);
            phMessage.Controls.Add(panel);
        }
    }
}

Page - Code:

public partial class TestPageBase
{
    protected void Process(object sender, EventArgs e)
    {
        try
        {
            this.btnSave.Enabled = true;
            ///
            /// just fake exception to demonstrate
            ///
            if (DateTime.Now.Second % 3 == 0) throw new Exception();
            this.ShowMessage(this.phMessage, "Processed... You need to save data to proceed...",
                MessageType.Information);
        }
        catch
        {
            this.ShowMessage(this.phMessage, "There is problem with processing data", 
                MessageType.Error);
            this.btnSave.Enabled = false;
        }
    }
    protected void Save(object sender, EventArgs e)
    {
        try
        {
            ///
            /// save data
            ///
            this.ShowMessage(this.phMessage, "Data saved successfully...", 
                MessageType.Success);
        }
        catch
        {
            this.ShowMessage(this.phMessage, "There is a problem of saving data", 
                MessageType.Success);
        }
    }
}

Page - Markup:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <style type="text/css">
  div.ErrorMessage,
  div.InformationMessage,
  div.SuccessMessage { padding:5px; }
  div.ErrorMessage { border:solid 1px #f00background-color:#fb9d9dcolor:#000 !important; }
  div.InformationMessage { bordersolid 1px #f59600background-color:#fac26a; }
  div.SuccessMessage { border:solid 1px #298012background-color:#82cf6e;  }    
 </style>
 <script runat="server">            
 </script>
 <script language="javascript" type="text/javascript">
 </script>
</head>
<body>    
 <form id="form1" runat="server">   
  <asp:PlaceHolder runat="server" ID="phMessage" />             
  <div>
   <asp:Button runat="server" ID="btnProcess" Text="Process" OnClick="Process" />
   <asp:Button runat="server" ID="btnSave" Text="Save" OnClick="Save" />
  </div>
 </form>
</body>
</html>


2 comments:

Ankur Nigam said...

Hi,
Won't you think that the title is little inappropriate to its content?
The post talks about how to show any generated error message to user whereas the title indicates that its going to talk about how to handle generated error message appropriately without resulting in application crash.
As the post title suggests, you should rather talk something like centralized error handling or something.
Won't you agree?

Charith Shyam Gunasekara said...

I agree with you. Thank you very much.

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...