find a control in current page

こ雲淡風輕ζ 提交于 2019-11-29 16:17:24

If a control is nested inside other controls, you need to find it recursively.

Here is a helper method. It searches a control recursively.

Helper Method

public static Control FindControlRecursive(Control root, string id)
{
   if (root.ID == id) 
     return root;

   return root.Controls.Cast<Control>()
      .Select(c => FindControlRecursive(c, id))
      .FirstOrDefault(c => c != null);
}

Usage

var myControl =  (MyControl)FindControlRecursive(Page, "ViewMeetingActions"); 

First, recursively loop through the controls on the page. Using the following helper class:

using System.Web.UI;

public class ReflectionHelper
{

/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <returns>Control if found, null if not found</returns>
/// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static Control FindControlRecursive(Control ParentControl, string ControlId)
{
    if (ParentControl.ID == ControlId) {
        return ParentControl;
    }

    foreach (Control Ctl in ParentControl.Controls) {
        Control FoundCtl = FindControlRecursive(Ctl, ControlId);
        if ((FoundCtl != null)) {
            return FoundCtl;
        }
    }
    return null;
}

/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID.  Attempt to Invoke() Control method.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <param name="MethodName"></param>
/// <param name="parameters"></param>
/// <returns>bool true if executed, bool false if error or not executed</returns>
/// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static bool FindControlRecursiveAndInvokeMethod(Control ParentControl, string ControlId, string MethodName, object[] parameters)
{
    var ctrl = FindControlRecursive(ParentControl, ControlId);

    if (ctrl != null)
    {
        try
        {
            MethodInfo[] ctrlMethods = ctrl.GetType().GetMethods();
            foreach (MethodInfo method in ctrlMethods)
            {
                if (method.Name == MethodName)
                {
                    method.Invoke(ctrl, parameters);
                    return true;
                }
            }
            //return false;
        }
        catch (System.Exception)
        {
            //return false;
        }
    }
    else
    {
        //return false;
    }

    return false;
}

/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID.  Attempt to set SetValue() on Control property.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <param name="PropertyName"></param>
/// <param name="value"></param>
/// <returns>bool true if executed, bool false if error or not executed</returns>
/// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static bool FindControlRecursiveAndSetPropertyValue(Control ParentControl, string ControlId, string PropertyName, string value)
{
    var ctrl = FindControlRecursive(ParentControl, ControlId);

    if (ctrl != null)
    {
        try
        {
            PropertyInfo[] ctrlProperties = ctrl.GetType().GetProperties();
            foreach (PropertyInfo property in ctrlProperties)
            {
                if (property.Name == PropertyName)
                {
                    property.SetValue(ctrl, value, new object[0]);
                    return true;
                }
            }
            //return false;
        }
        catch (System.Exception)
        {
            //return false;
        }
    }
    else
    {
        //return false;
    }

    return false;
}

/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID.  Attempt to set SetValue() on Control property.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <param name="PropertyName"></param>
/// <param name="value"></param>
/// <returns>bool true if executed, bool false if error or not executed</returns>
/// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static bool FindControlRecursiveAndSetPropertyValue(Control ParentControl, string ControlId, string PropertyName, int value)
{
    var ctrl = FindControlRecursive(ParentControl, ControlId);

    if (ctrl != null)
    {
        try
        {
            PropertyInfo[] ctrlProperties = ctrl.GetType().GetProperties();
            foreach (PropertyInfo property in ctrlProperties)
            {
                if (property.Name == PropertyName)
                {
                    property.SetValue(ctrl, value, new object[0]);
                    return true;
                }
            }
            //return false;
        }
        catch (System.Exception)
        {
            //return false;
        }
    }
    else
    {
        //return false;
    }

    return false;
}

}

Second, use the class to get the Control Ref:

Control ctrlActionMemberDropdown = ReflectionHelper.FindControlRecursive(this.Page, "action_member_dropdown");

Third, insert the row into the DropDownList Control:

ctrlActionMemberDropdown.Items.Insert(0, "<-- Select -->");

Thanks,

Page.Controls gives you just the collection of the topmost controls in the control hierarchy. The WebForm is itself a control, and contains many other controls. You will need to walk this hierarchy to see the entire collection of controls.

The FindControl method should find the control you are looking for. Can you share more code with us to demonstrate the problem?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!