When I began the programming for http://www.sceptermarketingtechnologies.com, I quickly noticed that Asp.net does not support the MessageBox class that I was familiar with in windows forms.
Since Asp.net runs on the server it doesn't have access to produce a modal dialog box on the client. However, by using a scripting language that runs in the clients browser a similar construct is available. In javascript the window.alert() function will produce a modal dialog similar to what you would expect from MessageBox in windows forms.
So, I decided to create a static class MessageBox
with a static method Show(), which will give you the ablility to to
still use the syntax MessageBox.Show("Your Message"); and then convert it
into the javascript window.alert("Your Message"); and add the script to the end of the current response stream.
I've written a how to on my blog at: http://sceptermt.blogspot.com/2008/02/how-to-create-message-box-class-in-c.html that shows the source code that I used in C# for my web application and details on how it works.
You
could also write overloads for the static show() method to allow the
class to do a javascript confirm() or a prompt(). I haven't needed that
functionality yet so I've just kept it simple.
Loading
Bechir BejaouiPosted Mar 22, 2008, 6:06 AM
// for postback events
public static void ShowMessageBox(string _message)
{
Page page = HttpContext.Current.Handler as Page;
page.RegisterStartupScript("key2", "");
}
// for async postback events
public static void ShowMessageBoxForAsync(string _message)
{
Page page = HttpContext.Current.Handler as Page;
ScriptManager.RegisterClientScriptBlock(page,page.GetType(), "key2", "alert('" + _message + "');", true);
}
// Then you can call this in, let's say , button_click event handler like this ( i assume that your class name is Utilities without any namespace definition) :
protected void myButton_Click(object sender, EventArgs e)
{
Utilities.ShowMessageBox("you entered a wrong password");
}
It achives the same goal