All systems need a way to share status messages with a user, without giving them feedback they don’t what has or what will happen. There are so many different ways to send an alert (modal window, JavaScript alert, inline HTML, etc…), regardless of what you use it always help to be consistent. I built this control based on a few different ideas/examples a long time ago, and I seem to find more uses for it all the time. You can call it from the server or from the client using JavaScript, making it the perfect single solution “notification” solution.
Here is an example of what it looks like.
** The text after the heading, is the code that was used to trigger the message box.
The control is pretty straight forward, on the server it works by “re-injecting” the rendered control when you call Show function. Since the control does not use viewstate, every time you post back to the server and call show again, the last message disappears and the new message is displayed. If you want the message to disappear “automatically” after a few seconds, you can can set the timeout in milliseconds. On the client (via JavaScript), you can create a simple function that will provide access to throwing alerts from the client without a postback.
Client Side Example
<script type="text/javascript">
function ShowSuccess(message) {
$alert = $('#MBWrapper1');
$alert.removeClass().addClass('MessageBoxInterface');
$alert.children('p').remove();
$alert.append('<p>' + message + '</p>').addClass('successMsg').show().delay(8000).slideUp(300);
}
</script>
Server Side Example
public partial class _Default : System.Web.UI.Page
{
protected void Success_Click(object sender, EventArgs e)
{
MessageBox1.ShowSuccess("Success, page processed.", 5000);
}
}
ASP.NET MessageBox User Control – Full Working Demo
Download and try the demo, if you like what you see… Here is what you need to include to make the control work in your project.
- MessageBox User Control (ASMX & CS)
- jQuery
- IMessageBox.css stylesheet
- All the graphics from the Images folder
** Note: If you move the images into a different directory, you’ll need to update the CSS to use the correct path to the images.
#1 by Daniel on February 4, 2013 - 7:19 am
Hi Zach,
Thanks for posting this.
When I publish/build your website project, there’s an error: “Could not load type DevOne.Global”.
Can you help me?
#2 by Rumesh Srivastav on January 6, 2012 - 2:58 am
Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about Populate Grid Control From XML Document Easily, for more details of this post check out this link…
hppt://www.mindstick.com/Articles/535bd817-c3c1-46dd-be9c-f14e42c7db78/?Creating%20User%20Define%20Control%20in%20ASP%20.Net
Thanks
#3 by Zach on January 11, 2012 - 4:31 pm
Glad it helped.
#4 by Yosef on August 2, 2011 - 8:50 am
Thanks for posting this.
Will it work in an update panel? If not, how can it be modified to work?
Additionally, I would like to put this control at the top of Site.Master and call it from any of the pages. Would it work in that scenario?
Thanks
#5 by Zach on August 2, 2011 - 10:41 am
Yes, it will work in both scenarios without any modifications. If you want to add it to your master page you’ll need to wrap access to the control in a public property and add a MasterType directive to your ASPX page so you can call it like “Master.MessageBox.ShowSuccess();”.