我有 50 个文本框,从 TextBox1 到 TextBox50。我想从所有这 50 个文本框中检索值。我尝试循环但失败了。我想要一些类似 TextBox(i).Text 的代码,其中 i 从 1 变化到 50。循环应该产生以下结果。Response.Write(TextBox1.Text); Response.Write(TextBox2.Text); 儿子直到 Response.Write(TextBox50.Text);
我怎样才能做到这一点?
您可以使用FindControl
which 将字符串作为参数,"TextBox" + i
像这样传递它:
TextBox tb = this.FindControl("TextBox" + i) as TextBox;
if (tb != null)
{
Response.Write(tb.Text);
}
您可以简单地循环思考它们,如下所示:
string value=""; // store each textbox value in this variable
foreach (Control x in this.Controls) // loop through the controls in the form
{
if (x is TextBox) // if the program found a textbox in the form
{
value = (x as TextBox).Text; // set the value of the textbox number x to the string value
listBox1.Items.Add(value); // here is a listbox to show the resule
}
}