How to dynamically not render a script tag?
.net, asp.net, c#, html, javascript
Solution
You can use ClientScriptManager.RegisterClientScriptBlock or ClientScriptManager.RegisterStartupScript to conditionally add the script.
if (condition)
{
String csname2 = "ButtonClickScript";
Type cstype = this.GetType();
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}
If you have script that is not small then you can put the script in some js file and use ClientScriptManager.RegisterClientScriptInclude
if (condition)
{
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptInclude("ScriptKey", "ScriptURLToInclude");
}
Problem
I want to be able to dynamically (on page load) decide to set it to `Visible=false` so it won't be rendered. I tried `runat=server` but that's only for virtual paths.