2
ASP.NET 2.0 Web窗体如何遍历用户控件中的控件以找到某个控件?
那么如何遍历用户控件中的所有控件并找到某种类型的控件并向其添加事件?
我有类似的问题How do I add a event to an ASP.NET control when the page loads?处理添加事件 - 但这是不同的,如果我想找到一个控件。
SCENARIO
控制是一个定制的控制:
<asp:Repeater runat="server" ID="options" OnItemDataBound="options_OnItemDataBound">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
</HeaderTemplate>
<ItemTemplate>
<td>
<span>
<asp:Label runat="server" ID="optionName">
</asp:Label>
<asp:DropDownList runat="server" ID="optionValues" CssClass="PartOption">
</asp:DropDownList>
</span>
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
上的用户控制的定制控制声明:
<td><def:CustomControl id="somePartOptions" runat="server"></td>
在用户控制的后面的代码,我在Page_Load事件中尝试了以下内容:
foreach(Control control in partOptions.Controls) {
FindDropDownControl(control);
}
protected void FindDropDownControl(Control controlContainer) {
bool isRepeater = false;
if (controlContainer is Repeater) {
isRepeater = true;
}
if (controlContainer.HasControls()) {
foreach (Control subControl in controlContainer.Controls) {
FindDropDownControl(subControl);
}
}
}
但是,布尔标志始终为false。那么我在做什么?我最终想在转发器的itemTemplate中找到dropdownlist控件,但我甚至找不到转发器。
感谢,
可能的重复:http://stackoverflow.com/questions/619449/find-a-control-in-a-webform – Marcel 2013-10-17 08:10:46