Reputation: 1
Greetigs fellow programmers!
I have a question for you about the implementation of a CheckBox check across a Gridview.
First of all, the Gridview is binded to a different datasource according to the querystring that links to the page. This is working so far, i see different elements by different querystrings.
What the problem is, and i cannot yet detect how to fix, is that if i select let's say 3 out of 4 checkboxes and press and button that verifies the selections (writes selections to textbox), the program AUTOMATICALLY CHECKS ALL the checkboxes and gives me all the values (which i don't need) . I browsed for answers but could not find something similar (or i didn't search good enough,w/e).
Could some kind fellow programmer maybe explain what the mistake is, how to guard from possible bugs, or indicenter code here
ate the changes i need to do?
The code for the Gridview:
<asp:GridView ID="Foods" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="CheckBox1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the C# code: (code is under a Button's onclick method)
String output = "";
foreach (GridViewRow row in Foods.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if ((cb != null) && (cb.Checked = true))
{
output += row.Cells[1].Text.ToString() + ",";
}
}
TextBox1.Text = output;
Any help will be appreciated!
Upvotes: 0
Views: 5517
Reputation: 16389
in the C# code
if ((cb != null) && (cb.Checked = true))
this
cb.Checked = true
should be
cb.Checked == true
you are making an assignment, instead of checking the value
Upvotes: 1