title: dynamic load user control and javascript
body: <p>I need to load user control dynamically in my page. I have a gridview with check box inside user control.</p>
  
  <p><img src="https://i.stack.imgur.com/6Avf1.png" alt="enter image description here"></p>
  
  <p>The user control will load dynamically based on the tab that is clicked.</p>
  
  <p>User is allowed to select all check boxes when they click the top-most check box.</p>
  
  <p>I am using javascript to perform this function.</p>
  
  <p>The javascript only works when I click the contact person, but when I click other tab.The javascript is doesn't work.</p>
  
  <p>I opened view source to check. There is only JavaScript for contact person.</p>
  
  <p>I can't see any javascript for sales order and delivery order. </p>
  
  <p>In addition, I embed javascript in each difference user control
  Here is my javascript</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
      function SelectAllCheckboxes1(chk) {
          $('#&lt;%=gvSaleOrder.ClientID%&gt;').find("input:checkbox").each(function () {
              if (this != chk) {
                  this.checked = chk.checked;
              }
          });
      }    
      &lt;/script&gt;
  
  
  
  
  &lt;asp:GridView ID="gvSaleOrder" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                  CellPadding="4" CssClass="rightColumnModule2Table" DataKeyNames="SALES_ID,SALES_ORDER_CODE,STATUS"
                                  ForeColor="#333333" GridLines="None" OnRowCommand="gvSaleOrder_RowCommand"
                                  OnRowDataBound="gvSaleOrder_RowDataBound"&gt;
                                  &lt;AlternatingRowStyle CssClass="gvRowAlternative" /&gt;
                                  &lt;Columns&gt;
                                  &lt;asp:TemplateField&gt;
                                          &lt;HeaderTemplate&gt;
                                              &lt;asp:CheckBox ID="chkSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);"/&gt;
                                          &lt;/HeaderTemplate&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:CheckBox ID="chkSelect" runat="server" /&gt;
                                          &lt;/ItemTemplate&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" /&gt;
                                      &lt;/asp:TemplateField&gt;
  
                                    &lt;asp:TemplateField HeaderText="Sales Order Number"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:LinkButton ID="lnkOrderNumber" runat="server" CommandArgument='&lt;%#Eval("SALES_ORDER_CODE") %&gt;'
                                                  CommandName="View" Text='&lt;%#Eval("SALES_ORDER_CODE") %&gt;' &gt;&lt;/asp:LinkButton&gt;
                                          &lt;/ItemTemplate&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" /&gt;
                                      &lt;/asp:TemplateField&gt;
  
                                       &lt;asp:TemplateField HeaderText="Date"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:Label ID="lblSalesOrderDate" runat="server" Text='&lt;%#Eval("SALES_ORDER_DATE") %&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" /&gt;
                                      &lt;/asp:TemplateField&gt;
  
  
                                      &lt;asp:BoundField DataField="ACCOUNT_OWNER" HeaderText="Customer Name" ReadOnly="True"
                                          SortExpression="ACCOUNT_OWNER"&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" ForeColor="Black" /&gt;
                                      &lt;/asp:BoundField&gt;
  
                                           &lt;asp:TemplateField HeaderText="Total Amount"&gt;
                                          &lt;ItemTemplate&gt;
                                            &lt;asp:Label ID="lblCurrency" runat="server" Text='&lt;%#Eval("CURRENCY_NAME") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;asp:Label ID="lblTotalAmount" runat="server" Text='&lt;%#Eval("TOTAL_AMOUNT") %&gt;'&gt;&lt;/asp:Label&gt;
                                          &lt;/ItemTemplate&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" /&gt;
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:TemplateField HeaderText="Status"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;asp:Label ID="lblStatus" runat="server" Text='&lt;%#Eval("STATUS") %&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" /&gt;
                                      &lt;/asp:TemplateField&gt;
  
                                    &lt;asp:TemplateField HeaderText="Action"&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;table width="100%"&gt;
                                                  &lt;tr&gt;
                                                      &lt;td &gt;
                                                          &lt;asp:LinkButton ID="linkEdit" runat="server"  CommandArgument='&lt;%#Eval("SALES_ORDER_CODE") %&gt;'
                                                              CommandName="Edits"&gt;Edit&lt;/asp:LinkButton&gt;
                                                                  &amp;nbsp;
                                                                &lt;asp:LinkButton ID="linkDelete" runat="server" CommandArgument='&lt;%#Eval("SALES_ORDER_CODE") %&gt;'
                                                              CommandName="Deletes" OnClientClick="javascript:return confirm('Confirm Delete?');"&gt;Delete&lt;/asp:LinkButton&gt;
                                                      &lt;/td&gt;
  
                                                  &lt;/tr&gt;
                                              &lt;/table&gt;
                                          &lt;/ItemTemplate&gt;
                                          &lt;ItemStyle CssClass="gvRow1C" /&gt;
                                      &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>My back end</p>
  
  <pre><code> private void loadControl()
   {
  
  
       if(  (string)Session[subTaskURL] != "")
       {
           Control control = this.LoadControl(Session[subTaskURL].ToString());
           if (control != null)
           {
               // Gives the control a unique ID. It is important to ensure
               // the page working properly. So use control.GetType().Name
               // as the ID.
               control.ID = control.GetType().Name;
               phSubContent.Controls.Add(control);
           }
       }
   }
  
  //--Ensure all control will be dispose--
   public override void Dispose()
   {
  
       for (int i = 0; i &lt; phSubContent.Controls.Count; i++)
       {
           Control ctrl = phSubContent.Controls[i];
           if (ctrl != null)
               ctrl.Dispose();
       }
       base.Dispose();
   }
   //**Ensure all control will be dispose**
  
  protected override void OnInit(EventArgs e)
  {
      base.OnInit(e);
      //--Run on first time .Get default menu for the item--
      if (Session["subTaskID"] == null)
      {
          selectTopSubMenu();
  
      }
      adjustAccessLevelDB();
      loadControl();
  
  }
  </code></pre>
  
  <p><a href="http://www.codeproject.com/Articles/59781/Dynamic-Loading-of-ASP-NET-User-Controls" rel="nofollow noreferrer">Here</a> is the reference that I follow.</p>