Get reflection control

I need to get a control [text box, shortcut, button ... etc.] from my page using ID using reflection

In my case, I have a class that inherits all other system pages, and this class overrides the onload event to change some properties to some controls.

how to set the visibility state for a text field by its name, but since I do not have direct control on the page, since I can have it on the content holder on the main page, so I could not use the findcontrol method, I also think that it is a recursive function it takes too long to find the control

So, I am trying to do this with a reselection to find the control with its name and then change its visible or enabled state

I used the FieldInfo class but did not work with me

FieldInfo fi = this.GetType().GetField("ControlID", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);

Any help!

+3
source share
3 answers

Have you tried FindControl

  Control myControl = this.FindControl("TextBox2");
  if(myControl !=null)
  {
       // Do Stuff
  }
+7
source

Perhaps you need to recursively look for your control using FindControl and then call yourself?

private static Control FindNestedControl(string id, Control parent)
{
    Control item = parent.FindControl(id);

    if(item == null) 
    {
        foreach(var child in parent.Controls)
        {
            item = child.FindNestedControl(id, child);

            if(item != null)
            {
                return item;
            }
        }
    }

    return null;
}

And call it by passing the current instance of the page with:

Control item = FindNestedControl("bob", this);

This can be slow, so pay attention to this if there is a whole group of controls on the page :)

Another way to do this is to simply set the controls as properties in the base class:

public abstract class BasePage : Page
{
    #region Properties

    /// Gets the textbox for editing in derived classes.
    protected TextBox SomeTextBox
    {
       return this.textBox; }
    }

    #endregion

}

- :

public abstract class BasePage : Page
{
        /// <summary>
        /// Called when loading the page.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Do some basic setup.

            // Now pass the controls to the derived classes.
            this.ConfigureControl(this.mainText);
            this.ConfigureControl(this.nameDropDown);
            this.ConfigureControl(this.someOtherControl);            
        }

        /// <summary>
        /// Provides hook for derived classes.
        /// </summary>
        /// <param name="control">The core control.</param>
        protected virtual void ConfigureControl(Control control)
        {
        }
 }

:

protected override void ConfigureControl(Control control)
{
    switch(control.ID) 
    {
        case "mainText":
        TextBox mainText = (TextBox)control;
        mainText.Text = "works";
        break;
    }
}

, , , . ..

+1

Here's a very useful extension method for recursively managing child elements:

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);
}

Using:

IEnumerable<Control> allChildren = parent.GetChildControls();
0
source

All Articles