我如何处理动态生成的文本框

时间:2019-03-26 17:40:02

标签: c# asp.net session session-variables session-state

我正在创建一个网上商店,目前正在使用购物车。我会在会话中将买家想要的产品保留在购物车中,并在“ ShoppingCart”页面上生成一个表格。购物车上会创建一个带有删除按钮的行,一个用于数字的文本框,依此类推。我创建了一个“更新购物车”按钮,当我按下它时,我希望在那里替换每个产品的变量“数字”该产品行中的数字。但我不知道如何处理文本框,因为例如“ textbox1.text”不起作用。

我不知道要提前多少个文本框,这取决于客户在购物车中添加了多少种不同的产品。

一些帮助将不胜感激!预先感谢!

这是我生成文本框的代码

private void vulCart()
        {
            myTable.Rows.Clear();

            //header row aan maken 
            TableRow rowH = new TableRow();
            TableCell cellH1 = new TableCell();
            TableCell cellH2 = new TableCell();
            TableCell cellH3 = new TableCell();
            TableCell cellH4 = new TableCell();
            TableCell cellH5 = new TableCell();
            TableCell cellH6 = new TableCell();

            cellH1.Text = "Id";
            cellH1.CssClass = "bold";

            cellH2.Text = "Product";
            cellH2.CssClass = "bold";

            cellH3.Text = "Prijs";
            cellH3.CssClass = "bold";

            cellH4.Text = "Aantal";
            cellH4.CssClass = "bold";

            cellH5.Text = "Totaal";
            cellH5.CssClass = "bold";

            cellH6.Text = "";
            cellH6.CssClass = "bold";

            rowH.Cells.Add(cellH1);
            rowH.Cells.Add(cellH2);
            rowH.Cells.Add(cellH3);
            rowH.Cells.Add(cellH4);
            rowH.Cells.Add(cellH5);
            rowH.Cells.Add(cellH6);

            myTable.Rows.Add(rowH);

            //generate a row for each items in session "shoppingcart"
            if (Session["ShoppingCart"] != null)
            {
                ShoppingCart cart = (ShoppingCart)Session["ShoppingCart"];

                double totaal = 0;

                // for elk product dat in shopping staat 
                for (int i = 0; i < cart.Items.Count; i++)
                {
                    double totaalProduct = 0;

                    totaalProduct = Convert.ToDouble(cart.Items[i].Aantal) * Convert.ToDouble(cart.Items[i].Prijs);

                    TableRow row = new TableRow();

                    TableCell cell1 = new TableCell();
                    TableCell cell2 = new TableCell();
                    TableCell cell3 = new TableCell();
                    TableCell cell4 = new TableCell();
                    TableCell cell5 = new TableCell();
                    TableCell cell6 = new TableCell();

                    Button btn_Delete = new Button();
                    btn_Delete.CssClass = "btn";
                    btn_Delete.Text = "Verwijder";
                    btn_Delete.Attributes.Add("ProductNaam", cart.Items[i].Product.ToString());
                    btn_Delete.Click += new EventHandler(Btn_Click);

                    cell1.Text = Convert.ToString(i + 1);
                    cell2.Text = cart.Items[i].Product.ToString();
                    cell3.Text = cart.Items[i].Prijs.ToString();
                    cell4.Text = cart.Items[i].Aantal.ToString();
                    cell5.Text = totaalProduct.ToString();
                    cell6.Controls.Add(btn_Delete);




                    row.Cells.Add(cell1);
                    row.Cells.Add(cell2);
                    row.Cells.Add(cell3);
                    row.Cells.Add(cell4);
                    row.Cells.Add(cell5);
                    row.Cells.Add(cell6);

                    myTable.Rows.Add(row);

                    totaal = totaal + totaalProduct;


                }
            }

        }

0 个答案:

没有答案