Create MessageBox user control using ASP.NET and CSS

In my previous article I wrote about an importance of handling different message types properly. In this article we'll create a simple ASP.NET user control and apply CSS styles shown in previous article.

Let me explain why we need a user control. If you want every aspx page to support same notification logic, you will need a same functionality on each page. Thus, a user control emerges as logic solution. This can be placed on each aspx page or on a Master Page if you use it. This way you can render any information or an error in the same manner on any place in your application. You can, for example, use this control to show your error messages.

msgbox

Let's see what functionalities we want to implement:

  • We want our control to render four different message types (described in previous article). Thus we'll need a way to inform our control which CSS class to implement
  • We need the ability to pass the text to be displayed
  • At the end, we want to have a close button on our control to enable a user to hide the message after reading. Beside this, we have to provide the developers with the ability to choose wether to show close button or not.

To do this, we'll create a new user control and apply the design that supports the requests above:

<div class="container">
<asp:Panel ID="MessageBox" runat="server">
<asp:HyperLink runat="server" id="CloseButton" >
<asp:Image runat="server" ImageUrl="~/images/close.png" 
AlternateText="Click here to close this message" />
</asp:HyperLink>
<p>
<asp:Literal ID="litMessage" runat="server"></asp:Literal></p>
</asp:Panel>
</div>

 

And CSS classes that style this control:

body{
font-family:Arial, Helvetica, sans-serif; 
font-size:13px;
}
.info, .success, .warning, .error, .validation {
border: 1px solid;
margin: 10px 0px;
background-repeat: no-repeat;
background-position: 10px center;
}
.info {
color: #00529B;
background-color: #BDE5F8;
background-image: url('images/info.png');
}
.success {
color: #4F8A10;
background-color: #DFF2BF;
background-image:url('images/success.png');
}
.warning {
color: #9F6000;
background-color: #FEEFB3;
background-image: url('images/warning.png');
}
.error {
color: #D8000C;
background-color: #FFBABA;
background-image: url('images/error.png');
}
.container
{
}
.info p, .success p, .warning p, .error p {
padding: 0px 50px;
}
.info a, .success a, .warning a, .error a {
float: right;
padding: 10px;
cursor:pointer;
}
.container img {
border: none;
}

We have one DIV that implements class "container". It will act as a most outer container. I left that class blank but you can apply some styles here, e.g. width. Next, we'll have one Panel control that will be accessible from the server, one Literal control that will render a message and one HyperLink with an Image that will be our Close button. That's our design.

Let's see what is happening in the code behind of our user control.

We'll have one boolean property that will indicate wether to render Close button or not.

public bool ShowCloseButton { get; set; }

Next, we'll declare an Enum that will hold our four different message types:

public enum MessageType
{
Error = 1,
Info = 2,
Success = 3,
Warning = 4
} 

Now, we need a method that will show the panel with the message and apply correct class.

public void Show(MessageType messageType, string message)
{
CloseButton.Visible = ShowCloseButton;
litMessage.Text = message;
switch (messageType)
{
case MessageType.Error:
MessageBox.CssClass = "error";
break;
case MessageType.Info:
MessageBox.CssClass = "info";
break;
case MessageType.Success:
MessageBox.CssClass = "success";
break;
case MessageType.Warning:
MessageBox.CssClass = "warning";
break;
}
this.Visible = true;
} 

This method do a few things:

  • It will show/hide Close button according to our ShowCloseButton property.
  • It will apply a message to a Literal
  • It will apply a CSS class based on MessageType Enum

Let me explain a few things here. First, I chose Literal instead of Label control, because Label is rendered in SPAN element and Literal to pure text.

Next, our Close button will be a HyperLink because it has to be accessible from the server. You are probably asking yourself why. There are two reasons for this. First, close button will have to be displayed ONLY if our ShowCloseButton property is set to true. If so, we have to attach "onclick" event handler to a HyperLink and pass it a ClientID of our Panel control. Why this?

There is a difference between an ID and a ClientID. Each server control will have a ClientID different form ID you gave it. ClientID consists of a control ID and all parent control's IDs. This is how JavaScript see our control. And if we want to find it on the client we'll search for a ClientID. ClientID typically looks like: ParentControl1ID_ParentControl2ID_ControlID.

If this isn't simple enough let me explain it this way - if we have more than one MessageBox panel on a page, document.getElementById won't find the right one if we try to get this element with its ID. You have to provide a full client name with all parent controls.

protected void Page_Load(object sender, EventArgs e)
{
if (ShowCloseButton)
CloseButton.Attributes.Add("onclick", "document.getElementById('" + 
MessageBox.ClientID + "').style.display = 'none'");
}

