Wednesday, 2 March 2011

Win7 Style Messagebox Control for ASP.NET

it was implemented by using the ModalPopupExtender control from the AjaxControlToolkit as well as some additional CSS and images. 
Now I have tried to implement it using jQuery and CSS,am hoping you will like it too.









Returning a practical and easily recognizable message to a user is critical to a positive user experience on a website.  In this article we'll create a simple ASP.net user control to display a message back to the user.  The user control we will make can easily be drag and dropped onto any section of an aspx page or master page.  After that, it just takes one line of code to display a message.

Sprite for the Icons



The Html
Our html source code for the message box control is simply a wrapping container with an inner message type logo and the message to display.

  1. <asp:Panel ID="pnlMessage" Runat="server" Visible="False">
  2.     <div id="divMessagesLogo" runat="server" class="messages-logo"></div>
  3.     <div class="messages-text"><asp:Literal id="litMessage" runat="server"></asp:Literal></div>
  4. </asp:Panel>
  5. <asp:Literal ID="litSpace" Runat="server"></asp:Literal>

The Code

  1. public enum MessageType : int
  2. {
  3.     Error = 0,
  4.     Warning = 1,
  5.     Success = 2
  6. }
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. }
  10. public void SetMessage(string sMessage)
  11. {
  12.     SetMessage(sMessage, MessageType.Error);
  13. }
  14. public void SetMessage(string sMessage, MessageType messageType)
  15. {
  16.     pnlMessage.CssClass = "messages messages-error";
  17.     if (messageType == MessageType.Warning)    {
  18.         pnlMessage.CssClass = "messages messages-warning";
  19.     }
  20.     else if (messageType == MessageType.Success)    {
  21.         pnlMessage.CssClass = "messages messages-success";
  22.     }
  23.     pnlMessage.Visible = (sMessage.Length > 0);
  24.     litMessage.Text = sMessage;
  25.     litMessage.Visible = true;
  26.     litSpace.Text = "<br />";
  27. }
  28. public void ClearMessage()
  29. {
  30.     litMessage.Text = "";
  31.     litSpace.Text = "";
  32.     pnlMessage.Visible = false;
  33. }
You can style the MessageBox control however you want, but here is the styling I'll use for this example. 

The CSS


  1. .messages {width:75%; padding:5px; margin-left:3px}
  2. .messages-logo {height:32px; width:32px; float:left; background:url(message_logos.png)}
  3. .messages-text {margin-left:40px; padding:6px 0}
  4. .messages-error {background-color:#fbe3e4; color:#d12f19; border:solid 1px #fbc2c4}
  5. .messages-error .messages-logo {background-position:-64px 0}
  6. .messages-warning {background-color:#faffcf; color:#5e5f00; border:solid 1px #edef00}
  7. .messages-warning .messages-logo {background-position:-32px 0}
  8. .messages-success {background-color:#e0efd1; color:#003300; border:solid 1px #a5cc7a}
  9. .messages-success .messages-logo {background-position:0 0}

How-to Use It

Now that we have the control build.  Simply add the control to the page like in the example below.
  1. <%@ Register src="Messages.ascx" tagname="Messages" tagprefix="uc1" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>Messagebox Example</title>
  6.     <link href="messages.css" rel="Stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.         <uc1:Messages ID="MessageBox" runat="server" />
  11.      </form>
  12. </body>
  13. </html>


It just takes 1 line of code now to add your message.
MessageBox.SetMessage("ERROR: An example of an error message!");

That's it, hope you enjoy this control. :)