Thursday, 14 April 2011

JavaScript Alert 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. 

As easy as it may be, the extensive use of the alert() status message though out a website calls. 

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

Such a thing doesn’t exist so we have to create it our selves. so this is Basic Idea to display Alerts in Applications Code-behind.

In form :

Take one Button ,
    <asp:Button ID="btnClick" runat="server" Text="Alert" onclick="Button1_Click" />

In code Behind:
protected void Button1_Click(object sender, EventArgs e)
        Response.Write("<script language='javascript'>alert('Hi This Alert from Code brhind :');</script>");



MSDN Click here to see Actual content

No comments:

Post a Comment