title: How to programmatically create and use a list of checkboxes from ASP.NET?
body: <p>I have a page with a table of stuff and I need to allow the user to select rows to process. I've figured out how to add a column of check boxes to the table but I can't seem to figure out how to test if they are checked when the form is submitted. If they were static elements, I'd be able to just check do <code>this.theCheckBox</code> but they are programaticly generated.</p>
  
  <p>Also I'm not very happy with how I'm attaching my data to them (by stuffing it in there ID property).</p>
  
  <p>I'm not sure if it's relevant but I'm looking at a bit of a catch-22 as I need to known which of the checkboxes that were created <em>last time around</em> were checked before I can re-run the code that created them.</p>
  
  <hr>
  
  <p><em>Edit:</em>
  I've found an almost solution. By setting the <code>AutoPostBack</code> property and the <code>CheckedChanged</code> event:</p>
  
  <pre><code>checkbox.AutoPostBack = false;
  checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
  </code></pre>
  
  <p>I can get code to be called on a post back for any check box that has changed. However this has two problems:</p>
  
  <ul>
  <li>The call back is processed after (or during, I'm not sure) <code>Page_Load</code> where I need to use this information</li>
  <li>The call back is not called for check boxes that were checked when the page loaded and still are.</li>
  </ul>
  
  <hr>
  
  <p><em>Edit 2:</em></p>
  
  <p>What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of <code>Form_Load</code>:</p>
  
  <pre><code>foreach (string v in this.Request.Form.AllKeys)
  {
      if (v.StartsWith(Prefix))
      {
          var data = v.Substring(Prefix.Length);
      }
  }
  </code></pre>
  
  <p>everything else seems to run to late.</p>