我有一个登录控件,并且在标题控件中嵌套了 2 层,即页面 --> 标题控件 --> 登录控件。我无法使用 FindControl 获得对页面上控件的引用。我希望能够设置控件的可见属性,例如
if (_loginControl != null)
_loginControl.Visible = false;
我最终使用递归 FindControl 方法来查找嵌套控件。
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}