Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Retain Value for dropdownlists in gridview
    primarykey
    data
    text
    <p>I'm currently doing a project on generating invoices for small and medium companies. </p> <p>I have 3 dropdownlists (DDL).</p> <p>The first Dropdownlist (Product Category) has already been set to 4 ListItem, i.e Carpets,Furnitures, etc</p> <p>Upon selecting from first DDL, the second dropdownlist (Product Type) would retrieve data from database matching to the selected value from first DDL.</p> <p>Similarly upon selecting from second DDL, the third dropdownlist (Product ID) would retrieve data from database matching to the selected value from second DDL.</p> <p>Thirdly, upon selecting from third DDL, the textboxes (Product Description, Unit Price) would be populated with data retrieve from database matching to the selected value in third DDL.</p> <p>Now, the problem is when I click on a button "Add New Row" which dynamically add new row each time clicked, the selected value in second and third DDL is no more there but the values in textboxs remains there.</p> <p>Could someone help me out on how to retain the values for second and third DDL even is user clicks on button "Add New Row" as he/she wants?</p> <p>Below is the full code that I have done so far. </p> <p>GenInvoice.aspx</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/AdminLogin.master" AutoEventWireup="true" CodeFile="GenInvoice.aspx.cs" Inherits="GenInvoice" %&gt; </code></pre> <p> </p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="Row Number" HeaderText="Row Number" /&gt; &lt;asp:TemplateField HeaderText="Category"&gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged"&gt; &lt;asp:ListItem&gt;Please Select&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Carpets&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Electrical Appliances&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Furnitures&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Others&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Type"&gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlType_SelectedIndexChanged"&gt; &lt;asp:ListItem&gt;Please Select&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Model No" &gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList ID="ddlModelNo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlModelNo_SelectedIndexChanged" &gt; &lt;asp:ListItem&gt;Please Select&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Product Description"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblDescription" runat="server"&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Quantity"&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="false" OnTextChanged="txtQuantity_TextChanged" &gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Unit Price"&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID="txtUPrice" runat="server" AutoPostBack="true"&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:Label ID="lblGTotal" runat="server"&gt;Grand Total :&lt;/asp:Label&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Sub - Total"&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID="txtSTotal" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:TextBox ID="txtGTotal" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField&gt; &lt;FooterStyle /&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button ID="btnAddNewRow" runat="server" Text="Add New Row" OnClick="btnAddNewRow_Click" /&gt; </code></pre> <p>&nbsp;</p> <p></p> <p>GenInvoice.aspx.cs</p> <pre><code>public partial class GenInvoice : System.Web.UI.Page { private double subTotal = 0; protected void Page_Load(object sender, EventArgs e) { //GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound); if (!Page.IsPostBack) { SetInitialRow(); } } private void SetInitialRow() { DataTable dt = new DataTable(); DataRow dr = null; //dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Row Number", typeof(string))); dt.Columns.Add(new DataColumn("Category", typeof(string))); dt.Columns.Add(new DataColumn("Type", typeof(string))); dt.Columns.Add(new DataColumn("Model No", typeof(string))); dt.Columns.Add(new DataColumn("Description", typeof(string))); dt.Columns.Add(new DataColumn("Quantity", typeof(string))); dt.Columns.Add(new DataColumn("Unit Price", typeof(string))); dt.Columns.Add(new DataColumn("Sub-Total", typeof(string))); dr = dt.NewRow(); dr["Row Number"] = 1; dr["Category"] = string.Empty; dr["Type"] = string.Empty; dr["Model No"] = string.Empty; dr["Description"] = string.Empty; dr["Quantity"] = string.Empty; dr["Unit Price"] = string.Empty; dr["Sub-Total"] = string.Empty; dt.Rows.Add(dr); dr = dt.NewRow(); //Store the DataTable in ViewState ViewState["CurrentTable"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); } private void AddNewRowToGrid() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count &gt; 0) { for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++) { //extract the TextBox values //TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); //TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); //DropDownList ddlProductType = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddlProductType"); DropDownList ddlCategory = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlCategory"); DropDownList ddlType = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("ddlType"); DropDownList ddlModelNo = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("ddlModelNo"); Label lblDescription = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("lblDescription"); TextBox txtQuantity = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("txtQuantity"); TextBox txtUPrice = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("txtUPrice"); TextBox txtSTotal = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("txtSTotal"); drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["Row Number"] = i + 1; //dtCurrentTable.Rows[i - 1]["Category"] = box1.Text; dtCurrentTable.Rows[i - 1]["Category"] = ddlCategory.Text; dtCurrentTable.Rows[i - 1]["Type"] = ddlType.Text; dtCurrentTable.Rows[i - 1]["Model No"] = ddlModelNo.Text; dtCurrentTable.Rows[i - 1]["Description"] = lblDescription.Text; dtCurrentTable.Rows[i - 1]["Quantity"] = txtQuantity.Text; dtCurrentTable.Rows[i - 1]["Unit Price"] = txtUPrice.Text; dtCurrentTable.Rows[i - 1]["Sub-Total"] = txtSTotal.Text; rowIndex++; } dtCurrentTable.Rows.Add(drCurrentRow); ViewState["CurrentTable"] = dtCurrentTable; GridView1.DataSource = dtCurrentTable; GridView1.DataBind(); } } else { Response.Write("ViewState is null"); } //Set Previous Data on Postbacks SetPreviousData(); } private void SetPreviousData() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count &gt; 0) { for (int i = 0; i &lt; dt.Rows.Count; i++) { //TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1"); //TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2"); //DropDownList ddlProductType = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddlProductType"); DropDownList ddlCategory = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlCategory"); DropDownList ddlType = (DropDownList)GridView1.Rows[rowIndex].Cells[2].FindControl("ddlType"); DropDownList ddlModelNo = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("ddlModelNo"); Label lblDescription = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("lblDescription"); TextBox txtQuantity = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("txtQuantity"); TextBox txtUPrice = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("txtUPrice"); TextBox txtSTotal = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("txtSTotal"); ddlCategory.Text = dt.Rows[i]["Category"].ToString(); ddlType.Text = dt.Rows[i]["Type"].ToString(); ddlModelNo.Text = dt.Rows[i]["Model No"].ToString(); lblDescription.Text = dt.Rows[i]["Description"].ToString(); txtQuantity.Text = dt.Rows[i]["Quantity"].ToString(); txtUPrice.Text = dt.Rows[i]["Unit Price"].ToString(); txtSTotal.Text = dt.Rows[i]["Sub-Total"].ToString(); rowIndex++; } } } } protected void btnAddNewRow_Click(object sender, EventArgs e) { AddNewRowToGrid(); } private void Calc() { double grandtotal = 0.0; foreach (GridViewRow dr in GridView1.Rows) { double price = Convert.ToDouble(((TextBox)dr.FindControl("txtUPrice")).Text); //double price = double.Parse(((TextBox)dr.FindControl("txtUPrice")).Text); int qty = int.Parse(((TextBox)dr.FindControl("txtQuantity")).Text); //Calculates Sub-Total double total = price * qty; //Displays Sub-Total ((TextBox)dr.FindControl("txtSTotal")).Text = Convert.ToString(total); //Calculates GrandTotal grandtotal = grandtotal + total; } //Displays Grand Total GridViewRow row = GridView1.FooterRow; ((TextBox)row.FindControl("txtGTotal")).Text = Convert.ToString(grandtotal); } protected void ddlType_SelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow dr in GridView1.Rows) { //string type = Convert.ToString(((DropDownList)dr.FindControl("ddlType")).Text); DropDownList ddlType = (DropDownList)dr.FindControl("ddlType"); DropDownList ddlModelNo = (DropDownList)dr.FindControl("ddlModelNo"); //ddlModelNo.Items.Clear(); //if (type == "Cupboard") //{ MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem"); con.Open(); //MySqlCommand cmd = new MySqlCommand("SELECT productID FROM product WHERE productType='" + typeCupboard + "'", con); MySqlCommand cmd2 = new MySqlCommand("SELECT productID FROM product WHERE productType='" + ddlType.SelectedValue + "'", con); MySqlDataAdapter da1 = new MySqlDataAdapter(cmd2); DataSet ds1 = new DataSet(); da1.Fill(ds1); ddlModelNo.DataSource = ds1; ddlModelNo.DataValueField = "productID"; ddlModelNo.DataTextField = "productID"; ddlModelNo.DataBind(); ddlModelNo.Items.Insert(0, new ListItem("Please Select", "Please Select")); Session["ddlTypeValue"] = ddlType.SelectedValue; } } protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow dr in GridView1.Rows) { //string type = Convert.ToString(((DropDownList)dr.FindControl("ddlType")).Text); DropDownList ddlCat = (DropDownList)dr.FindControl("ddlCategory"); //DropDownList ddlModelNo = (DropDownList)dr.FindControl("ddlModelNo"); Session["ddlCat"] = ddlCat.SelectedValue; string category = Convert.ToString(((DropDownList)dr.FindControl("ddlCategory")).Text); if (category == "Carpets") { DropDownList ddlType = (DropDownList)dr.FindControl("ddlType"); MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem"); con.Open(); MySqlCommand cmd = new MySqlCommand("SELECT carpets FROM producttypecarpets", con); MySqlDataAdapter da = new MySqlDataAdapter(cmd); //MySqlDataReader dz = cmd.ExecuteReader(); DataSet ds = new DataSet(); da.Fill(ds); ddlType.DataSource = ds; ddlType.DataValueField = "carpets"; ddlType.DataTextField = "carpets"; ddlType.DataBind(); ddlType.Items.Insert(0, new ListItem("Please Select", "Please Select")); } if (category == "Electrical Appliances") { DropDownList ddlType = (DropDownList)dr.FindControl("ddlType"); MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem"); con.Open(); MySqlCommand cmd = new MySqlCommand("SELECT electrical FROM producttypeelectrical", con); MySqlDataAdapter da = new MySqlDataAdapter(cmd); //MySqlDataReader dz = cmd.ExecuteReader(); DataSet ds = new DataSet(); da.Fill(ds); ddlType.DataSource = ds; ddlType.DataValueField = "electrical"; ddlType.DataTextField = "electrical"; ddlType.DataBind(); ddlType.Items.Insert(0, new ListItem("Please Select", "Please Select")); } if (category == "Furnitures") { DropDownList ddlCategory = (DropDownList)dr.FindControl("ddlCategory"); DropDownList ddlType = (DropDownList)dr.FindControl("ddlType"); //DropDownList ddlModelNo = (DropDownList)dr.FindControl("ddlModelNo"); //Label lblDescription = (Label)dr.FindControl("lblDescription"); //TextBox txtUPrice = (TextBox)dr.FindControl("txtUPrice"); //string catFurnitures = "Furnitures"; MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem"); con.Open(); MySqlCommand cmd = new MySqlCommand("SELECT DISTINCT productType FROM product WHERE productCategory='" + ddlCategory.SelectedValue + "'", con); MySqlDataAdapter da = new MySqlDataAdapter(cmd); //MySqlDataReader dz = cmd.ExecuteReader(); DataSet ds = new DataSet(); da.Fill(ds); ddlType.DataSource = ds; ddlType.DataValueField = "productType"; ddlType.DataTextField = "productType"; ddlType.DataBind(); ddlType.Items.Insert(0, new ListItem("Please Select", "Please Select")); } //if (category == "Others") //Type(); //GridView1.DataBind(); } } protected void ddlModelNo_SelectedIndexChanged(object sender, EventArgs e) { foreach (GridViewRow dr in GridView1.Rows) { string type = Convert.ToString(((DropDownList)dr.FindControl("ddlType")).Text); DropDownList ddlType = (DropDownList)dr.FindControl("ddlType"); DropDownList ddlModelNo = (DropDownList)dr.FindControl("ddlModelNo"); Label lblDescription = (Label)dr.FindControl("lblDescription"); TextBox txtUPrice = (TextBox)dr.FindControl("txtUPrice"); //if (type == "Cupboard") //{ MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem"); con.Open(); //MySqlCommand cmd = new MySqlCommand("SELECT productID FROM product WHERE productType='" + typeCupboard + "'", con); MySqlCommand cmd2 = new MySqlCommand("SELECT productDesc,productSellingPrice FROM product WHERE productID='" + ddlModelNo.SelectedValue + "'", con); MySqlDataAdapter da1 = new MySqlDataAdapter(cmd2); MySqlDataReader myDataReader = cmd2.ExecuteReader(); while (myDataReader.Read()) { //ddlCompanyName.Items.Add(dr["compName"].ToString()); lblDescription.Text = myDataReader["productDesc"].ToString(); txtUPrice.Text = myDataReader["productSellingPrice"].ToString(); } DataSet ds1 = new DataSet(); da1.Fill(ds1); ddlModelNo.DataSource = ds1; ddlModelNo.DataValueField = "productID"; ddlModelNo.DataTextField = "productID"; ddlModelNo.DataBind(); } } protected void txtQuantity_TextChanged(object sender, EventArgs e) { Calc(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload