gjshah7254
4/20/2016 - 7:43 PM

Add Javascript to any C# code behind file.

Add Javascript to any C# code behind file.

ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('hi!')", true);//no need to use javascript with alert when bool set to true

ScriptManager.RegisterStartupScript(page, page.GetType(), "msg", "alert('hi!')", true);

ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('hi!');</script>", true);

<script runat="server">
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "uniquevalue", "document.write('test');", true);
    }
</script>

private void RegisterScript()
    {
        System.Web.UI.ClientScriptManager cs = Page.ClientScript;
        if (!cs.IsStartupScriptRegistered(this.GetType(), "tag-filter"))
        {
            string script = string.Format(@"<script src=""{0}"" type=""text/javascript""></script>", ResolveClientUrl("~/custom/scripts/tag-filter.js"));
            cs.RegisterStartupScript(this.GetType(), "tag-filter", script, false);
        }
    }