3

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.

3 Answers 3

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

and on server side how to check which check-boxes are selected?
Actually I need to associate <%# Eval("RRID") %> with each check-box because after user selects check-boxes I will be able to query with each selected check-box id or row id.
You can try adding custom attribute to checkbox like <asp:CheckBox runat="server" id="checkBoxID" SomeAttr='<%# Eval("RRID") %>' /> and later accessing it like checkBox.Attributes["SomeAttr"].ToString()
2

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;
}

2 Comments

Actually I need to associate <%# Eval("RRID") %> with each check-box because after user selects check-boxes I will be able to query with each selected check-box id or row id.
@NidaSulheri But you already have access to all of the row information through the RepeaterItem object. There's no need to embed the information in the ID property.
2

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).

Comments

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.