That's why we'll attach onclick event handler on the server.

This will work, but I am not satisfied with that large "switch" block in our Show method. We can optimize this a little. Since our class names and Enum items are the same, we can replace a complete "switch" block with a single line of code:

public void Show(MessageType messageType, string message)
{
CloseButton.Visible = ShowCloseButton;
litMessage.Text = message;
MessageBox.CssClass = messageType.ToString().ToLower();
this.Visible = true;
} 

This line will read the NAME of Enum item that we passed to our Show method and assign it to MessageBox panel as a CSS class.

What else can we do? We can create four wrapper methods to make it easier for developers:

public void ShowError(string message)
{
Show(MessageType.Error, message);
}
public void ShowInfo(string message)
{
Show(MessageType.Info, message);
}
public void ShowSuccess(string message)
{
Show(MessageType.Success, message);
}
public void ShowWarning(string message)
{
Show(MessageType.Warning, message);
} 

This isn't required, but I prefer to have more choices during development. So the final code will look like this

public partial class MyMessageBox : System.Web.UI.UserControl
{
#region Properties
public bool ShowCloseButton { get; set; }
#endregion
#region Load
protected void Page_Load(object sender, EventArgs e)
{
if (ShowCloseButton)
CloseButton.Attributes.Add("onclick", "document.getElementById('" + 
MessageBox.ClientID + "').style.display = 'none'");
}
#endregion
#region Wrapper methods
public void ShowError(string message)
{
Show(MessageType.Error, message);
}
public void ShowInfo(string message)
{
Show(MessageType.Info, message);
}
public void ShowSuccess(string message)
{
Show(MessageType.Success, message);
}
public void ShowWarning(string message)
{
Show(MessageType.Warning, message);
} 
#endregion
#region Show control
public void Show(MessageType messageType, string message)
{
CloseButton.Visible = ShowCloseButton;
litMessage.Text = message;
MessageBox.CssClass = messageType.ToString().ToLower();
this.Visible = true;
} 
#endregion
#region Enum
public enum MessageType
{
Error = 1,
Info = 2,
Success = 3,
Warning = 4
} 
#endregion
}

Now, let's see how our user control can be shown on a pages:

<uc1:MyMessageBox ID="MyMessageBox1" runat="server" ShowCloseButton="true" />
<uc1:MyMessageBox ID="MyMessageBox2" runat="server" ShowCloseButton="false" />
<uc1:MyMessageBox ID="MyMessageBox3" runat="server" ShowCloseButton="true" />
<uc1:MyMessageBox ID="MyMessageBox4" runat="server" />

You can just drag&drop a control on a form and set (or omit) ShowCloseButton property. In a code behind, you can show this control in many ways:

MyMessageBox1.ShowInfo("This is an information. This MessageBox works!");
MyMessageBox2.Show(MyMessageBox.MessageType.Success, "Your profile has been changed");
MyMessageBox3.ShowError("This is an error message");
MyMessageBox4.ShowWarning("This is a warning!");

Conclusion

You saw how to create ASP.NET user control that can handle different message types. You also saw how to implement a Close button on a server control.

If you want to try this you can download a sample code shown in this article.

I am aware that this code isn't perfect, although it did a great job for me on several projects. Did you ever need or do something similar? Do you have any other ideas how this could be implemented? Share it!

More articles in Blog archive or elsewhere
Advertisement

25 Comment(s)

jrummell

jrummell 28 May 2008 #

Very nice.  Thanks for sharing!

Khushal

Khushal 29 May 2008 #

Hey Great Work!!! Keep it up. Smile

video script

video script 29 May 2008 #

Excellent article, I like it very much.

Adnan

Adnan 30 May 2008 #

Kudos on this great tutorial. Very helpful. Eager for more. Good Luck!

ari

ari 03 Jun 2008 #

אהבתי מאוד תודה רבה!
Like It.Thanks

Vlad

Vlad 06 Jun 2008 #

Hi janco,
Great article and great idea! I really like your code style.
I decided to use your code as a base for my user control, and started playing with it.
I work with .NET 2.0, so I had to make some small changes (like property) to the code, but other than that it was works nice.
Messages are usually not shown at page load as in your example, so I tried to create a message from code behind on the fly (without it being defined in html) - no success, but with the definition in html, all works nice but one thing - close button...it shows even when the control is not visible on page load. This is resolved easily by adding
CloseButton.Visible = false;
in controls page_load.
The things I would like to add/change in this control:
1. Control the size from code behind
2. Positioning from code behind
3. Floating
4. Draggable (not sure about this..)
Will update here if will make something usable Smile


