Sunday, October 26, 2008

Windows Forms like Messagebox in web page

I have often come accross web pages where web programmers resent they miss the MessageBox that is available and highly used in windows programming. And for sure I am no exception. While searching for MessageBox functionality in web page, I have found one which is quite simple and useful.

Note the MessageBox function in the code snippet below. It takes a string as a parameter and pops it up in a messagebox window using Label control and javascript. For illustration of it's use, I have added a textbox with ID txtName and a button with ID btnOK. User enters his/her name in the textbox and clicks ok. If the user input contains special characters &, _ or @, the messagebox will be prompted with the message that name can not contain such charaters. If not, user's name will be prompted using same message box.
Fig: Displaying windows form like MessgeBox in web page

Here goes the MesageBox function in C#, which we can call anytime. It takes the message text (string) as input and displays as in the figure above. The only trick involved is:
1. Create a new label from code behind
2.Initialize the text of the lable to a little javascript code [which is a call to javascript's alert function].
3. Add this label to to the page's control bag.

That's all. Now lets look into the function itself.

//the message box function
private void MessageBox(string msg)
Label lbl = new Label();

lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";


Now lets go with an example. We will have a form in which there is one textbox and one button. We will message user when undesired special character is detected in the textbox. Lets have the form markup like this [within the form].


<form id="form1" runat="server">


<asp:Label ID="Label1" runat="server" Text="Your Name:"></asp:Label>

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="OK" onclick="Button1_Click" />




Now is the time for the event handler on button click.

//invoked when button btnOK is clicked
protected void Button1_Click(object sender, EventArgs e)
string name = txtName.Text;
if (!ValidateString(name))
MessageBox("You can not put special characters _, & or @ in your name.");
MessageBox("Your name is" + " " + name);

This is just another function for test purpose only, that validates the characters in the textbox.

//returns true if the string contains _,& or @
//otherwise returns false
public bool ValidateString(string str)
if((str.Contains("_") || (str.Contains("&"))||(str.Contains("@"))))
return false;
return true;

This MessageBox function, although a simple one can be extensively used throughout the pages, using MasterPage concept or base page concept or anything else that propagates inheritance. The only limitation is that for this method to work javascript must enabled in the browser (and you know this is the issue in all pages that use javascript). Further, for the function ValidateString, which currently checks only three special characters, you can extend it to include other special charactes also.

Your creative suggestions are always welcome! Happy programming!
kick it on


Sagnik said...

not working within AJAX Update Panel.

Anonymous said...

I liked your clever solution however it will not work if your application uses the Server.HttpEncode() method and may not work with AJAX enabled websites.

The Server.HttpEncode() method strips any special HTML characters (like angled <> brakets) from any content being sent to the browser that should be text and converts it into it's ASCII equivalent. This method is used so that any potentially harmful scripts or other HTML content is harmlessly displayed as text instead of interpreted & executed as code by the browser.

So, if the website you're developing is using the Server.HtmEncode() method to provide added security the JavaScript code that you're supplying to Text property for a Label will not be executed and will instead be displayed as pure text.

There are much better solutions to supplying dynamically generated code to the server.

You could use methods provided by the Page.ClientScript property to register JavaScript with the page (more information can be found:

Or if you're developing an Ajax Enabled website you should register your dynamic JavaScript with the ScriptManager (more information can be found: you can register your script using the ScriptManager Proxy (use MSDN to look it up).

Anonymous said...

This approach is not working when the button is placed in update panel control

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles