Javascript Alert From Asp.Net

When we need javascript actions from asp.net we need to use RegisterClientScriptBlock method.

it is a better practice to make a js actions class and put your common js methods in it.

this little method below alerts from any page when you call the JsAlert Method.

 
JsActions.JsAlert("Hi Serdar ;)");
 
public static class JsActions
{
    public static void JsAlert(string alertText)
    {
        Page p = HttpContext.Current.Handler as Page;
        p.ClientScript.RegisterClientScriptBlock(p.GetType(),
            "myAlert",
            string.Format("<script>alert('{0}');</script>", alertText));
    }
}

dotnetslackers

Be Sociable, Share!

Category: Asp.Net - Comment »


Leave a Reply



Back to top