user2531590
user2531590

Reputation:

Button action event to get row details of grid view inside repeater

I am having some problem when trying to get the rows with marked checkbox in grid view. My grid view was set up using a repeater, here is the code:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
                    <ItemTemplate>
                        <!-- COLLAPSIBLE PANEL EXTENDER -->
                        <asp:Panel ID="pHeader1" runat="server" CssClass="cpHeader">
                            <!-- Collapsible panel extender header -->
                            <div class="form-group" style="background-color: #ffb848; height: 30px; vertical-align: middle">
                                <div class="col-md-6">
                                    <div style="float: left; color: White; padding: 5px 5px 0 0">
                                        <asp:Label ID="lblProduct" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' runat="server" />
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div style="float: right; color: White; padding: 5px 5px 0 0">
                                        <asp:Label ID="lblHeaderText1" runat="server" />
                                    </div>
                                </div>
                                <div style="clear: both"></div>
                            </div>
                        </asp:Panel>
                        <!-- Collapsible panel extender body -->
                        <asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
                            <asp:Label ID="lblBodyText1" runat="server" />
                            <!-- Grid view to show products based on each category -->
                            <asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="725px" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <Columns>
                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="cbCheckRow" runat="server" ItemStyle-Width="50px"/>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="id" HeaderText="Name" ItemStyle-Width="50px" />
                                    <asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="450px" />
                                    <asp:BoundField DataField="unitQuantity" HeaderText="Unit Quantity" />
                                    <asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200px">
                                        <ItemTemplate>
                                            <asp:TextBox ID="tbQuantity" runat="server" Width="40" Text="0" OnTextChanged="tbQuantity_TextChanged" AutoPostBack="true"/>
                                            <asp:Label ID="lblCheckAmount" runat="server" ForeColor="#a94442"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                </Columns>
                            </asp:GridView>
                        </asp:Panel>
                        <asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
                            ExpandControlID="pHeader1" Collapsed="true" TextLabelID="lblHeaderText1" CollapsedText="Show"
                            ExpandedText="Hide" CollapsedSize="0"
                            ScrollContents="false">
                        </asp:CollapsiblePanelExtender>
                    </ItemTemplate>
                </asp:Repeater>

What I am trying to do is, when a button is on click, it will get the datakey which is id for the rows with marked checkbox and store them into a list. So how am I supposed to write the code to loop each row of grid view to check if the check box has marked and store them into a string list?

Thanks in advance.

Upvotes: 1

Views: 1799

Answers (2)

ekad
ekad

Reputation: 14614

Let's say you want to put the DataKey values in a List<string>, then use a nested loop inside the Repeater and the GridView. The below code will collect all of the values in idList:

List<string> idList = new List<string>();

foreach (RepeaterItem ri in Repeater1.Items)
{
    GridView gvProduct = (GridView)ri.FindControl("gvProduct");

    foreach (GridViewRow gr in gvProduct.Rows)
    {
        CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
        if (cb.Checked)
        {
            // add the corresponding DataKey to idList
            idList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
        }
    }
}

Upvotes: 1

zey
zey

Reputation: 6105

Try this code ,

  foreach (RepeaterItem i in Repeater1.Items)
{
    GridView _gv = (GridView)i.FindControl("gvProduct");
    foreach (GridViewRow _g in _gv.Rows)
          {
    CheckBox chk = (CheckBox)_g.FindControl("cbCheckRow") ;
    if(chk.Checked)
     {
      .......
     }
         }
}

Upvotes: 0

Related Questions