Click here to Skip to main content
15,886,045 members
Articles / Message
Article

Customized Message Bar

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL4 min read 6.9K   1  
 Today I will show you the message bar which I normally used to display the standard place to display the messages like confirmation, errors,

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

 

Today I will show you the message bar which I normally used to display the standard place to display the messages like confirmation, errors, alerts etc. I also embed this customized message bar with ASP.net Validation Summary control and Validation controls will work accordingly.


Herr are simple steps which you have follow:


Add the below code in your aspx page this will display the customized message other than the Validation controls. For example you when you save the record in the database you can display on the page that record has been saved successfully.

<!-- Message bar-->
    <table style="border-collapse: collapse" width="100%" align="center" border="0">
        <tr>
            <td>
                <table id="msgs" style="border-collapse: collapse" bordercolor="#cccccc" cellpadding="1"
                    width="100%" align="left" border="1" runat="server">
                    <tbody>
                        <tr>
                            <td>
                                <table width="100%" bgcolor="#fffce1" border="0">
                                    <tr>
                                        <td align="center" width="20" bgcolor="#fffce1">
                                            <asp:Image ID="msgpic" runat="server"></asp:Image></td>
                                        <td class="message" bgcolor="#fffce1">
                                            <asp:Label ID="msgtext" runat="server"></asp:Label></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
    <!-- // Message bar-->

This control will display the in the same interface but it will only with the Validation controls, so add this line as well.

<asp:validationsummary id=validSum runat="server" CssClass="rtext" Width="100%" ForeColor=" " DisplayMode="SingleParagraph"></asp:validationsummary>


Add this line of code in you Page (Presentation Layer) code behind.

#region Help message code
    /// <summary>
    /// To display the message bar
    /// </summary>
    /// <param name="strError">Error test</param>
    /// <param name="dtype">pic type</param>
    /// <param name="myMsg">ok message </param>
    private void DisplayConfMsg(string strError, short dtype, string myMsg)
    {
        UserInterface.UI cls;
        string strMsg;
        switch (dtype)
        {
            case 1:
                if (string.IsNullOrEmpty(strError) == true)
                {
                    strMsg = myMsg;
                    cls = new UI(msgtext, msgs, msgpic);
                    cls.DisplayMsg(strMsg, false);
                }//if
                else
                {
                    strMsg = strError;
                    cls = new UI(msgtext, msgs, msgpic);
                    cls.DisplayMsg(strMsg, true);
                }//else
                break;
            case 2:
                strMsg = myMsg;
                cls = new UI(msgtext, msgs, msgpic);
                cls.DisplayMsg(strMsg, "1");
                break;
            case 3:
                strMsg = myMsg;
                cls = new UI(msgtext, msgs, msgpic);
                cls.DisplayMsg(strMsg, "3");
                break;
        }//switch
    }//DisplayConfMsg
    #endregion


This the User Interface Class where I kept all the function related to message bar. So just create one and paste this code.
      
  private System.Web.UI.HtmlControls.HtmlTable msgs;
        private System.Web.UI.WebControls.Label msgtext;
        private System.Web.UI.WebControls.Image msgpic;

        #region Message Bar

        public UI(System.Web.UI.WebControls.Label ml, System.Web.UI.HtmlControls.HtmlTable tb, System.Web.UI.WebControls.Image mpic)
        {
            msgs = tb;
            msgtext = ml;
            msgpic = mpic;

        }

        public void DisplayMsg(string textmsg, bool typemsg)
        {
            if (typemsg == true)
            {
                msgs.Visible = true;
                msgtext.CssClass = "errortext";
                msgtext.Text = textmsg;
                msgpic.ImageUrl = "images/errorpic.gif";


            }
            else
            {
                msgs.Visible = true;
                msgtext.CssClass = "oktext";
                msgtext.Text = textmsg;
                //msgtext.Style["color"] = "#666666";
                //msgtext.Style["CssClass"]="message";
                msgpic.ImageUrl = "images/okpic.gif";
                msgtext.Attributes.Add("class", "message");
            }
        }//DisplayMsg

        public void DisplayMsg(string textmsg, string typemsg)
        {
            if (typemsg == "1")
            {
                msgs.Visible = true;
                msgtext.CssClass = "errortext";
                msgtext.Text = textmsg;
                msgpic.ImageUrl = "images/msgpic.gif";
            }
        }//DisplayMsg

        /// <summary>
        /// for message bar
        /// </summary>
        /// <param name="textmsg"></param>
        /// <param name="typemsg"></param>
        public void DisplayStatus(string textmsg, short typemsg)
        {

            switch (typemsg)
            {
                case 1:
                    msgs.Visible = true;
                    msgtext.CssClass = "msgtext";
                    msgtext.Text = textmsg;
                    msgpic.ImageUrl = "images/testpic.gif";
                    break;
            }
        }

        /// <summary>
        /// for message bar
        /// </summary>
        public void ClearMsg()
        {
            msgs.Visible = false;
            msgtext.CssClass = "";
            msgtext.Text = "";
            msgpic.ImageUrl = "";
        }
        #endregion


After finish all the above steps finally you have to pass the messages (Presentation Layer)


//To display the confirmation messages
string err=””;
string msg=”Your record has been updated successfully.”;

DisplayConfMsg(err, 1, msg);

//To display the error messages
string err=”Error: Unattended error occord while updating the record.”;
string msg=””;

DisplayConfMsg(err,0, msg);


For Validation controls Add this control in the ASPX page.

<asp:RequiredFieldValidator ID="rfvFocalPoints" runat="server" ControlToValidate="ddlFocalPoints"
                                    CssClass="rtext" ErrorMessage='<table style="BORDER-COLLAPSE: collapse" width="100%" align="center" border="0"><tr><td><table id="msgs" style="BORDER-COLLAPSE: collapse" borderColor="#cccccc" cellPadding="1" width="100%" align="left" border="1" runat="server"><tbody><tr><td><table width="100%" bgColor="#fffce1" border="0"><tr><td align="center" width="20" bgColor="#fffce1"><IMG src="images/warn.gif" border="0"></td><td class="message" bgColor="#fffce1"><asp:label id="msgtext" CssClass="errortext" Runat="server">Please select Focal Point from the dropdown list.</asp:label></td></tr></table></td></tr></tbody></table></td></tr></table>'
                                    Font-Names="Wingdings 3" ForeColor=" " InitialValue="0">f</asp:RequiredFieldValidator>

The idea of choosing Validation Control messages and Customized message bar is because user will experience the same look n feel to view any system related messages.

Happy Coding!

This article was originally posted at http://wiki.asp.net/page.aspx/808/customized-message-bar

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --