title: How do i get access to checkboxes dynamically generated in loop
body: <p>I am new to asp.net and vb.net programming and i can't find a answer to my problem. I have dynamically generated checkboxes in a loop at runtime within a sub. </p>
  
  <p>This is a grid scheduler program that displays selected day's and selected hours from a location which is selected from a different page. I want to acces the checkboxes by id but i cant get acces to them because the checkboxes are not declared at class level.</p>
  
  <p>Can anyone help me please, i have searched all day long for a solution. I Prefer VB but C# is fine also.</p>
  
  <p>Below is my codebehind</p>
  
  <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not IsPostBack Then
        BindLocationDayTime()
     End If
  End Sub
  
  Public Sub BindLocationDayTime()
          Dim ID As Integer
          Dim Name As String
          Dim Day As Integer
          Dim Time As Integer
          Dim StartDate As DateTime
          Dim EndDate As DateTime
  
          Dim Locations As SqlDataReader = GetLocations()
  
          For Each Item In Locations
  
              Dim LRow As New TableRow()
              Dim LCell As New TableCell()
              LCell.Text = Locations.Item("Name")
              LCell.Attributes.Add("class", "LocationHeader")
              LCell.Attributes.Add("colspan", "5")
              LRow.Cells.Add(LCell)
              LocationData.Rows.Add(LRow)
  
              Dim Location As SqlDataReader = GetLocation(Convert.ToInt32(Locations.Item("Id")))
  
              While Location.Read()
                  Name = Location("Name").ToString()
                  StartDate = Location("StartDate")
                  EndDate = Location("EndDate")
              End While
  
              Dim dtfi As Globalization.DateTimeFormatInfo = Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
              Dim tRowCount As Integer = 0
  
              Do While StartDate &lt;= EndDate
                  Dim LocationDayTime As SqlDataReader = GetPlayDayTime(Convert.ToInt32(Locations.Item("Id")))
  
                  For Each row In LocationDayTime
                      Day = LocationDayTime.Item("DayID")
                      Time = LocationDayTime.Item("TimeID")
                      ID = Locations.Item("Id")
                      If Day = 1 Then
                          If StartDate.DayOfWeek = DayOfWeek.Monday Then
                              BindDays(StartDate, ID, tRowCount, Time)
                              tRowCount = tRowCount + 1
                          End If
                      ElseIf Day = 2 Then
                          If StartDate.DayOfWeek = DayOfWeek.Tuesday Then
                              BindDays(StartDate, ID, tRowCount, Time)
                              tRowCount = tRowCount + 1
                          End If
                      ElseIf Day = 3 Then
                          If StartDate.DayOfWeek = DayOfWeek.Wednesday Then
                              BindDays(StartDate, ID, tRowCount, Time)
                              tRowCount = tRowCount + 1
                          End If
                      ElseIf Day = 4 Then
                          If StartDate.DayOfWeek = DayOfWeek.Thursday Then
                              BindDays(StartDate, ID, tRowCount, Time)
                              tRowCount = tRowCount + 1
                          End If
                      ElseIf Day = 5 Then
                          If StartDate.DayOfWeek = DayOfWeek.Friday Then
                              BindDays(StartDate, ID, tRowCount, Time)
                              tRowCount = tRowCount + 1
                          End If
                      End If
                  Next
                  StartDate = StartDate.AddDays(1)
              Loop
          Next
      End Sub
  
  Public Sub BindDays(ByVal StartDate As DateTime, ByVal ID As Integer, ByVal tRowCount As Integer, ByVal Time As Integer)
          Dim dtfi As Globalization.DateTimeFormatInfo = Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
          Dim tRow As New TableRow()
  
          Dim Cell1 As New TableCell()
          Dim strDayOfWeek As String = dtfi.GetDayName(StartDate.DayOfWeek)
          Cell1.Text = UppercaseFirstLetter(strDayOfWeek &amp; " ") &amp; (StartDate.Date.ToShortDateString &amp; " om ") &amp; (Time &amp; "uur ")
          Cell1.Attributes.Add("class", "MemberCell")
          tRow.Cells.Add(Cell1)
  
          Dim Cell2 As New TableCell()
          Dim cbAvailible As New CheckBox()
          cbAvailible.ID = (StartDate.Date) &amp; "," &amp; (Time)
          cbAvailible.Checked = False
          Cell2.Controls.Add(cbAvailible)
          tRow.Cells.Add(Cell2)
  
          Dim Cell3 As New TableCell()
          Dim Label As New Label()
          Label.Text = ("(Op deze datum ben ik verhinderd)")
          Cell3.Controls.Add(Label)
          tRow.Cells.Add(Cell3)
  
          If tRowCount Mod 2 Then
              tRow.Attributes.Add("class", "alternatingItemStyle")
          Else
              tRow.Attributes.Add("class", "itemStyle")
          End If
  
          LocationData.Rows.Add(tRow)
  
      End Sub
  
  #End Region
  
  #Region " Events "
  
  Private Sub Insert_Click(sender As Object, e As EventArgs) Handles Insert.Click
  ' I want to get here al the checkbox id and insert the values to a databse
  End Sub
  
  End Region
  </code></pre>