Javascript csharp Alert box Code
- Open Alert.cs File which you have created inside the AppCode Folder.You can add the following piece of code in the Alert.cs file
- We need to show Alerts at various Events like incorrect username or
password or compulsory fields are not provided during registration at
that we can show an alert to user for this we have made this common
class.It is a generic class and it can be used in any project and it can
be called from any page without even creating the object as it is
public static.
- Both Alert Class and Show Method are Public and Static
which makes them accessible without creating object in all files of
project
Alert.cs C# Code:
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);
}
}
}
Calling Alert Class from Csharp Code
Alert.Show("Incorrect UserName/Password ");
Here we just have to pass the Message in the form of String and it will show the alert
where we suppose write that last line ?
ReplyDeleteyeah, where we should include the last line? i'm a bit confused...
ReplyDeletepls tell us where to add that last line...
ReplyDelete