我正确地在页面中添加了 javascript 吗?
是否有用于这些目的的特殊System.Web.UI.Control
?
代码:
public partial class Site : System.Web.UI.MasterPage
{
protected override void OnLoad(EventArgs e)
{
#if DEBUG
Page.Header.Controls.Add
(new LiteralControl("<script>" +
"alert(\"Hello! Run in [DEBUG] mode!\")");" +
"</script>"));
#else
Page.Header.Controls.Add
(new LiteralControl("<script>" +
"alert(\"Hello! Run in [RELEASE] mode!\")");" +
"</script>"));
#endif
base.OnLoad(e);
}
}
答案 0 :(得分:1)
看看here
您可以使用ClientScriptManager.RegisterClientScriptBlock(Type, String, String)
。
Microsoft Page:
中的示例String scriptText = "";
scriptText += "function DisplayCharCount(){";
scriptText += " spanCounter.innerText = " + " document.forms[0].TextBox1.value.length";
scriptText += "}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "CounterScript", scriptText, true);