First we need to define the Message box in Master Page

<div id="divMessagePanel" runat="server" class="ShowMessage">
<asp:Label ID="lblError" runat="server"></asp:Label>
</div>




And we define the Common Function to show a Message

-          Enum declare for user friendly Param pass

   public enum ShowMessageType
        {
            EMPTY = 0,
            INFORMATION = 1,
            ERROR = 2,
            WARNING = 3
        }


/// <summary>
/// Shows the User defined Message here
/// </summary>
/// <remarks>
/// e.g.: 
/// CommonBehavior.ShowMessage(CommonBehavior.ShowMessageType.ERROR, ex.Message.ToString(), pnlStatus, lblStatus, imgStatus);
/// </remarks>
/// <param name="enumMsgType">Display type of the Message. ex : CommonBehavior.ShowMessageType.ERROR</param>
/// <param name="strMsg">Pass the display message text.</param>
/// <param name="pnlShow">Pass the Pannel Control id for Show the message</param>
/// <param name="lblMsg">pass the Lable control id for display the message</param>
/// <param name="imgShow">If you display image then Pass Image Control id, otherwise pass 'null'</param>
/// <returns>Void</returns>
public static void ShowMessage(ShowMessageType enumMsgType, String strMsg, HtmlControl pnlShow, Label lblMsg, Image imgShow)
{
     pnlShow.Visible = true;
     lblMsg.Text =  strMsg ;
     if (enumMsgType.Equals(ShowMessageType.EMPTY))
     {
        pnlShow.Visible = false;
     }
     else if (enumMsgType.Equals(ShowMessageType.ERROR))
     {
        /* if (imgShow != null)
        {
         imgShow.CssClass = "ErrorImgageShow";
         imgShow.ImageUrl = @"~\images\Error.jpg";
        }
        lblMsg.ForeColor = System.Drawing.Color.Red;*/
        lblMsg.CssClass = "errorText";
      }
      else if (enumMsgType.Equals(ShowMessageType.INFORMATION))
      {
        lblMsg.CssClass = "resultText";
      }
      else if (enumMsgType.Equals(ShowMessageType.WARNING))
      {
         lblMsg.CssClass = "warnText";
      }
    }


Now, we can show the user friendly message or exception message using the above function i.e anywhere in your page(.cs file),

catch (Exception ex)
{
CommonBehavior.ShowMessage(CommonBehavior.ShowMessageType.ERROR, ex.Message, (HtmlControl)Master.FindControl("divMessagePanel"), (Label)Master.FindControl("lblError"), (Image)Master.FindControl("imgStatus"));
}



Whenever the page call the function then the message will be displayed after the page load.

Comments (0)