Auto-Compute Wheck Check in a Repeater

Hi, I have a repeater that display a customer’s bill. Now, the customer has the option to select which of the account he/she will pay. Therefore, in my interface, I have a checkbox in my repeater. All I wanted is to compute for the total amount to pay based on the selected checkboxes. Below is my code for my repeater under ItemTemplate.


<ItemTemplate>
                        <tr>
                            <td style="border: 1px solid #000;">
                                <asp:CheckBox ID="chkEntryCode" Text='<%# DataBinder.Eval(Container.DataItem, "Code") %>' runat="server" />
                            </td>
                            <td style="border: 1px solid #000;">
                                <asp:Label ID="lblRequestID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "requestid") %>'></asp:Label>
                            </td>
                            <td style="border: 1px solid #000;"><%# DataBinder.Eval(Container.DataItem, "Description") %></td>
                            <td style="border: 1px solid #000;">
                                <asp:Label ID="lblAmount" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Amount") %>'></asp:Label>
                            </td>
                        </tr>
                    </ItemTemplate>

And for me to compute for the amount to pay based on the checked checkboxes. I have the following code.


protected void rBill_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        double dAmountToPay = 0;

        for (int i = 0; i <= rBill.Items.Count - 1; i++)
        {
            CheckBox chkItem = (CheckBox)rBill.Items[i].FindControl("chkEntryCode");
            Label lblAmount = (Label)rBill.Items[i].FindControl("lblAmount");

            if (chkItem.Checked)
                dAmountToPay = dAmountToPay + double.Parse(lblAmount.Text);
        }
        lblTotalAmount.Text = dAmountToPay.ToString();
    }

Well, based from the code I provided, there’s nothing happens at all. Can anyone help me please.

Well, one problem is that this is in your item_command, and there is nothing executing said command. Try setting the AutoPostBack property of the checkbox to true. Although, I do not think this is the best way of handling this. Would be best to just do it with JS. As now you going to have to wait for a postback each time.

Also, might be a bit better to use a foreach instead of a for loop


foreach (RepeaterItem ri in rBill.Items)
{
CheckBox chkItem = (CheckBox)ri.FindControl("chkEntryCode");

}

I hope this helps

The chkEntryCode contains the unique reference of the customer’s rendered service. My problem was, how am I be able to reference on my lblAmount based on the checked checkbox?

In your code, u need to make the checkbox post back. Once in the post back via the autopostback feature. U can grab the checkboxes value if its checked.

CheckBox chk = (CheckBox)rBill.Items[e.Item.ItemIndex].FindControl(“chkID”);

That is if you do it on Item command. If you want to do it from a buttom click, then loop through the items as I suggested above

I think this is all about AJAX but I do not know how to implement it with this kind of scenario. So if I click/check on the checkbox, I will automatically compute for the rendered services.