在 ASP.NET 应用程序上工作,我的项目需要从页面中查找控件,使用以下语法从页面中查找控件:
public static Control FindControlRecursive(Control Root, string Id)
{
Control FoundCtl = new Control();
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
if (FoundCtl != null && FoundCtl.ID == Id)
{
Type ty = FoundCtl.GetType();
var r = FoundCtl as ty;
//var r = FoundCtl as Telerik.Web.UI.RadComboBox;
}
FoundCtl = FindControlRecursive(Ctl, Id);
//if (FoundCtl != null)
// return FoundCtl;
}
return FoundCtl;
}
为了从控件中检索控件值需要强制转换。对于演员使用波纹管语法
FoundCtl as TextBox;
是否可以按如下方式进行查找控制
Type ty = FoundCtl.GetType();
var r = FoundCtl as ty;