Home > Asp.net, C#, CSS > How to handle errors and other messages in asp.net pages

How to handle errors and other messages in asp.net pages


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>

Categories: Asp.net, C#, CSS Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment