Capture Checked Value From Data Grid

I continue to get an error of

Object not set to instance of object

What do I need to change so that this code can execute succesfully?

HTML

<td valign="top" style="text-align: left; width: 200px;">
<asp:GridView runat="server" ID="datagridTest" AutoGenerateColumns="false" GridLines="Both" ShowFooter="true" >
<Columns>
<asp:BoundField DataField="field1" HeaderText="1st Set" />
<asp:BoundField DataField="field2" HeaderText="2nd Set" />
<asp:TemplateField>
    <ItemTemplate>
        <asp:Label runat="server" Text='<%#Eval("categoryID") %>' ID="managerID" Visible="false"></asp:Label>
     </ItemTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="Checked" runat="server" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("Checked")) %>' />
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>                                
<asp:Button runat="server" ID="btnChecked" CssClass="Buttons" Text="Add" OnClick="btnC_Click" />
</td>

C#

protected void btnC_Click(object sender, EventArgs e)
{
try
{
    foreach (GridViewRow row in datagridTest.Rows)
    {
        string ID = ((Label)row.FindControl("managerID")).Text;
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)row.FindControl("Checked");
            if (chk.Checked) {}
            else {}
        }
    }
}
catch (Exception exception) { throw exception; }
}

Answers


in your html you are setting your gridviews id as

ID="datagridTest"

but in the code behind you use "dgRD".this may be the root your problem.


Need Your Help

Overlaying several CLR reference fields with each other in explicit struct?

c# .net struct clr

Edit: I'm well aware of that this works very well with value types, my specific question is about using this for reference types.

How does implementing multiple COM interfaces work in C++?

c++ com static-cast multiple-interface-implem

I am trying to understand this example code regarding Browser Helper Objects.