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[i].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
--
Alan Silver
(anything added below this line is nothing to do with me)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;
}
"Alan Silver" wrote:
> 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[i].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
> --
> Alan Silver
> (anything added below this line is nothing to do with me)
>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" wrote:
>> 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[i].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
>>
>> --
>> Alan Silver
>> (anything added below this line is nothing to do with me)
>
--
Alan Silver
(anything added below this line is nothing to do with me)
0 comments:
Post a Comment