ASP.NET CSS Message Box Control
All web application need to display messages to users, including but not limited to erroneous or missing form entries and successful posting or saving of form data. Typically, I would use a label control on the page and set the appropriate text (‘Successful Update’, ‘Missing Email Address’, etc..) and CSS Class. Since I was using the same code on every page on every project. I decided to create a server control(sbcMsgBox) in c# that would reduce the amount of code I would need to write and that would meet the following criteria:
- Minimal code-behind.
- Automatically show the control to prevent having to explicitly code visibility.
- CSS based for easy styling.
- Minimal, table-less markup.
- Automatically set the appropriate styling (error, information, warning, success).
Figure 1. Sample message boxes produced by sbcMsgBox control
In order to use the sbcMsgBox control in your project, you’ll need to download and copy the dll to your project’s bin folder. If you want to see the control in your projects toolbox in order to drag and drop on a web page, simply right click on your toolbox and select ‘choose items’ from the pop up menu. Once the screen loads, select browse and choose the dll from your projects bin folder.
You will also need to reference the control either on your page or in your web.config file. To reference in your web.config, add the assembly to the <pages> section in <system.web>.
Figure 2. Referencing sbcMsgBox control in web.config <pages> <controls> <add assembly="sbcMsgBox" namespace="SBWebControls" tagPrefix="sbc" /> </controls> </pages>
To reference on your page, either drag the control onto your page in design mode or type the following at the top of the page.
Figure 3. Referencing sbcMsgBox in page <%@ Register assembly="sbcMsgBox" namespace="SBWebControls" tagprefix="sbc" %>
The sbcMsgBox control defines the following Properties, which can be set at design time declaratively or at run time in the code behind:
- ShowCloseButton – sets whether the message displayed can be closed. If set to yes you should see a little close icon in the upper right hand corner. The default value is true.
- UseJQuery – sets whether jQuery is used to animate the closing of the message with a slide-up or fade-out animation. This property is irrelevant if showCloseButton is set to false. The default value is false.
- fxType – sets the JQuery animation for SlideUp or FadeOut. The default value is FadeOut..
- BoxWidth – sets the width (ex., 350px) of the message box. If not set, the message will fill the width of the parent container control.
- Centered – sets with the message box will be centered within the parent container. This property only applies if the boxWidth is set.
- Title – if set, the message displayed will show a title
- ErrText, InfoText, WarnText & SuccessText – this is the message to display. The control will render the appropriate style (warning, error, etc..).
- ShowMessageImage – If set to ‘true’ the control will display a message icon (through css) appropriate to the type of message. The default value is ‘true’.
- CssClass – this sets the css class for styling the control. The default class name is sbc_msgbox (see Figure 4). If using the default style you don’t need to set this property.
- <CssClass> – defines main message div
- <CssClass> .closebutton – defines the image used for the close button. You can use your own image or the one provided (close.png)
- <CssClass> h4 – message title
- <CssClass> p – message text
- In order to style the different types of messages the control attaches the appropriate css class by appending (_err, _info, _warn, etc.) to the CssClass property (see Figure 5).
- <CssClass>_err (see css below)
- <CssClass>_info
- <CssClass>_warn
- <CssClass>_success
Figure 4. Default styling for sbcMsgBox control /* Shared styling for all the different */ /* message types */ .sbc_msgbox { padding:10px; color:#555; margin-bottom:10px; -moz-border-radius: 5px; border-radius: 5px; } .sbc_msgbox .closebutton { width:7px; height:7px; background-image:url(close.png); background-repeat:no-repeat; cursor:pointer; float:right; margin-top:-1px; margin-right:-1px; } .sbc_msgbox h4 { margin:0; font-size:110%; padding:3px 0; } .sbc_msgbox p { margin:0; padding:10px; }
Figure 5. Style for each of the different message types (error, warning, etc…) /* Style overrides for each of the different */ /* message types */ /* note: only displaying error message styling here. /* See css file in download for all message types */ /* error message */ .sbc_msgbox_err { background-color:#fcfce1; border:solid 1px #ffcc00; } .sbc_msgbox_err h4 { border-bottom:solid 1px #ffcc00; } .sbc_msgbox_err p.msgimg { background-image:url(error.png); background-repeat:no-repeat; background-position:left 10px; padding-left:25px; }
The sbcMsgBox control defines the following Methods:
- SetTitleText – this method takes three parameters (messageType, title and text) and simply sets the message type (info, error, etc..), title and text property in one line.
The demo project contains several examples of setting properties in markup as well as some code behind examples. I hope you enjoyed this article and find the sbcMsgBox control useful. Please comment or email me with any questions. Happy Programming!

