title: Telerik Radgrid dynamically added controls disappear after postback
body: <p>I have the feeling this is a rookie problem, but I must be missing something.</p>
  
  <p>I have a <strong>Telerik Radgrid</strong>. In one of the columns I add some controls, during the <code>ItemDataBound</code> event. These controls function as checkboxes of sort, so I change some properties using Javascript when people click them.
  <br />
  When it comes to saving everything, the user clicks a button, which causes a postback. <br />
  Now, after the postback, my dynamically created controls are gone. <br />
  How can I retrieve their values to save them?</p>
  
  <p>Cheers,
  CJ</p>
  
  <p><strong>Edit:</strong> <br /></p>
  
  <p>In the ItemCreated event I have this:</p>
  
  <pre><code>If (TypeOf (e.Item) Is GridDataItem) Then
  Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
  Dim diCell As TableCell = dataItem("Periode")
  
  Dim tbl As Table = CType(diCell.FindControl("tblPeriode"), Table)
  
  Dim cell As TableCell
  Dim sdsPerioden As New SqlDataSource()
  sdsPerioden.ConnectionString = cs
  sdsPerioden.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
  sdsPerioden.SelectCommand = "OPLEIDING_PERIODE_JAAR_SELECT_BY_OPLEIDING_ID"
  sdsPerioden.SelectParameters.Add(New Parameter("INT_OPLEIDING_ID", DbType.Int32, Session("OPLEIDING_ID")))
  Dim dv As DataView = CType(sdsPerioden.Select(DataSourceSelectArguments.Empty), DataView)
  
  Dim iJaar As Integer = 0
  sJaren = New List(Of String)
  
  For Each row As DataRow In dv.Table.Rows
      cell = New TableCell()
      cell.BorderStyle = BorderStyle.None
      cell.Style("margin-right") = "5px"
      cell.Style("padding") = "0px"
      cell.Width = Unit.Pixel((row("PERIODEN_PER_JAAR") * (15 + 2)) + ((row("PERIODEN_PER_JAAR") / 2) * 3) + 1)
      For i As Integer = 1 To row("PERIODEN_PER_JAAR")
          Dim div As HtmlGenericControl = New HtmlGenericControl("div")
          dv.ID = "div" + iJaar.ToString().PadLeft(2, "0") + iPeriode.ToString().PadLeft(2, "0")
          dv.Style.Add("height", "15px")
          dv.Style.Add("width", "15px")
          dv.Style.Add("display", "inline-block")
          dv.Style.Add("border-style", "solid")
          dv.Style.Add("border-width", "1px")
          dv.Style.Add("border-color", sKleur)
          dv.Style.Add("background-color", "#FFFFFF")
          dv.Attributes("IS_CHECKED") = "0"
          dv.Attributes("class") = "periodeCheck"
          cell.Controls.Add(div)
      Next
      tbl.Rows(0).Cells.Add(cell)
  Next
  End If
  </code></pre>
  
  <p>The Javascript that responds to clicks on the divs:</p>
  
  <pre><code>$(".periodeCheck").click(function () {               
      if ($(this).attr('IS_CHECKED') == "0")
      {
          var color = $(this).css('border-left-color');
          $(this).attr('IS_CHECKED', '1');            
          $(this).css('border-style', 'none');
          $(this).css('background-color', color);                     
      }
      else
      {
          var color = $(this).css('background-color');
          $(this).attr('IS_CHECKED', '0');
          $(this).css('border-style', 'solid');
          $(this).css('border-width', '1px');
          $(this).css('border-color', color);
          $(this).css('background-color', "#FFFFFF");
  
      }
      return false;
  });
  </code></pre>
  
  <p>Now when I click on my Save button, I notice that the ItemCreated event fires first, then the Button_Click event. <br />
  In the Button_Click Event I have this:</p>
  
  <pre><code>For i = 0 To rgOpleidingLeeritem.MasterTableView.Items.Count - 1
      Dim tbl As Table = CType(rgOpleidingLeeritem.MasterTableView.Items(i).FindControl("tblPeriode"), Table)
      Dim div As HtmlGenericControl
      For Each cell As TableCell In tbl.Rows(0).Cells
          For Each ctl As Control In cell.Controls
              div = CType(ctl, HtmlGenericControl)
              If (div.Attributes("IS_CHECKED") = "1") Then
                  'Do something
              End If          
          Next
      Next
  Next
  </code></pre>
  
  <p>The 'Do something part is never reached, no matter how many divs I 'check' <br />
  According to the element inspector the divs do have the property 'IS_CHECKED' set to '1' after being clicked on.</p>