Saturday, May 7, 2011

Javascript alert from codebehind

Hi Guys,

I have made a class which will allow to popup a message box easily in web pages.
Just include this class in your application.

"Alert.cs"


using
System.Web;
using System.Text;
using System.Web.UI;

///
/// A JavaScript alert
///

public static class Alert
{

///
/// Shows a client-side JavaScript alert in the browser.
///

/// The message to appear in the alert.
public static void Show(string message)
{
// Cleans the message to allow single quotation marks
string cleanMessage = message.Replace("'", "\\'");
string script = "";

// Gets the executing web page
Page page = HttpContext.Current.CurrentHandler as Page;

// Checks if the handler is a Page and that the script isn't allready on the Page
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
}
}
}

Now just call it from any page by using

Add.Show(Message that you want to print);

eg. Add.Show("Hello");

Hope this will help you and make your work easy...

No comments:

Post a Comment