In highly interactive websites and intranet sites, you probably want to let the users know what’s going on when they delete, save, export etc. on the site.
As easy as it may be, the extensive use of the alert() status message though out a website calls for a unified and simple implementation in order to avoid duplicate code – a centralized method.
Such a thing doesn’t exist so we have to create it our selves.
I’ve written a static class called Alert with one public method called Show. The implementation is as simple as can be. Just put the .cs file in the App_Code folder on your website and you instantly have access to the method from all pages and user controls.
public static class Alert
/// <param name="message">The message to appear in the alert.</param>
public static void Show(string message)
// Cleans the message to allow single quotation marks
string cleanMessage = message.Replace("'", "\\'");
// 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);
void btnSave_Click(object sender, EventArgs e)
Alert.Show("You document has been saved");
Alert.Show("You do not have write permission to this file");
Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.