如果你把表单用动态的table来排版,就可以来遍历控件了。
先看页面代码:
<table id="Table1" runat="server" style="width: 221px">
<tr>
<td style="width: 60px">Name</td>
<td><asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 60px">From</td>
<td><asp:TextBox ID="FromTextBox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 60px">To</td>
<td><asp:TextBox ID="ToTextBox" runat="server"></asp:TextBox></td>
</tr>
</table>
先是保存到各自的 ViewState
foreach (HtmlTableRow row in Table1.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
Control control = cell.Controls[0];
if (control is TextBox)
{
ViewState[control.ID] = ((TextBox)control).Text;
}
}
}
foreach (HtmlTableRow row in Table1.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
Control control = cell.Controls[0];
if (control is TextBox)
{
string value = (string)ViewState[control.ID];
if (value != null)
{
((TextBox)control).Text = value;
}
}
}
}