Search code examples
javascriptasp.netdynamic-usercontrols

Dynamically added javascript not evaluated in ascx control


I have an ascx control which has a literal placed on it during design time. At run time, in Page_Load, I set the literal's text property to a javascript that I retrieve from a database. However, it appears that the javascript code is not evaluated. What do I need to do in order to get the script evaluated?

In .ascx:

<asp:Literal id="litTags" runat="server"></asp:Literal>

In ascx.cs:

protected void Page_Load(object sender, EventArgs e)
  {
      //Set literal's text to the script
      litTags.Text = "Javascript tag and script from database";
  }

Solution

  • Make sure you wrap that js code in the proper tag:

    <script type="text/javascript">'
    // js code here
    </script>