Forum Moderators: open
I have spent all morning googling for answers and I've tried everything the top sites have said (4guys, datagridgirl, msdn) but it just wont work and I cannot figure out what I'm doing wrong.
I am new to c# and .net - we are using VS 2008 and .net 3.5.
The aspx file contains:-
<asp:GridView ID="gridview_remove" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Course_Code" HeaderText="Course Code" />
<asp:BoundField DataField="Course_Title" HeaderText="Course Title" />
<asp:TemplateField HeaderText="Remove from SAR?">
<ItemTemplate>
<asp:CheckBox runat="server" ID="Remove_Course_Code" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The c# file contains:-
foreach (GridViewRow row in gridview_remove.Rows)
{
string cbn = row.FindControl("Remove_Course_Code").ClientID.ToString();
CheckBox cb = (CheckBox)row.FindControl(cbn);
if (cb != null)
{
Response.Write("field checked: " + cb.Checked);
}
else
{
Response.Write("NULL<br>");
}
}
The problem I have is that I cannot identify the fields checked when I click a submit button on the aspx page. The above code prints out NULL when I select all the checkboxes displayed.
I have tried the following line of code:-
CheckBox cb = (CheckBox)row.FindControl("Remove_Course_Code");
But this states that no checkboxes were checked by saying cb.Checked is false - even though I am ticking the checkboxes displayed.
Can someone identify what I am doing wrong?
Any help appreciated as this is driving me crazy.