Skip to main content

Message Panel



This in post show you how effectively you can show you messages and error regarding the user events using the Message Panel control. following are the step by step description of how to create and use Message panel.

Create Message panel

In your application add folder App_Code.
To do that right clicking on Web project select Add Asp.net folder >> App_Code



Add one class file name MessagePanel.cs and copy pest following code into it.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Controls
{
   // TODO: add clear link (display: none;) plus requried js
   [ToolboxData("<{0}:MessagePanel runat=\"server">")]
    public class MessagePanel : Panel, INamingContainer
    {
        private const string VSKEY_MESSAGE = "Message";
        private const string VSKEY_ERROR = "Error";

        bool _showMessagePanel;
        bool _showErrorPanel;

        string _messageCssClass;
        string _errorCssClass;
        string _messageIconUrl;
        string _errorIconUrl;

        public MessagePanel()
        {
            ViewState[VSKEY_MESSAGE] = String.Empty;
            ViewState[VSKEY_ERROR] = String.Empty;
        }
       
        #region Accessors
        public string Message
        {
            get { return (string)ViewState[VSKEY_MESSAGE]; }
            set { ViewState[VSKEY_MESSAGE] = value; }
        }

        public string Error
        {
            get { return (string)ViewState[VSKEY_ERROR]; }
            set { ViewState[VSKEY_ERROR] = value; }
        }

        public bool ShowMessagePanel
        {
            get { return _showMessagePanel; }
            set { _showMessagePanel = value; }
        }

        public bool ShowErrorPanel
        {
            get { return _showErrorPanel; }
            set { _showErrorPanel = value; }
        }

        public string MessageCssClass
        {
            get
            {
                if (this._messageCssClass == null)
                {
                    this._messageCssClass = "MessagePanel";
                }
                return _messageCssClass;
            }
            set { _messageCssClass = value; }
        }

        public string ErrorCssClass
        {
            get
            {
                if (this._errorCssClass == null)
                {
                    this._errorCssClass = "ErrorPanel";
                }
                return _errorCssClass;
            }
            set { _errorCssClass = value; }
        }

        public string MessageIconUrl
        {
            get
            {
                if (_messageIconUrl == null)
                {
                    _messageIconUrl = "~/Resources/images/messagebox_info.gif";
                }
                return _messageIconUrl;
            }
            set { _messageIconUrl = value; }
        }

        public string ErrorIconUrl
        {
            get
            {
                if (_errorIconUrl == null)
                {
                    _errorIconUrl = "~/Resources/images/messagebox_critical.gif";
                }
                return _errorIconUrl;
            }
            set { _errorIconUrl = value; }
        }

        #endregion

        protected override void Render(HtmlTextWriter writer)
        {
            if (null != Page)
                Page.VerifyRenderingInServerForm(this);

            if (this.ShowErrorPanel)
            {
                Panel errorPanel = BuildPanel(this.Error, this.ErrorCssClass, ErrorIconUrl);
                this.Controls.Add(errorPanel);
            }

            if (this.ShowMessagePanel)
            {
                Panel messagePanel = BuildPanel(this.Message, this.MessageCssClass, MessageIconUrl);
                this.Controls.Add(messagePanel);
            }

            base.Render(writer);
        }

         protected virtual Panel BuildPanel(string messageText, string cssClass, string imageUrl)
        {
            Panel result = new Panel();

            if (null != imageUrl && cssClass.Length > 0) result.CssClass = cssClass;

            if (null != imageUrl && imageUrl.Length > 0)
            {
                System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                image.ImageUrl = imageUrl;
                result.Controls.Add(image);
            }

            Panel message = new Panel();
            message.Controls.Add(new LiteralControl(messageText));
            result.Controls.Add(message);

            return result;
        }

        public void ShowMessage(string message)
        {
            ShowMessage(message, true);
        }

        public void ShowMessage(string message, bool clearExistingMessages)
        {
            if (clearExistingMessages)
                this.Message = message;
            else
                this.Message += " " + message;

            this.ShowMessagePanel = true;
            this.Visible = true;
        }

        public void ShowError(string message)
        {
            ShowError(message, true);
        }

        public void ShowError(string message, bool clearExistingMessages)
        {
            if (clearExistingMessages)
                this.Error = message;
            else
                this.Error += " " + message;

            this.ShowErrorPanel = true;
            this.Visible = true;
        }

        public void Clear()
        {
            this.Visible = false;
        }


    }
}



Add following CSS code in your .css file and put proper images at path shown into the code

/* Start: Message/Error Display CSS*/
div.MessagePanel {
/*background:#EEEEEE none repeat scroll 0%;*/
border:1px dashed #0BAF4D;
color:#0BAF4D;
font-weight:bold;
/*padding:5px;*/
font-size:13px;
margin-bottom:10px;
}


div.ErrorPanel {
/*background:#FFFFCC none repeat scroll 0%;*/
border:1px dashed red;
color:red;
font-weight:bold;
font-size:13px;
margin-bottom:10px;
}

div.ErrorPanel img, div.MessagePanel img {
float:left;
}

div.ErrorPanel div, div.MessagePanel div {
margin-left:32px;
padding-top:8px;
padding-bottom:8px;
text-align:left;
}
/* End: Message/Error Display CSS*/



How to put Message panel into .ASPX or .ASCX file

Add following into the web.config

<system.web>
  <pages>
     <controls>
         <add tagPrefix="Controls" namespace="Controls"/>
     controls>
  pages>
system.web>

Add following in your .ASPX or .ASCX file where you want to show message panel.

<Controls:MessagePanel ID="msgPanel" runat="server">
Controls:MessagePanel>


How to code with Message panel

In codebehind file use following methods to show and display messages and errors

To clear message

msgPanel.Clear();

To show message

msgPanel.ShowMessage("Meesage");

To show error

msgPanel.ShowError("Error");


Out put of message panel is like bellow

Message





Error




Comments

  1. this is useful for any .net framework ? or only support for .net 3.5 framework ?

    ReplyDelete
  2. I had tested it with 2.0, 3.0, 3.5

    ReplyDelete

Post a Comment

Popular posts from this blog

MongoDB - How to reduce storage size after deleting documents on replica set?

MongoDB does not free the disk space for OS use after deleting bunch of documents from the collection. We can reclaim that space using following cases. By using compact command - Can be run on collections, but block all orations on database, need small downtime By using repaireDatabase - Run on database need extra space and serious downtime By dumping database and than drop and restore it - Time consuming and need downtime By copy database using temporary name drop original and then re copy with original name - Time consuming and need downtime Following is the best method of achieve same without downtime on replica set Step down secondary server Delete database files Start server, It will start Replica Set Initial Sync. After sync over do the same with other secondary servers one by one For primary server step it down. Make sure one of the secondary become primary Delete database file on primary Start primary server It will start Replica Set Initial Sync. After R...

LINQ union with group by

In my recent development activity with LINQ query, I need to get the some of user points from tow different tables where user's points are based on some activities. There are many users who are into one table but not in another table and some are in both table. For example tables having data like this Table 1 UserID   Points 1           10 2           20 Table 2 UserID    Points 2           20 3           30 Result should be UserID    Points 1           10 2           40 3           30 I try to get this result with LINQ query using joins and group by samples from the web. I had googling to get something useful but dont get any success. So I try with new logic and here it is, I think it will help full to other also. Example Item class to use public cla...

SqlConnection from ObjectContext.Connection

Following is C# code to get the  SqlConnection from ObjectContext.Connection . This is usefully to run dynamically created query using ADO.NET using connection string specified into the EDMX ObjectContext object. C# Code Example : using ( EntityContext context = new EntityContext ()) { EntityConnection ec = ( EntityConnection )context.Connection; SqlConnection sc = ( SqlConnection )ec.StoreConnection; SqlDataAdapter da = new SqlDataAdapter (strQuery, sc); DataSet ds = new DataSet (); da.Fill(ds); return ds.Tables[0]; }