Search code examples
c#asp.netgridviewdropdownrepeater

DropDownList inside GridView inside Repeater Control not working properly


I'm using Repeater control to generate tables on my dashboard. In the repeater, is a gridview to display item details. In the first column og the gridview is a dropdownlist to select an item and on selected index of that item, I'm fetching item details and the billing process continues for each table. Now the problem is that I'm only able to get the item details in the first repeater. Nothing happens when I select an item in another table or repeater. kindly help me with this issue.

Here is my ASPX markup:

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

And my c# code to fetch item details:

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


Solution

  • There is a problem in your code. you are always selecting the first item from the repeater control,

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

    Change your code to get the item from foreach loop, so your code will be

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

    try to use rptrItems instead of the first item of the repeater control each time.

    Hope this will help.