Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamically create JavaScript alert & confirmation dialogs from server side C#
public static void CreateAlert(string key, string alertString)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
if (!page.ClientScript.IsStartupScriptRegistered(key))
{
page.ClientScript.RegisterStartupScript(typeof(Page), key, "<script language=\"JavaScript\">alert(\"" + alertString + "\");</script>");
}
}
}
public static void CreateConfirm(System.Web.UI.WebControls.Button btn, string confirmString)
{
btn.Attributes.Add("onclick", "return confirm(\"" + confirmString + "\");");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment