我目前正在开展一个为中小型公司生成发票的项目。
我有3个下拉列表(DDL)。
第一个下拉列表(产品类别)已经设置为4个ListItem,即地毯,家具等
从第一个DDL中选择后,第二个下拉列表(产品类型)将从匹配到第一个DDL中所选值的数据库中检索数据。
同样,从第二个DDL中选择时,第三个下拉列表(产品ID)将从数据库中检索匹配到第二个DDL中所选值的数据。
第三,从第三个DDL中选择后,文本框(产品描述,单价)将填充从数据库中检索的数据,该数据与第三个DDL中的选定值匹配。
现在,问题是当我单击“添加新行”按钮时,每次单击时动态添加新行,第二个和第三个DDL中的选定值不再存在,但文本框中的值仍然存在。
有人可以帮我解决如何保留第二和第三个DDL的值,即使是用户点击他/她想要的“添加新行”按钮吗?
以下是我到目前为止所做的完整代码。
GenInvoice.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/AdminLogin.master" AutoEventWireup="true" CodeFile="GenInvoice.aspx.cs" Inherits="GenInvoice" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:BoundField DataField="Row Number" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>Carpets</asp:ListItem>
<asp:ListItem>Electrical Appliances</asp:ListItem>
<asp:ListItem>Furnitures</asp:ListItem>
<asp:ListItem>Others</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlType_SelectedIndexChanged">
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model No" >
<ItemTemplate>
<asp:DropDownList ID="ddlModelNo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlModelNo_SelectedIndexChanged" >
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="false" OnTextChanged="txtQuantity_TextChanged" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:TextBox ID="txtUPrice" runat="server" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblGTotal" runat="server">Grand Total :</asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub - Total">
<ItemTemplate>
<asp:TextBox ID="txtSTotal" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtGTotal" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterStyle />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnAddNewRow" runat="server" Text="Add New Row" OnClick="btnAddNewRow_Click" />
GenInvoice.aspx.cs
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 > 0)
{
for (int i = 1; i <= 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 > 0)
{
for (int i = 0; i < 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();
}
}
答案 0 :(得分:0)
当我浏览你的代码时,我注意到你写了这个
ddlCategory.Text = dt.Rows[i]["Category"].ToString();
ddlType.Text = dt.Rows[i]["Type"].ToString();
ddlModelNo.Text = dt.Rows[i]["Model No"].ToString();
实际上应该是这样的
fillCategory();
if(ddlCategory.items.contains(ddlCategory.items.FindByText(dt.Rows[i]["Category"].ToString())))
{
ddlCategory.clearselection();
ddlCategory.items.FindByText(dt.Rows[i]["Category"].ToString()).Selected=True;
}
fillType();
if(ddlType.items.contains(ddlType.items.FindByText(dt.Rows[i]["Type"].ToString())))
{
ddlType.clearselection();
ddlType.items.FindByText(dt.Rows[i]["Type"].ToString()).Selected=True;
}
fillModel();
if(ddlModelNo.items.contains(ddlModelNo.items.FindByText(dt.Rows[i]["Model No"].ToString())))
{
ddlModelNo.clearselection();
ddlModelNo.items.FindByText(dt.Rows[i]["Model No"].ToString()).Selected=True;
}
你必须在选择之前填写下拉列表。 做这些改变。
并告诉我如果你有解决方案