title: dynamic Javascript in asp.net
body: <p>well. The problem i'm facing is i have my own custom control and i do a query , get records and dynamically add html controls to the page based on the data.</p>
  
  <p>Now there's the problem of adding some dynamic javascript</p>
  
  <p>I do this with the help of literal controls. </p>
  
  <pre><code>&lt;asp:Literal runat="server" ID="latEventToolTipJqueryScripts"&gt;&lt;/asp:Literal&gt;
  </code></pre>
  
  <p>This works like a charm</p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript"&gt;
  // &lt;![CDATA[
      Sys.Application.add_load(WireEvents_&lt;%=this.ID%&gt;); // fix wiring for .NET ajax updatepanel
      $(WireEvents_&lt;%=this.ID%&gt;); // handle page load wiring
  
      function WireEvents_&lt;%=this.ID%&gt;() {
          &lt;asp:Literal runat="server" ID="latEventToolTipJqueryScripts"&gt;&lt;/asp:Literal&gt;
      }
  
  // ]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>I add the literal text dynamically from code behind.</p>
  
  <p><strong>However, when placing the control in an updatepanel, the postbacks don't update the script.</strong> </p>
  
  <p>EDIT: The <code>Sys.Application.add_load</code> rewires the necessary functions just fine with the updatepanel. The problem is that the script that needs to be in place of the literal, doesn't update when in an updatepanel.</p>
  
  <p>I've tried the ClientScript.RegisterStartupScript but it has the same effect as with the literal control trick. Any help?</p>
  
  <p><strong>---------------------------SOLVED (tnx to Pranay Rana)----------------------------------</strong></p>
  
  <p>Got rid of the literal in the ascx side. as well as the <code>Sys.Application.add_load</code></p>
  
  <p>now it's only in the code behind. The thing that was throwing me off was the JQuery thing.</p>
  
  <pre><code>this.strBuilderJS.Append( "&lt;script language='javascript' type='text/javascript'&gt;" +
                                  "$(WireEvents_" + this.ID + ");" + 
                                  "function WireEvents_" + this.ID + "(){"+
                                  "    alert('stuff');");
  
  this.strBuilderJS.Append(       "}&lt;/script&gt;");
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>ScriptManager.RegisterStartupScript(this, this.GetType(), "strBuilderJS", strBuilderJS.ToString(), false);
  </code></pre>