我目前在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;
}
谢谢,新年快乐!
〜在圣地亚哥
如果它只是一次性的事情,请考虑在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;
}
