Skip to content

Instantly share code, notes, and snippets.

@aromig
Created January 25, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aromig/255ab89b9f3b65d92b89 to your computer and use it in GitHub Desktop.
Save aromig/255ab89b9f3b65d92b89 to your computer and use it in GitHub Desktop.
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