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   
General[My vote of 1] MessageBox Appearancemembergerhard4512 Feb '10 - 2:59 
Hi,
 
the Yabu MessageBox in my project has a black background, making the msg text unreadable. The OK button is left aligned.
QuestionHave you got the VB.NET version??memberMember 47744552 Feb '10 - 12:08 
Hi,
have you the version of this in Visual Basic?
Please, this will be help me a lot
Regards,
 

Jorge
AnswerRe: Have you got the VB.NET version??membernono3142 Feb '10 - 22:14 
You can try the joined zip of this post: http://www.dotnetgurus.net/post/2009/09/15/ASPNet-MessageBox-with-AJAX.aspx[^]
 
To achieve the same alone, you can use a code converter, like http://www.developerfusion.com/tools/convert/csharp-to-vb/[^]
QuestionDo you plan to release a server control version ?membernono3142 Feb '10 - 3:02 
Hi,
It's a beautiful job at this point. I tried to use it as base for a msgbox server control, but didn't success. Do you plan to release one (no ascx, png embedded and over-ridable, ...) ?
AnswerRe: Do you plan to release a server control version ?memberAli Al Omairi(Abu AlHassan)16 Apr '11 - 22:50 
Do you know what this is the first question i asked before a year when i begin learning ASP.NET.
But Now I have got the answer. I just submited an artcle with the name 'Confirm Message Box' that i guess it contains an answer to your question.
 
100 Rose | [Rose]
Help people,so poeple can help you.

QuestionDrag&drop uscMsgBoxmembergerhard4527 Jan '10 - 1:00 
Hi Yavuz,
 
Also thanks for the good work!!!
 
I am also new (3 days) to ASP.NET and did the 4 steps out of 5 you described in your answer to ronziv.
 
But I am not sure about step 5:
5. Drag and drop the control to the page that you want to give messages.
 
Could explain, which file to drag (uscMessageBox.ascx ?) and where to drop it?
 
Thanks
 
Gerhard
QuestionNot work well with Firefox?membervunc26 Nov '09 - 22:29 
Dear Yavuz,
I can use you usercontrol but firefox is not display it well.
It appears 2 modalpopups (1 in the left for a few seconds, before it go to the center of the screen). I think you can use div position in css (-1000) for a hidden layer.
Thanks for a very good control.
AnswerRe: Not work well with Firefox?memberYavuz Kucukpetek10 Dec '09 - 10:42 
Sorry, could not understand the problem and tried it in firefox but again could not understand see any problem again Smile | :)
 
Thank you very much for your advice, I am sure it will help somebody facing this strange problem.
GeneralVB Project doesn't work ShowConfirmationmemberbrunovianna6 Nov '09 - 7:09 
Hi Man! Good Job, the component is nice. But!!
 
A version in VB.Net converted, doesn't work a ShowConfirmation. The button Cancel don't return a enum with Cancel information.
 
Do you can check this, please. I need so much this.
 
Thank You!!!
Bruno Vianna
 
ps: Sorry for poor english, i am brazilian and my english level is basic. Thumbs Up | :thumbsup:
GeneralRe: VB Project doesn't work ShowConfirmationmemberYavuz Kucukpetek10 Dec '09 - 10:37 
Hi brunovianna,
 
Sorry I did not get a notification email about the new messages.
 
I have checked the vb.net code and the problem is in the constructor method of the MsgBoxEventArgs class.
File uscMsgBox.ascx.vb
Lines 20-24 :
 
Public Sub New(ByVal answer As enmAnswer, ByVal args As String)
Answer = Answer
args = args
End Sub
 
this should be as
 
Public Sub New(ByVal answer_ As enmAnswer, ByVal args_ As String)
Answer = answer_
Args = args_
End Sub
 
As you know c# is case sensitive but vb.net is not. And this problem is caused during the conversion process from c# to vb.net
 

Thank you for your feedback.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.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