Looping through repeater only finds first checked checkbox, not the rest

A

Alan Silver

Hello,

I have a repeater with a list of products in it. Each item in the
repeater has a checkbox, allowing the product to be selected. When a
link button is clicked, I want to loop through the repeater and handle
each product whose checkbox is checked.

The repeater looks (greatly chopped down) like this...

<asp:Repeater ID="rptProducts" RunAt="Server">
<ItemTemplate>
<li><asp:CheckBox ID="chkDelete" RunAt="server"/>
<%#DataBinder.Eval(Container.DataItem, "PName")%>
</ItemTemplate>
</asp:Repeater>

and the code for the link button looks like...

void lbtDeleteProducts_Click(object o, EventArgs e) {
for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items.FindControl("chkDelete")).Checked) {
// do stuff here
}
}
}

The problem is that the loop works fine for the first product checked,
but not for any others. The weird thing is that this code is pretty much
identical to code I have used elsewhere that works fine. I can' see why
this only picks up the first checked checkbox. Anyone any idea why?

TIA
 
G

Guest

I use the following code and have got it working all the time

foreach(RepeaterItem rptItem in ptProducts.Items)
{
CheckBox chk = (CheckBox) rptItem.FindControl("chk_Select");
if(chk.Checked)
// do stuff here;
}
 
A

Alan Silver

I use the following code and have got it working all the time

Thanks, but that did the same as my code.

I have this sort of code working in plenty other places, I'm sure
there's some quirk here that I haven't noticed. I was hoping someone
else would spot it ;-(
foreach(RepeaterItem rptItem in ptProducts.Items)
{
CheckBox chk = (CheckBox) rptItem.FindControl("chk_Select");
if(chk.Checked)
// do stuff here;
}




Alan Silver said:
Hello,

I have a repeater with a list of products in it. Each item in the
repeater has a checkbox, allowing the product to be selected. When a
link button is clicked, I want to loop through the repeater and handle
each product whose checkbox is checked.

The repeater looks (greatly chopped down) like this...

<asp:Repeater ID="rptProducts" RunAt="Server">
<ItemTemplate>
<li><asp:CheckBox ID="chkDelete" RunAt="server"/>
<%#DataBinder.Eval(Container.DataItem, "PName")%>
</ItemTemplate>
</asp:Repeater>

and the code for the link button looks like...

void lbtDeleteProducts_Click(object o, EventArgs e) {
for (int i=0; i<rptProducts.Items.Count; i++) {
if (((CheckBox)rptProducts.Items.FindControl("chkDelete")).Checked) {
// do stuff here
}
}
}

The problem is that the loop works fine for the first product checked,
but not for any others. The weird thing is that this code is pretty much
identical to code I have used elsewhere that works fine. I can' see why
this only picks up the first checked checkbox. Anyone any idea why?

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top