1

I have a GridView populated with SQL data. If the account has not been provisioned, a dynamically created checkbox is created in the 6th column to allow for provisioning according to my company's guidlines. I need to check the checkbox in each record to see if it checked or not, but I cannot get the following code to work on my button click. The 6th column (which was manually added via edit columns and is no data bound) holds the dynamically created check box, but it never evaluates to true.

 foreach (GridViewRow gv in GridView1.Rows)
    {
        int index = gv.RowIndex;
        //Get Value
        string id = "MainContent_GridView1_ctl00_" + index;

        CheckBox chk =  GridView1.Rows[index].Cells[6].FindControl(id) as CheckBox;

        //System.Windows.Forms.MessageBox.Show(chk.ToString());

            if (chk == null) { System.Windows.Forms.MessageBox.Show(id + " " + "is NOT checked"); }

            if (chk != null && chk.Checked)
            {
                System.Windows.Forms.MessageBox.Show(id + " " + "is checked");
            }

    }

Here is the page source after runtime

  <div>
 <table cellspacing="0" align="Center" rules="all" border="1" id="MainContent_GridView1" style="width:800px;border-collapse:collapse;">
  <tr>
   <th align="left" scope="col">Employee Number</th><th align="left" scope="col">First Name</th><th align="left" scope="col">Last Name</th><th align="left" scope="col">License Code</th><th align="left" scope="col">Job Title</th><th scope="col">Provisioned</th><th scope="col">Process</th>
  </tr><tr>
   <td>123123</td><td>Pete</td><td>Griffin</td><td>&nbsp;</td><td>Test Record</td><td align="center">False</td><td align="center"><input id="MainContent_GridView1_ctl00_0" type="checkbox" name="ctl00$MainContent$GridView1$ctl02$ctl00" checked="checked" /></td>
  </tr>
 </table>
</div>

I have been all over the web. I am a moderate C# programmer, but no pro by any means.

  • Is your "if (chk == null)" always evaluating true? A null tells me that the checkbox was never created. Otherwise there should be another check for "if (chk != null && !chk.Checked)" – Shar1er80 Apr 03 '15 at 14:54
  • Yes, its always null. There is currently one test record in the database and the checkbox is there at runtime and I can check it, but always false. –  Apr 03 '15 at 14:57
  • Possibly related: http://stackoverflow.com/a/2860969/102937 – Robert Harvey Apr 03 '15 at 14:58
  • If chk resulted in null then it wasn't successfully casted as a CheckBox. Catch the FindControl result as an object and then view what it is in debug. – Shar1er80 Apr 03 '15 at 15:12
  • Do I just catch that as a string or boolean? –  Apr 03 '15 at 16:16
  • Here is the code I am using to generate the checkbox at runtime –  Apr 03 '15 at 16:30
  • var checkBox = new System.Web.UI.WebControls.CheckBox(); GridView1.Rows[index].Cells[6].Controls.Add(checkBox); –  Apr 03 '15 at 16:30
  • Ok, I figured it out. I was looking for the wrong ID. Instead of looking for MainContent_GridView1_ctl00_0 I needed to be looking for cl100_0 only. That worked. Thanks! –  Apr 03 '15 at 16:33
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Apr 03 '15 at 18:27
  • Where is there a tag in my title? –  Apr 14 '15 at 02:09

0 Answers0