0

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 hereate 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!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

dbugger!!!! you sure do what your nick says!I 've been blind with this(you know how it can get)! Thank you very much for your reply!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.