【问题标题】:How to Dynamically create textboxes using ASP.NET and then save their values in Database?如何使用 ASP.NET 动态创建文本框,然后将它们的值保存在数据库中?
【发布时间】:2013-02-11 16:47:52
【问题描述】:

我正在创建一个调查网站。我想动态添加文本框,然后在数据库中获取它们的值。

现在假设我从下拉列表中选择 4 个文本框以动态显示。

下拉选择代码:

     protected void NumDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "TextBox")
        {

            int j;
            i = int.Parse(NumDropDown.SelectedValue);
            Session["i"] = i;
            switch (i)
            {
                case 1:
                    t = new TextBox[i];
                    Session["textBox"] = t;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);

                    }
                    break;
                case 2:
                    t = new TextBox[i];
                    Session["textBox"] = t;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);

                    }
                    break;

                case 3:
                    t = new TextBox[i];
                    Session["textBox"] = t;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);

                    }
                    break;
                case 4:
                    t = new TextBox[i];
                    List<TextBox> MyTextBoxes;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);
                        try
                        {
                            MyTextBoxes = (List<TextBox>)Session["AddedTextBox"];
                            MyTextBoxes.Add(t[j]);
                            Session["AddedTextBox"] = MyTextBoxes;
                        }
                        catch
                        {
                            MyTextBoxes = new List<TextBox>();
                            MyTextBoxes.Add(t[j]);
                            Session["AddedTextBox"] = MyTextBoxes;
                        }
                    }
                    break;
            }

        }
    }

2) 然后在这里我在文本框中输入值,例如 a、b、c、d,然后单击添加:

添加点击时的点击代码:

1) 首先我在 Page_Init 上检查了会话:

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["AddedTextBox"] != null)
        {
            string a;
            string b;
            string c;
            string d;

            int listCount = ((List<TextBox>)Session["AddedTextBox"]).Count;
            foreach (TextBox t in ((List<TextBox>)Session["AddedTextBox"]))
            {
                if (listCount == 1)
                {

                }
                if (listCount == 2)
                {

                }
                if (listCount == 3)
                {

                }
                if (listCount == 4)
                {
                    if (t.ID == "txtCheckbox0")
                    {
                        a = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        b = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        c = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        d = t.Text;
                    }

                }
            }
        }

但这里的问题是我没有得到文本值,它们似乎是空的。 请帮我解决这个问题。

【问题讨论】:

标签: c# asp.net


【解决方案1】:

这听起来像是 asp.net 向页面动态添加控件的古老经典问题。

问题在于回发时控件的视图状态和重建。

您需要在页面生命周期的正确时间运行在回发上生成控件的相同代码,以确保回发值与服务器端控件匹配。

当然,如果您想要一个 hacky 快捷方式,请直接访问 Request.Form["textCheckbox" + index]

A useful article on the subject.

【讨论】:

    【解决方案2】:

    正如@jenson-button-event 提到的,您可以通过Request.Form 访问TextBox 值,这是一个示例:

    ASPX:

    <asp:DropDownList ID="ddlTextBoxes" runat="server">
        <asp:ListItem Value="1" Text="1" />
        <asp:ListItem Value="5" Text="5" />
    </asp:DropDownList>
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" /><br />
    <asp:PlaceHolder ID="container" runat="server" Visible="false">
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
    </asp:PlaceHolder>
    

    背后的代码:

        protected void Add(object sender, EventArgs e)
        {
            int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value);
            var table = new Table();
    
            for (int i = 0; i < numOfTxt; i++)
            {
                var row = new TableRow();
                var cell = new TableCell();
    
                TextBox textbox = new TextBox();
                textbox.ID = "Textbox" + i;
                textbox.Width = new Unit(180);
    
                cell.Controls.Add(textbox);
                row.Cells.Add(cell);
                table.Rows.Add(row);
            }
    
            container.Controls.AddAt(0,table);
            container.Visible = true;
        }
    
        protected void Submit(object sender, EventArgs e)
        {
            var textboxValues = new List<string>();
            if (Request.Form.HasKeys())
            {
                Request.Form.AllKeys.Where(i => i.Contains("Textbox")).ToList().ForEach(i =>
                    {
                        textboxValues.Add(Request.Form[i]);
                    });
            }
    
            //Do something with the textbox values
            textboxValues.ForEach(i => Response.Write(i + "<br />"));
            container.Visible = false;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 2021-05-12
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多