0

私は現在、中小企業向けの請求書を生成するプロジェクトを行っています。

3 つのドロップダウンリスト (DDL) があります。

最初のドロップダウン リスト (製品カテゴリ) は、すでに 4 つの ListItem (カーペット、家具など) に設定されています。

最初の DDL から選択すると、2 番目のドロップダウンリスト (製品タイプ) は、最初の DDL から選択された値に一致するデータをデータベースから取得します。

同様に、2 番目の DDL から選択すると、3 番目のドロップダウンリスト (製品 ID) は、2 番目の DDL から選択された値に一致するデータをデータベースから取得します。

3 番目に、3 番目の DDL から選択すると、3 番目の DDL で選択された値に一致するデータベースから取得したデータがテキスト ボックス (製品説明、単価) に入力されます。

さて、問題は、クリックするたびに新しい行を動的に追加する「新しい行を追加」ボタンをクリックすると、2番目と3番目のDDLで選択された値がなくなり、テキストボックスの値が残ることです。

ユーザーが必要に応じて「新しい行を追加」ボタンをクリックしても、2 番目と 3 番目の 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();
    }

}
4

1 に答える 1

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

ドロップダウンを選択する前に、ドロップダウンに入力する必要があります。変更を行います。

解決策が得られたかどうか教えてください

于 2013-06-19T09:45:26.117 に答える