title: when checking if a checkbox has been checked or not I got a null object
body: <p>I am trying to see if a row has been selected or not by using a checkbox.  The row is inside of a ListView.  I got a null object error.</p>
  
  <pre><code>&lt;td&gt;
     &lt;asp:CheckBox id="CheckBox1" runat="server"
                   value='&lt;%# Eval("rdvId") %&gt;'
                   AutoPostBack="true"
                   OnCheckedChanged="Check_Clicked"/&gt;
  &lt;/td&gt;
  
  protected void Check_Clicked(Object sender, EventArgs e)
  {
  
      int KeyId = 0;
  
      foreach (ListViewDataItem item in ListView1.Items)
      {
          /*     if (((CheckBox)item.FindControl("MyCheckBox")).Checked) */
          CheckBox myCheckBox = (CheckBox)item.FindControl("MyCheckBox");
          if (myCheckBox.Checked)     //      &lt;===  generate a null object.
          {
              KeyRdvId = Convert.ToInt32(ListView1.DataKeys[item.DataItemIndex].Value); 
                 // ( I want to get the key of the selected listview) 
          }
      }
  }
  </code></pre>
  
  <p>I used this code in the procedure protected void btnSubmit_Click(object sender, EventArgs e)
  and it works there !!!</p>
  
  <pre><code>protected void btnSubmit_Click(object sender, EventArgs e)
  {
      int iCptCheckBox = 0;
      int indxChkBox = 0;
  
      foreach (ListViewDataItem item in ListView1.Items)
      {
  
          var chk = item.FindControl("MyCheckBox") as System.Web.UI.HtmlControls.HtmlInputCheckBox;
          if (chk != null &amp;&amp; chk.Checked)
          {
              indxChkBox = Convert.ToInt32(chk.Value);//  &lt;==== here it is working 
  </code></pre>
  
  <p>Any idea why it is not working in the first one ?</p>