Janko

Janko 06 Jun 2008 #

Vlad, thanks for the comment! You are absolutely right CloseButton.Visible = false; has to be added in control's Page_Load event.

Regarding visibilty: that is correct, these message boxes are not shown in page load. You'll have to implement "visible" property in user control that could be set later on a page.

I appreciate your efforts! If you come up with something, please share it. Thanks again!

piggy

piggy 12 Jun 2008 #

hey cool one buddy. u rock.

Chirdeep

Chirdeep 27 Jun 2008 #

Great work mate, keep it up and thanks for sharing.

Cheers!!!

jrummell

jrummell 09 Jul 2008 #

Once again, excellent work!  Here's my take on it: john.rummell.info/.../...e-MessageBox-control.aspx.

Janko

Janko 09 Jul 2008 #

@jrummell: excellent example, thanks!

Vladimir

Vladimir 11 Jul 2008 #

Good article!
I changed the close button to be asp:ImageButton instead of asp:Image so that I can add the server call: OnClick="HideMe" which was implemented:
        protected void HideMe(object sender, EventArgs e)
        {
            this.Visible = false;
        }
I did that because i had some issue with complex ajax page using this control.
And yeah if using .net 2.0 property should be:
        private bool _ShowCloseButton;
        public bool ShowCloseButton
        {
            get { return _ShowCloseButton; }
            set { _ShowCloseButton = value; }
        }
instead of:
public bool ShowCloseButton { get; set; }

Good job!
Vlad

Nikola

Nikola 15 Jul 2008 #

KUDOs for the great article, and outstanding Blog!!!
I like the idea of presenting Validation errors this way but I couldn't find the explanation how to relate ValidationSummary control to MessageBox control.
Cheers, and looking forward to find more magic on your blog..
n.

Janko

Janko 15 Jul 2008 #

Nikola, thanks for the compliments!

What I wanted to show about ValidationSummary is that you can style it just like other message boxes simply by applying "validation" class to it. This si obvoius but, you would be surprised if I tell you that most of the developers I worked with think that ValidationSummary control must be simple bulleted list with white background. Smile

Bill Beckelman

Bill Beckelman 16 Jul 2008 #

Janko,

Thank you for the great post. I modified your control to use the ModalPopup control from the AjaxControlToolkit. Please take a look if you get time as I would appreciate you input.

beckelman.net/.../...Message-Box-User-Control.aspx

Best Regards,
Bill Beckelman

Lawrence77

Lawrence77 10 Jan 2009 #

very hard to learn caz i'm a beginner...
Anyway GoodWork...

Deepak Nigam

Deepak Nigam 25 Feb 2009 #

Nice work dude.......

Jooma

Jooma 26 Feb 2009 #

Good tutorial. I tried to do  exactly the same in VB.NET. Everything's find except the "Close Button" is click the message box won't go away, it  just stay there. Any suggestion on doing this in VB.NET ? If possible can you please provide the sample with VB.NET too ? Thanks a lot for this lesson.

Marvin..

Marvin.. 01 Apr 2009 #

Great work!, just a question regarding updatepanel, has any one work around this problem?

Malte Hansen

Malte Hansen 23 Apr 2009 #

If you want to extend this article.
I think it would be interesting to see how to use the messagebox the best way, i mean how to display error, succes and warning messages across a site.

I was playing around with it on a masterpage, and then calling it from a class all around the site.
But i havent had success because i havent been able to fetch the type (MessageBox), to be able to cast the usercontrol to the right type, to get the showMessage Methods..

That may be a little too code-concerned to fit in your style..
btw, your tutorial really inspired me Smile

Marvin Rodas

Marvin Rodas 23 Apr 2009 #

hi Janko, i´m using your usercontrol in my website however i got a problem when using Ajax´s updatepanel and progresspanel because the messagebox will show up without the message nor the image, just the close button and OK button will appear. After the first try I press the OK button and then it will work fine but not the first time.

thanks for any suggestion to the problem.

Tomas Matejka

Tomas Matejka 19 May 2009 #

I really love this one....

Venkat

Venkat 25 May 2009 #

Hi,

  It  is very good one.

Thanks

Venkat

mrunal

mrunal 21 Oct 2009 #

Hi
i m mrunal
thank u so much friend

Syed

Syed 26 Oct 2009 #

I like your work it is really helpful..
Keep it up man.. keep posting more

Comments are closed
via Ad Packs
9292