Repeater控件内GridView内的DropDownList无法正常工作

时间:2019-05-06 08:57:19

标签: c# asp.net gridview dropdown repeater

我正在使用Repeater控件在仪表板上生成表。在转发器中,是一个网格视图,用于显示项目详细信息。在GridView的第一列中,gridview是一个下拉列表,用于选择一个项目,在该项目的选定索引上,我正在获取项目详细信息,并且为每个表继续进行计费。现在的问题是,我只能在第一个转发器中获得商品详细信息。当我在另一个表或中继器中选择一个项目时,什么也没有发生。请帮我解决这个问题。

这是我的ASPX标记:

<div class="row">
                                <asp:Repeater ID="rptrTables" runat="server" OnItemDataBound="rptrTables_ItemDataBound">
                <ItemTemplate>
                <div class="col-md-6">
                  <div class="card card-info">
                   <div class="card-header with-border">
                     <h3 class="card-title"><asp:Label ID="lblTableNumber" runat="server" Text='<%# Eval("TableName") %>'></asp:Label></h3>
                     <div class="card-tools">
                       <button type="button" class="btn btn-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    </div>
                     </div>
                   <div class="card-body">
                   <div class="row">
                   <div class="col-md-12">
                  <div class="form-group">
                                                                <asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="table table-bordered table-hover table-responsive" GridLines="None" PageSize="10" OnRowDataBound="gvRowDataBound">
                                            <Columns>
                                                 <asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" />
                                                <asp:BoundField DataField="RowNumber" HeaderText="Sl. No." />
                                                <asp:TemplateField HeaderText="Item Name">
                                                    <ItemTemplate>
                                                        <asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"></asp:DropDownList>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Price">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice")%>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Qty">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity")%>'></asp:Label>
                                                    </ItemTemplate>
          </asp:TemplateField>

             <asp:TemplateField HeaderText="Line Total">
                 <ItemTemplate>
                  <asp:Label ID="lblLineTotal" runat="server" Text="0"></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                    <asp:TemplateField HeaderText="Add Item" ConvertEmptyStringToNull="False">
           <ItemTemplate>
            <asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" />
         </ItemTemplate>
          </asp:TemplateField>
            </Columns>
             </asp:GridView>
             </div>
             </div>
              </div>
              </div>
            <div class="card-footer">
             <div class="pull-right">
              <asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print KOT" />
                                <asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print Final Bill" />
                                <asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Complete Order" />
                            </div>
                    </div>
                 </div>
               </div>
             </ItemTemplate>
              </asp:Repeater>
          </div>

还有我的C#代码来获取商品详细信息:

protected void GetItemDetails(object sender, EventArgs e)
    {       
        foreach(RepeaterItem rptrItems in rptrTables.Items)
        {
            GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");
            foreach (GridViewRow row in gvItems.Rows)
            {
                DropDownList ddl = sender as DropDownList;
                Control ctrl = row.FindControl("drpItemName") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                    if (ddl.ClientID == ddl1.ClientID)
                    {
                        Label UnitPrice = row.FindControl("lblUnitPrice") as Label;
                        Label QTTY = row.FindControl("lblQuantity") as Label;
                        Label UPrice = row.FindControl("lblUnitPrice") as Label;
                        Label LINETOTAL = row.FindControl("lblLineTotal") as Label;
                        SqlConnection conn = new SqlConnection(constring);
                        conn.Open();
                        if (ctrl != null)
                        {
                            if ((ddl1.ID == ddl.ID) && (ddl1.SelectedIndex == ddl.SelectedIndex))
                            {
                                string str = "select * from ItemMaster where ItemName='" + ddl1.SelectedItem.ToString() + "'";
                                SqlCommand com = new SqlCommand(str, conn);
                                SqlDataReader reader = com.ExecuteReader();
                                while (reader.Read())
                                {
                                    UnitPrice.Text = reader["UnitPrice"].ToString();
                                    QTTY.Text = reader["Quantity"].ToString(); ;
                                    decimal totamt = Convert.ToDecimal(QTTY.Text) * Convert.ToDecimal(UnitPrice.Text);
                                }
                                reader.Close();
                                conn.Close();
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "swal", "swal('Item already selected, you can increase the quantity instead!', 'warning');", true);
                            }
                        }

                    }
                }
            }
        }             
    }

screen1

screen2

1 个答案:

答案 0 :(得分:0)

您的代码有问题。您总是从中继器控件中选择第一项,

GridView gvItems =(GridView)rptrTables.Items [0] .FindControl(“ grdOrder”);

更改代码以从foreach循环中获取项目,因此您的代码将为

GridView gvItems = (GridView)rptrItems.FindControl("grdOrder");

每次尝试使用rptrItems而不是转发器控件的第一项。

希望这会有所帮助。