Click here to Skip to main content
Click here to Skip to main content

AJAX Enabled MessageBox

By , 14 Sep 2009
 

Introduction

As you know, MessageBox, I mean showing popup messages to users is not hard, but there are some complicated issues in web applications because there is no "MessageBox.Show(...)" option in ASP.NET. Inshallah, at the end of this article we will have a similar one.

Using the Code

To get a better idea about our purpose, here is the list of features of the MessageBox that we will generate:

  • The MessageBox should have a simple usage. We should show the message with such a single line of code.
  • The MessageBox should be modal. I mean, a user should not be able to do any other operation while the message is open.
  • The MessageBox should support multiple messages. Multiple messages may be shown through one postback.
  • The MessageBox should have a different appearance according to the type of the message, whether it is an error message or just an information message.
  • The MessageBox should have confirmation functionality.
  • The MessageBox should be AJAX ready.

Now we know what we need and let's analyze how it works.

The MessageBox will be a usercontrol and can be used by adding to the page or to another usercontrol. A simple drag and drop operation.

First we need a message class. Message class is a simple class, having only two properties which are MessageText of string type and MessageType of enmMessageType type. enmMessageType has four items: Error, Success, Attention and Info.

The usercontrol has a method named AddMessage(). This method has two overloads:

public void AddMessage(string msgText, enmMessageType type){....}        
public void AddMessage(string msgText, 
	enmMessageType type, bool postPage, bool showCancelButton, string args){...}

Another important thing about this usercontrol is that it is AJAX enabled. All the contents of the usercontrol are inside an UpdatePanel named udpMsj which has an UpdateMode of Conditional. And OnPreRender state of the usercontrol if any message is added, the UpdatePanel is updated by calling the Update() method. In this way, the content is refreshed only if it is needed.

I will not describe these methods and any other parts of the usercontrol any more, instead I will show the usage of these methods and this will make things precise.

1. Simply Showing A Message

protected void btnInfo_Click(object sender, EventArgs e) 
{     
     uscMsgBox1.AddMessage("The user saved successfully but should be activated 
	by an administrator within 3 days.", 
	YaBu.MessageBox.uscMsgBox.enmMessageType.Info); 
}

This will show the following message:

InfoMessage

Simply by changing the second parameter of the AddMessage method which is YaBu.MessageBox.uscMsgBox.enmMessageType.Info, the message will have a different appearance as shown below.

When the MessageType is set to ERROR:

ErrorMessage

When the MessageType is set to ATTENTION:

AttentionMessage

When the MessageType is set to SUCCESS:

SuccessMessage

2. Show Multiple Messages at a Time

protected void btnMultiple_Click(object sender, EventArgs e) 
{ 
     uscMsgBox1.AddMessage("The user saved successfully.", 
		YaBu.MessageBox.uscMsgBox.enmMessageType.Success);
     uscMsgBox1.AddMessage("The newly saved user account should be 
		activated by an administrator within 3 days", 
		YaBu.MessageBox.uscMsgBox.enmMessageType.Attention);
}

When multiple messages are added:

MultipleMessages

3. Show Confirmation Message

And lastly, we can use it to get confirmation. But this is not as simple as the above ones:

protected void Page_Load(object sender, EventArgs e)
{
     uscMsgBox1.MsgBoxAnswered += MessageAnswered;
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
     uscMsgBox1.AddMessage("Do you confirm to save a new user?.", 
	YaBu.MessageBox.uscMsgBox.enmMessageType.Attention, true, true, txtArg.Text);
}
public void MessageAnswered(object sender, YaBu.MessageBox.uscMsgBox.MsgBoxEventArgs e)
{
     if (e.Answer == YaBu.MessageBox.uscMsgBox.enmAnswer.OK)
     {
          uscMsgBox1.AddMessage("You have just confirmed the transaction. 
		The user saved successfully. You have entered " + txtArg.Text + 
		" as argument.", YaBu.MessageBox.uscMsgBox.enmMessageType.Info);
     }
     else
     {
          uscMsgBox1.AddMessage("You have just cancelled the transaction.", 
				YaBu.MessageBox.uscMsgBox.enmMessageType.Info);
     }
}

The first messagebox for confirmation looks like:

Confirm1Message

And the second one after the user's answer looks like this:

Confirm2Message

That's all, enjoy it. A simple application is attached to this article.

History

  • September 15 2009: First release

License

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

About the Author

Yavuz Küçükpetek
Software Developer (Senior) NetCad
Turkey Turkey
Member
Yavuz is a software developer since 2001 and working as senior software developer at NetCad in Ankara, Turkey.
 
He has experience in mostly Microsoft technologies such as ASP.Net, SQL Server, C#, VB.Net, WPF, WCF etc. and especially interested in security and performance issues.
 
He also writes articles both in Turkish and English at:
http://www.dotnetgurus.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionError Trying to use this control in a different controlmemberMember 267020814 Sep '12 - 8:39 
I am trying to use this control in a different user control. I am getting the following error. I registered the user control in the parent control.
 
Microsoft JScript runtime error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'MainContent_WebPartManager1_MsgBox1_udpMsj'. If it is being updated dynamically then it must be inside another UpdatePanel.
 
Do I need to use it differently when I use it in the control.
 
Thanks for the help in advance.
 
Rao
Questionplease upload code in vb?memberjichp17 Apr '12 - 17:07 
hi , good work do you also have the code in vb? the example in vb is down thanks
SuggestionSource code of the 'YaBu.MessageBox.dll'...memberReza5529 Jan '12 - 22:39 
This article is very nice Big Grin | :-D
 
Please give a download link for source code of the 'YaBu.MessageBox.dll' (if may be for you).
 
Thanks a lot
impossible is impossible

QuestionIssue with key "Enter"membernospherato21 Dec '11 - 5:10 
thanks for the article,, this message box is amazing, but i'm facing an issue, when i use the message box as a custom control then drag & drop it in my page , the message pop up when i press "Enter key" when the focus be only on any textbox any help to overcome this problem will be appreciated Smile | :)
GeneralMy vote of 5memberMathan_CodeProject19 Nov '11 - 21:25 
very good article, fitted excatly.....
GeneralVery cool.memberMember 427806515 Sep '11 - 5:07 
Very cool. It was just what I wanted.
I made some adjustments in order to use VS2010.
 
Thank you.
GeneralRe: Very cool.memberpjotr_III29 Nov '11 - 0:17 
Can you please provide the adjustments you made for VS 2010.That woul#d be great.
Thanks in advance
Peter

GeneralRe: Very cool.memberEverson SB6 Dec '11 - 0:54 
Yes, of course I can. Sorry for the delay to look at your touch.
Help me out. how can I send the code? By e-mail it? Must be at CodeProject?
Generalusing client sidememberAli Al Omairi(Abu AlHassan)16 Apr '11 - 23:02 
using a hidden button as a target control to the extender is a great idea.
but why not to use client side show/hide functions; that makes more user satisfaction if you just call them as;
 
$find('extenderclientid').show(); and $find('extenderclientid').hide();
 
100 Rose | [Rose]
Help people,so poeple can help you.

Generaldon't work whith new AjaxControlToolkit.dll Version=3.5.40412.0membermitz1 Nov '10 - 10:20 
Uour controll don't work whith new assembly of AjaxControlToolkit.dll Version=3.5.40412.0
need rebuild CSharp_MessageBox.dll whith new asembly Sigh | :sigh:
upyachka watches you

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Sep 2009
Article Copyright 2009 by Yavuz Küçükpetek
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid