按钮操作事件,以获取转发器内网格视图的行详细信息

时间:2014-01-22 10:12:57

标签: c# asp.net gridview repeater

尝试在网格视图中获取带有标记复选框的行时遇到问题。我的网格视图是使用转发器设置的,这是代码:

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

我要做的是,当点击一个按钮时,它将获得datakey,它是带有标记复选框的行的id,并将它们存储到列表中。那么我该如何编写代码来遍历网格视图的每一行以检查复选框是否已标记并将它们存储到字符串列表中?

提前致谢。

2 个答案:

答案 0 :(得分:1)

假设您要将DataKey值放在List<string>中,然后在RepeaterGridView内使用嵌套循环。以下代码将收集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());
        }
    }
}

答案 1 :(得分:0)

试试这段代码,

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