C# 如何使用ASP.NET动态创建文本框,然后将其值保存在数据库中?

C# 如何使用ASP.NET动态创建文本框,然后将其值保存在数据库中?,c#,asp.net,C#,Asp.net,我正在创建一个调查网站。我想动态添加文本框,然后在数据库中获取它们的值 现在让我们假设我从下拉列表中选择了4个文本框来动态显示 下拉列表中选择的代码: protected void NumDropDown_SelectedIndexChanged(object sender, EventArgs e) { if (DropDownList1.SelectedValue == "TextBox") { int j;

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

现在让我们假设我从下拉列表中选择了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;
            }

        }
    }
<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;
    }
protectedvoid NumDropDown\u SelectedIndexChanged(对象发送方,事件参数e)
{
如果(DropDownList1.SelectedValue==“文本框”)
{
int j;
i=int.Parse(NumDropDown.SelectedValue);
会话[“i”]=i;
开关(一)
{
案例1:
t=新文本框[i];
会话[“文本框”]=t;
对于(j=0;j
2) 然后在这里,我在文本框中输入a、b、c、d等值,然后单击添加:

添加单击的代码:

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

        }
    }
<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;
    }
1) 首先,我在页面_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;
                    }

                }
            }
        }
受保护的无效页\u Init(对象发送方,事件参数e)
{
如果(会话[“AddedTextBox”]!=null)
{
字符串a;
b串;
字符串c;
字符串d;
int listCount=((列表)会话[“AddedTextBox”])。计数;
foreach(文本框t位于((列表)会话[“AddedTextBox”]))
{
如果(listCount==1)
{
}
如果(listCount==2)
{
}
如果(listCount==3)
{
}
如果(listCount==4)
{
如果(t.ID==“txtCheckbox0”)
{
a=t.文本;
}
如果(t.ID==“txtCheckbox0”)
{
b=t.文本;
}
如果(t.ID==“txtCheckbox0”)
{
c=t.文本;
}
如果(t.ID==“txtCheckbox0”)
{
d=t.文本;
}
}
}
}
但是这里的问题是我没有得到文本值,它们看起来是空的。
请帮我解决这个问题。

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

问题是如何处理viewstate和回发控件的重建

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

当然,如果您想要一个黑客快捷方式,可以直接访问
请求。表单[“textCheckbox”+索引]


正如@jenson button事件所述,您可以通过
请求访问
文本框
值。表单
以下是一个示例:

ASPX:

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

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


代码隐藏:

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

        }
    }
<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;
    }
protectedvoid添加(对象发送方,事件参数e)
{
int numOfTxt=Convert.ToInt32(ddltextbox.SelectedItem.Value);
var table=新表();
对于(int i=0;ii.Contains(“Textbox”)).ToList().ForEach(i=>
{
textboxValues.Add(Request.Form[i]);
});
}