如何从自定义验证器访问嵌套在页面中的多个级别的 asp.net 控件?
具体来说,我正在生成位于占位符内的下拉列表,该占位符位于中继器内,该中继器位于另一个中继器内,该中继器位于另一个占位符内。
我需要访问所有下拉框的选定值以相互比较。
我当前的解决方案是遍历每个控件内的所有控件,直到我深入到足以访问下拉列表的:
For Each g As Control In sender.Parent.Controls
If g.GetType().ToString.Equals("System.Web.UI.WebControls.Repeater") Then
For Each k As Control In g.Controls
If k.GetType().ToString.Equals("System.Web.UI.WebControls.RepeaterItem") Then
For Each l As Control In k.Controls
If l.GetType().ToString.Equals("System.Web.UI.WebControls.Repeater") Then
For Each p As Control In l.Controls
If p.GetType().ToString.Equals("System.Web.UI.WebControls.RepeaterItem") Then
For Each n As Control In p.Controls
If n.GetType().ToString.Equals("System.Web.UI.WebControls.PlaceHolder") Then
For Each c As Control In n.Controls
If c.GetType().ToString.Equals("System.Web.UI.WebControls.DropDownList") Then
'Add the dropdownlist to an array so that I can use it after all drop down lists have been added for validation.
这似乎完全是在浪费资源。有没有更好的方法从自定义验证器访问这些控件?