Azeem
Azeem

Reputation: 2924

How to pass multiple selected check-box values in repeater C#

I am using Repeater control to display data from Database table in HTML table on ASPX page. There are also two link buttons Approve and Rejectwhich are bind with Repeater method ItemCommand.

There is a checkbox in first column of each row. This will enable user to select multiple rows and perform Approve and Reject operation. Here is the code:

<asp:Repeater ID="PendingRegRepeater" runat="server" OnItemCommand="PendingRegRepeater_ItemCommand">
                <HeaderTemplate>
                    <table>
                        <thead>
                            <tr>
                                <th>
                                    Select
                                </th>
                                <th>
                                    Customer Name
                                </th>
                                <th>
                                    Space ID
                                </th>
                                <th>
                                    Date
                                </th>
                                <th>
                                    Amount Paid
                                </th>
                                <th>
                                    Pin Code
                                </th>
                                <th>
                                    Payment Method
                                </th>
                                <th>
                                    Action
                                </th>
                            </tr>
                        </thead>
                </HeaderTemplate>
                <ItemTemplate>
                    <tbody>
                        <tr>
                            <td>
                                <asp:CheckBox runat="server" />
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "CustomerName") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "SpaceID") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "TransactionDate") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "AmountPaid") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "Pincode") %>
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "PaymentMethod") %>
                            </td>
                            <td>
                                <asp:LinkButton ID="lnkApprove" CommandName="Approve" CommandArgument='<%# Eval("RRID") %>' Text="Approve" runat="server" ForeColor="Black" Font-Underline="true"></asp:LinkButton>
                                /
                                <asp:LinkButton ID="lnkReject" CommandName="Reject" CommandArgument='<%# Eval("RRID") %>' Text="Reject" runat="server" ForeColor="Black" Font-Underline="true"></asp:LinkButton>
                            </td>
                        </tr>
                    </tbody>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

But I am unable to think a way to handle when user selects multiple checkboxes and presses Approve or Reject button. The compiler won't allow me to set ID attribute of Check-box a dynamic value <%# Eval("RRID") %>. Please suggest to handle this situation. thanks.

Upvotes: 3

Views: 7683

Answers (3)

Jacob
Jacob

Reputation: 3698

Greate, clearly sample i've made for you:

<asp:Repeater ID="rptPerson" runat="server" EnableViewState="false">
        <ItemTemplate>
            <asp:CheckBox ID="chCustomer" runat="server" Text='<%#Eval("FirstName") %>' />
            <asp:HiddenField ID="hidden" runat="server" Value='<%#Eval("LastName") %>' />
        </ItemTemplate>
</asp:Repeater>

In your C# code:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in rptPerson.Items)
    {
        CheckBox c = item.Controls[1] as CheckBox;
        if (c.Checked)
            doSomething((item.Controls[3] as HiddenField).Value);
    }
}

You can breakpoint at the beggining of this method to check wehre are the controls of your repeater positioned. In this example the asp:HiddenField is at position 3 and the asp:checkBox is at position 1.

I very !!! recommand you to create a UserControl that composite together all the elements you need for one row with the required properties like: Checked, ID etc... and simply put that control in the repeater section so it will generate all you need together for every row (include ID).

Upvotes: 2

Adil
Adil

Reputation: 148110

The id of checkbox will be generated using the id you assigned to the checkbox. ASP.net generated will have unique id for each checkbox in the generated row.

Html

<asp:CheckBox runat="server" id="checkBoxID" />

In code Behind

foreach (RepeaterItem item in PendingRegRepeater.Items)
{
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        var checkBox = (CheckBox)item.FindControl("checkBoxID");
        checkBox.Checked = true;
    }
}

Upvotes: 3

Servy
Servy

Reputation: 203821

Just set each checkbox a static ID. The actual ID object generated will be unique for each row.

You can then go through all of the rows and access that checkbox for each row:

foreach (RepeaterItem item in repeater.Items)
{
    var checkbox = item.FindControl("checkboxId") as CheckBox;
}

Upvotes: 2

Related Questions