【发布时间】:2016-05-30 09:43:21
【问题描述】:
在经典 ASP 中,我可以在循环通过未知输入字段时执行此操作:
<input id="textbox1" type="text">
<input id="textbox2" type="text">
<input id="textbox3" type="text">
<input id="textbox4" type="text">
<input id="textbox5" type="text">
For i = 1 To 5
strTextbox = request.form("textbox" & i)
If strTextbox <> "" Then
// Do the magic!
End If
Next
这样,用户可以在文本框 1、3、4 和 5 中输入值,或者可能只在 1 和 2 中输入值,我可以在 For 循环中收集输入值。
我怎么能在 C# 中做到这一点?
我不能这样做,因为我在我的 textbox.Text 中间添加了一个 i;
for (int i = 1; i < 6; i++)
{
strTextbox = textbox[i].Text;
if (!string.IsNullOrEmpty(strTextbox)
{
// Do the magic!
}
}
我现在有很多 if:s 检查循环内的每个文本框,但它必须是一种更简单的方法?
【问题讨论】: