title: How to persist data in asp repeater on postback
body: <p>Below is the markup page:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Master/MainPage.master" EnableViewState="true" AutoEventWireup="true" CodeBehind="edit.aspx.cs" Inherits="Website.View.edit" %&gt;
  
  ...
  
   &lt;asp:Repeater runat="server" ID="rptSample"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:HyperLink runat="server" ID="refLink" href='http://www.sample.com' Text='Test data' /&gt;
                  &lt;asp:CheckBox runat="server" ID="chkbxDelete" Text="Delete" /&gt;
              &lt;/ItemTemplate&gt;
              &lt;SeparatorTemplate&gt;
                  &lt;br /&gt;
              &lt;/SeparatorTemplate&gt;
    &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Here's how I bind data on code behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
              rptSample.DataSource = getData();
              rptSample.DataBind();
  }
  </code></pre>
  
  <p>I've also tried this one:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
         if(!IsPostBack)
          {
              rptSample.DataSource = getData();
              rptSample.DataBind();
          }
  }
  </code></pre>
  
  <p>And tried to get the data on btnSave_Click function</p>
  
  <pre><code>protected void btn_save_Click(object sender, EventArgs e) 
  {
    foreach(RepeaterItem item in rptSample.Items) 
    {
      CheckBox chkbx = (CheckBox) item.FindControl("chkbxDelete");
      if (chkbx.Checked) 
      {
          //Do something
      }
    }
  }
  </code></pre>
  
  <p>If I didn't add the <code>!IsPostBack</code>, the <code>rptSample.Items</code> will be empty but if I remove it, the checkbox will always be false.</p>
  
  <p>What's the problem??</p>
  
  <hr>
  
  <p>Edit:
  As per a user requested, here's the full page load function:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
        bindData();
  }
  
  protected void bindData()
  {
      List&lt;sp_SelectAttachments_Result&gt; attachments = DAL.SelectAttachmentsByID(Request["ID"]);
      if (attachments.Count == 0)
          divAttachments.Visible = false;
      else
      {
          divAttachments.Visible = true;
          rptAttachments.DataSource = attachments;
          rptAttachments.DataBind();
      }
  }
  </code></pre>
  
  <p>Here's the markup page for divAttachments and rptAttachments</p>
  
  <pre><code>&lt;div runat="server" id="divAttachments" visible="false"&gt;
          &lt;asp:Repeater runat="server" ID="rptAttachments"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:HiddenField Value='&lt;%# Eval("ID") %&gt;' runat="server" ID="hidID" /&gt;
                  &lt;asp:HyperLink runat="server" ID="refLink" href='&lt;%# $"/Utils/getFile.ashx?ID={ID}" %&gt;' Text='&lt;%# ((sp_SelectAttachments_Result)Container.DataItem).FileName %&gt;' /&gt;
                  &lt;asp:CheckBox runat="server" ID="chkbxDeleteAttachment" Text="Delete" /&gt;
              &lt;/ItemTemplate&gt;
              &lt;SeparatorTemplate&gt;
                  &lt;br /&gt;
              &lt;/SeparatorTemplate&gt;
          &lt;/asp:Repeater&gt;
      &lt;/div&gt;
  </code></pre>