Click here to Skip to main content
15,887,776 members
Articles / Web Development / ASP.NET

AJAX Enabled MessageBox

Rate me:
Please Sign up or sign in to vote.
4.73/5 (56 votes)
14 Sep 2009CPOL2 min read 200.1K   7.8K   108   57
MessageBox usercontrol that is AJAX ready and has confirmation support

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:

C#
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

C#
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

C#
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:

C#
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)


Written By
Software Developer (Senior) NetCad
Turkey Turkey
Yavuz is a software developer since 2001. He has been working as senior software developer in Netigma Project at NetCad since 2012.



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.

Comments and Discussions

 
AnswerRe: do you have the code in vb? Pin
Yavuz Küçükpetek19-Oct-09 11:42
Yavuz Küçükpetek19-Oct-09 11:42 
GeneralNice Work Pin
Sing Yuen Yip14-Oct-09 4:59
Sing Yuen Yip14-Oct-09 4:59 
QuestionError While using master page Pin
habibsatti14-Oct-09 4:11
habibsatti14-Oct-09 4:11 
AnswerRe: Error While using master page Pin
Yavuz Küçükpetek19-Oct-09 11:33
Yavuz Küçükpetek19-Oct-09 11:33 
GeneralRe: Error While using master page Pin
habibsatti19-Oct-09 18:13
habibsatti19-Oct-09 18:13 
GeneralVery Nice Pin
Paul Conrad12-Oct-09 16:31
professionalPaul Conrad12-Oct-09 16:31 
QuestionUsing the MsgBox utility to a web site project Pin
Ziv Ron6-Oct-09 11:04
Ziv Ron6-Oct-09 11:04 
AnswerRe: Using the MsgBox utility to a web site project Pin
Yavuz Küçükpetek7-Oct-09 4:44
Yavuz Küçükpetek7-Oct-09 4:44 
Hi ronziv,

Here are the steps I hope I do not bypass any Smile | :)

1. Make your web site project AJAX enabled
2. Reference the AjaxControlToolkit(dll) library.
3. Organize your web.config for controltoolkit as
<compilation debug="true">
<assemblies>
.......
<add assembly="Pamukkale.WebControls" />
</assemblies>
</compilation>

<pages>
<controls>
<add tagPrefix="AjaxControls" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/> </controls>
</pages>

add the underlined lines to the appropriate config sections.

4. Add a usercontrol to your project and name it for example uscMessageBox. And copy and paste the aspx and cs code from the messagebox control in project which is attached to the article.
PS: Be carefull not to break down the original usercontrol that you added. I mean through the copy paste process do not copy and paste all the page of code (aspx and the cs parts). Copy and paste part by part Smile | :)

5. Drag and drop the control to the page that you want to give messages.
GeneralRe: Using the MsgBox utility to a web site project Pin
Ziv Ron8-Oct-09 1:44
Ziv Ron8-Oct-09 1:44 
Generalcool work... Pin
Merih Yurtseveroglu5-Oct-09 19:40
Merih Yurtseveroglu5-Oct-09 19:40 
AnswerYashiasan sani Pin
hamzehlou.mehdi21-Sep-09 23:32
hamzehlou.mehdi21-Sep-09 23:32 
GeneralNice article, but... Pin
Dinesh Mani16-Sep-09 21:16
Dinesh Mani16-Sep-09 21:16 
GeneralRe: Nice article, but... Pin
Yavuz Küçükpetek16-Sep-09 22:06
Yavuz Küçükpetek16-Sep-09 22:06 
GeneralRe: Nice article, but... Pin
Dinesh Mani16-Sep-09 23:04
Dinesh Mani16-Sep-09 23:04 
GeneralSuper.... Pin
Jitku16-Sep-09 11:50
Jitku16-Sep-09 11:50 
QuestionWhy not using simple javascript like: window.alert, window.confirm? Pin
DrABELL16-Sep-09 11:19
DrABELL16-Sep-09 11:19 
AnswerRe: Why not using simple javascript like: window.alert, window.confirm? Pin
Yavuz Küçükpetek16-Sep-09 20:25
Yavuz Küçükpetek16-Sep-09 20:25 
GeneralRe: Why not using simple javascript like: window.alert, window.confirm? Pin
DrABELL17-Sep-09 6:58
DrABELL17-Sep-09 6:58 
GeneralGood Job Pin
Yildirim Kocdag15-Sep-09 21:44
Yildirim Kocdag15-Sep-09 21:44 
GeneralVery good Pin
samerh15-Sep-09 19:29
samerh15-Sep-09 19:29 
GeneralNice Pin
mambo_215-Sep-09 5:24
mambo_215-Sep-09 5:24 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.