Working on ASP.NET app, my project need to find control from page ,use bellow syntax to find control from a page:
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;
}
For retrieve control value from the control need to cast. For cast use bellow syntax
FoundCtl as TextBox;
Is it possible to cast find control as bellow
Type ty = FoundCtl.GetType();
var r = FoundCtl as ty;
You cannot do this in such way. All cast operators is not work with variable of type System.Type . Moreover, if you want to work with this control in runtime with reflection, you can use reflection methods to work with it (such as PropertyInfo.SetValue
etc).
But usually you exatly know what type is of concrete control. Why do you want to cast in runtime?