我目前在 ascx 控件中有一个下拉菜单。我需要从同一页面上的另一个 ascx 后面的代码中“找到”它。它的值用作 ascx #2 上 ObjectDataSource 的参数。我目前正在使用这段丑陋的代码。它有效,但我意识到如果控制顺序发生变化或发生其他各种事情,它就不会是我所期望的。有人对我应该如何正确地做到这一点有任何建议吗?
if(Page is ClaimBase)
{
var p = Page as ClaimBase;
var controls = p.Controls[0].Controls[3].Controls[2].Controls[7].Controls[0];
var ddl = controls.FindControl("ddCovCert") as DropDownList;
}
谢谢,新年快乐!
〜ck在圣地亚哥
通常,当您需要查找大量控件时,我会实现“FindInPage”或递归 FindControl 函数,您只需向其传递一个控件,它就会递归地沿控件树下降。
如果这只是一次性的事情,请考虑在 API 中公开所需的控件,以便您可以直接访问它。
public static Control DeepFindControl(Control c, string id)
{
if (c.ID == id)
{
return c;
}
if (c.HasControls)
{
Control temp;
foreach (var subcontrol in c.Controls)
{
temp = DeepFindControl(subcontrol, id);
if (temp != null)
{
return temp;
}
}
}
return null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)