C# 如何从动态创建的文本框中获取文本以存储在数据库中?

C# 如何从动态创建的文本框中获取文本以存储在数据库中?,c#,asp.net,C#,Asp.net,我有一个页面,其中n个文本框是根据下拉列表中的值n创建的。我的问题是如何将文本框中的值访问到字符串变量中,以便将它们存储在数据库中。 下面是创建文本框的代码 protected void ddlNumOfVolunteers_SelectedIndexChanged(object sender, EventArgs e) { try { // Get the number of labels to create. int numlabels

我有一个页面,其中n个文本框是根据下拉列表中的值n创建的。我的问题是如何将文本框中的值访问到字符串变量中,以便将它们存储在数据库中。 下面是创建文本框的代码

 protected void ddlNumOfVolunteers_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {


        // Get the number of labels to create.
        int numlabels = System.Convert.ToInt32(ddlNumOfVolunteers.SelectedItem.Text);
        for (int i = 1; i <= numlabels; i++)
        {
            Label myLabel = new Label();
            TextBox txtbox = new TextBox();
            // Set the label's Text and ID properties.
            myLabel.ID = "LabelVol" + i.ToString();
            myLabel.Text = "Volunteer " + i.ToString();
            txtbox.ID = "TxtBoxVol" + i.ToString();
            PlaceHolder1.Controls.Add(myLabel);
            PlaceHolder2.Controls.Add(txtbox);
            // Add a spacer in the form of an HTML <br /> element.
            PlaceHolder2.Controls.Add(new LiteralControl("<br />"));
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        } 
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }
}

提前感谢

有很多方法可以做到这一点

但是,您可以将它们存储在
列表的
数组中,甚至可以存储在
字典中

Dictionary<string,Textbox> myControls = new Dictionary<string,Textbox>();

...

TextBox txtbox = new TextBox();
txtbox.Id = "TxtBoxVol" + i.ToString();
myControls.Add(txtbox.Id,txtbox)

...

var myValue = myControls["TxtBoxVol1"].Text 


// when you are finished 
myControls.Clear();
Dictionary myControls=new Dictionary();
...
TextBox txtbox=新建TextBox();
txtbox.Id=“TxtBoxVol”+i.ToString();
myControls.Add(txtbox.Id,txtbox)
...
var myValue=myControls[“TxtBoxVol1”].Text
//当你完成的时候
myControls.Clear();
只需使用数组或列表来存储实例,任务完成,现在只需迭代数组即可。
Dictionary<string,Textbox> myControls = new Dictionary<string,Textbox>();

...

TextBox txtbox = new TextBox();
txtbox.Id = "TxtBoxVol" + i.ToString();
myControls.Add(txtbox.Id,txtbox)

...

var myValue = myControls["TxtBoxVol1"].Text 


// when you are finished 
myControls.Clear();