Web Technologies

BLOG for Web Technologies

Freewares, Free E-Books Download, SEO, Tips, Tricks, Tweaks, Latest News, .Net, PHP, ASP, ASP.Net, CSP, MS SQL Server, MySQL, Database
earnptr.com
Saturday, August 23, 2008
JavaScript: Alert.Show(message) from ASP.NET Code-behind

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.

Those kinds of status messages are widely used and are often implemented by a JavaScript alert box on the web page. ASP.NET doesn't natively support JavaScript functions from the code-behind files. You manually have to print out a script tag and add the alert() call to it.

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.

In Windows Forms it is very easy to pop up a status message by calling MessageBox.Show("message"). It is that kind of object model we want in ASP.NET for printing out JavaScript alerts. We want Alert.Show("message") in ASP.NET.

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.

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);
}
}
}

Demonstration

That class of only 30 lines of code enables us to add a JavaScript alert to any page at any time. Here is an example of a Button.Click event handler that uses the method for displaying status messages.

void btnSave_Click(object sender, EventArgs e)
{
try
{
SaveSomething();
Alert.Show("You document has been saved");
}
catch (ReadOnlyException)
{
Alert.Show("You do not have write permission to this file");
}
}

Labels: ,

posted by WebTeks @ 2:29 AM  
1 Comments:
Post a Comment
<< Home
 
Previous Post
Archives
Links
Template by

Free Blogger Templates

BLOGGER

Subscribe in NewsGator Online Subscribe in Rojo Add to Google Add to netvibes Subscribe in Bloglines Web Developement Blogs - BlogCatalog Blog Directory Blogarama - The Blog Directory Blog Directory & Search engine Computers Blogs - Blog Top Sites Top Computers blogs