title: Pre-Select Items in a dynamically populated CheckBoxList
body: <p>I've been experimenting with this and trying to understand the Page Life Cycle for hours upon hours. I'm completely stumped at this point :'( Any help would be GREATLY appreciated.</p>
  
  <p>Below are my methods. My <code>_PrefillWizard</code> method actually works for all the controls on my page except for those which are dynamically populated. The <code>CheckBoxList</code>s are such because they are automatically generated based on selectable values in a table in my DB. I already have this control in my markup like so... how do I force this to load and be ready before running my other code?</p>
  
  <pre><code>&lt;asp:CheckBoxList ID="MyLanguagesCBL" runat="server" RepeatDirection="Vertical"
      RepeatColumns="4" Width="100%" DataSourceID="LanguagesSDS"
      DataTextField="Language" DataValueField="ID"&gt;
  &lt;/asp:CheckBoxList&gt;
  &lt;asp:SqlDataSource ID="LanguagesSDS" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:ConnectionString1 %&gt;"
      SelectCommand="select ID, [Language] from LanguagesListTable"
      DataSourceMode="DataReader"&gt;
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>Obviously these particular controls are not yet generated or on the page by the time my <code>_PrefillWizard</code> has been called, resulting in none of the CheckBoxes being pre-filled. <strong>How do I make them generate before my prefill method is called?</strong> Thanks ;)</p>
  
  <p>The click event that kicks things off.</p>
  
  <pre><code>'On "Load" Button click, clears a Wizard control and Prefills it with the
  '  data from the clicked record based on the primary key stored in SessionState.
  Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand
      If e.CommandName = "Select" Then
          Session.Add("ClickedPrimaryKey", GridView2.DataKeys(e.CommandArgument).Value.ToString)
          'This is the main function which calls "_SetCheckBoxes" among others.
          _PrefillWizard(Session.Item("ClickedPrimaryKey"))
      End If
  End Sub
  </code></pre>
  
  <p>Here's my function for populating the my <code>CheckBoxList</code> control, but it obviously depends on the control having already been loaded on the page.</p>
  
  <pre><code>'Parse a CSV String from the Database back into CheckBoxList Selections
  Protected Sub _SetCheckBoxes(ByRef cbl As CheckBoxList, ByRef dt As DataTable, ByVal row As Integer, ByVal csv As String)
      'Do something if there is a value in the cell
      If Not IsDBNull(dt.Rows.Find(row)(csv)) Then
          For Each item As ListItem In cbl.Items
              item.Selected = False 'Reset the checkbox first
              'Then, if the ID value of the checkbox corresponds to a value 
              '  in the CSV string, then tick the checkbox.
              If csv.Contains(item.Value) Then
                  item.Selected = True
              End If
          Next
      End If
  End Sub
  </code></pre>