0

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;
Nimantha
  • 6,405
  • 6
  • 28
  • 69
shamim
  • 6,640
  • 20
  • 85
  • 151
  • 1
    You can't convert System.Object/Web.Control to something using target's own information. Type in cast should be specified in sour code, the same as generic constraint. – abatishchev Dec 06 '12 at 17:31

2 Answers2

1

The most proper way is the following:

TextBox textBox = FindControl("name") as TextBox;
if (textBox != null)
{
    // use it
}

Why doesn't it work for you?


Also you can use an extension method to find controls of given type recursively:

public static IEnumerable<Control> GetChildControls(this Control control)
{
    var children = (control.Controls != null) ? control.Controls.OfType<Control>() : Enumerable.Empty<Control>();
    return children.SelectMany(c => GetChildControls(c)).Concat(children);
}

Usage:

var textBoxex = this.GetChildControls<TextBox>();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • abatishchev,thanks for reply ,I know how to cast a control ,hope you see in my question description ,want to cast control on run time,in my project need to find several time so many controls, so decide to make a common method which find a control and return me a specific control object ,suppose if I send a control id which type=dropdown then it’s return me this control specific dropdown object – shamim Dec 06 '12 at 06:03
  • @shamim: Do you have a control to find as a `System.Type`? Then use [IsAssignableFrom()](http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx). – abatishchev Dec 06 '12 at 07:08
  • But more probably you may want to use compile-type generic: `Controls.OfType where T : Control`, i.e. `FindControl(id)`. – abatishchev Dec 06 '12 at 07:10
0

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?

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38