我的 xaml 中有一个字段集,它定义了标签和文本框的负载。然而。我无法使用递归访问文本框。
这是我的 xaml 中字段集的片段:
<fieldset runat="server" id="fdst1" class="default_form">
<dl>
<dt>
<asp:Label ID="lblCustomerId" Text="Customer ID" runat="server" />
</dt>
<dd>
<asp:TextBox AutoPostBack="true" OnTextChanged="TextBox_OnTextChanged" ID="txtCustomerId"
Enabled="false" runat="server" />
</dd>
</dl>
<dl>
<dt>
<asp:Label ID="Label1" Text="Associated Brand" runat="server" />
</dt>
<dd>
<asp:TextBox AutoPostBack="true" OnTextChanged="TextBox_OnTextChanged" ID="txtBrandName"
Enabled="false" runat="server" />
</dd>
</dl>
现在这是我用来获取文本框控件的代码...
private List<DropDownList> GetDropDownLists()
{
List<DropDownList> controls = new List<DropDownList>();
FindControls<DropDownList>(this.Controls, controls);
return controls;
}
/// <summary>
/// this will find all the contols in a collection of a given type.
/// </summary>
/// <typeparam name="T">Type of control to find</typeparam>
/// <param name="Controls">Contol Collection to look through</param>
/// <param name="foundControls">List of found controls</param>
public static void FindControls<T>(ControlCollection Controls, List<T> foundControls) where T : class
{
T found = default(T);
if (Controls != null && Controls.Count > 0)
{
for (int i = 0; i < Controls.Count; i++)
{
if (Controls[i] is T)
{
found = Controls[i] as T;
foundControls.Add(found);
break;
}
else
// Recursive method call.
FindControls<T>(Controls[i].Controls, foundControls);
}
}
}
现在返回的列表是空的,但我正在传递页面。控件应该在其中的控件集合。如果我使用表格,这一切都很好,但我的老板坚持认为它在字段集中。所以我的问题是如何在使用字段集时使用递归来获取这些文本框。