如果运行[DEBUG]模式

时间:2015-06-24 07:21:14

标签: c# asp.net web master-pages

我正确地在页面中添加了 javascript 吗? 是否有用于这些目的的特殊System.Web.UI.Control

Site.Mastre.cs中的

代码:

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);
  }
}

1 个答案:

答案 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);