我有50个文本框从TextBox1开始,直到TextBox50。我想从所有这50个文本框中检索值。我尝试循环但失败。我想要一些代码,如TextBox(i).Text,其中我从1到50变化。 循环应产生以下结果。 Response.Write(TextBox1.Text); Response.Write(TextBox2.Text); 儿子直到 Response.Write(TextBox50.Text);从C#中的文本框中检索文本的循环文本框ID
我该如何做到这一点?
我有50个文本框从TextBox1开始,直到TextBox50。我想从所有这50个文本框中检索值。我尝试循环但失败。我想要一些代码,如TextBox(i).Text,其中我从1到50变化。 循环应产生以下结果。 Response.Write(TextBox1.Text); Response.Write(TextBox2.Text); 儿子直到 Response.Write(TextBox50.Text);从C#中的文本框中检索文本的循环文本框ID
我该如何做到这一点?
您可以使用FindControl
这需要一个字符串作为参数,通过它"TextBox" + i
喜欢:
TextBox tb = this.FindControl("TextBox" + i) as TextBox;
if (tb != null)
{
Response.Write(tb.Text);
}
+1,尼斯解决方案!我正在建议制作一个阵列。 – gleng
可以循环以为他们只是为:
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
}
}
'我试过循环,但它failed'请显示你的代码。你是什么意思“失败”?编译错误?运行时异常?意外的输出?更加详细一些。 – tnw