Search code examples
c#asp.netwebmaster-pages

Generate javascript if run [DEBUG] mode


Correctly I add javascript to the page? Is there a special System.Web.UI.Control for these purposes?

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

Solution

  • Take a look here

    You can use ClientScriptManager.RegisterClientScriptBlock(Type, String, String) for this.

    The example from the Microsoft Page:

    String scriptText = "";
    scriptText += "function DisplayCharCount(){";
    scriptText += "   spanCounter.innerText = " + " document.forms[0].TextBox1.value.length";
    scriptText += "}";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "CounterScript", scriptText, true);