Monday, February 16, 2009

Displaying a Message Box from ASP.NET

ASP.NET does not have a class or method for displaying a message box. To display a message box one needs to write a javascript that looks something like that:
Response.Write("<script language='javascript'>window.alert('Hellow, World')</script>");

I always keep forgetting what is the exact syntax and how the javascript function is called and I find myself searching the internet or my previous code to find it out.
There are many solutions on the web, most of them sophisticated with lots of features, but I decided to write a super-simple static class that will wrap the previous javascript code with easy to remember and user c# code, and if I still won't remember, the development environment of ASP.NET will be happy to remind me, all I have to remember is "MessageBox" (this even I can remember) and type the magical "dot"...

So here is the class:

public static class MessageBox
{
public static void Show(string msg)
{
String str =
"<script language='javascript'>"
"window.alert('" msg "')</script>";
HttpContext.Current.Response.Write(str);
}
}

No comments:

Post a Comment