开发者

Using Page.FindControl() to get an instance of a (third party) control keeps returning null

I have an ASP.NET site and on one page, there are a couple of controls (third party - Telerik).

If I use P开发者_JS百科age.FindControl(), and pass in the ID of the control (Which is spelt correctly), this always returns null. Why?

This is on an .aspx page, and the controls are not in a control of itself etc. Can't remember if there is a masterpage or, so assume yes and no for any possible answers.

How could I programatically get an instance of the control?

Thanks


You need the reference to the control's NamignContainer if you want to use FindControl. If you don't know (or you don't have a reference to) it you must loop the control-tree recursively:

For example (as extension):

namespace ExtensionMethods
{
    public static class ControlExtensions
    {
        public static Control FindControlRecursive(Control rootControl, string controlID)
        {
           if (rootControl.ID == controlID) return rootControl;

           foreach (Control controlToSearch in rootControl.Controls)
           {
              Control controlToReturn = FindControlRecursive(controlToSearch, controlID);
              if (controlToReturn != null) return controlToReturn;
           }
           return null;
        }
    }
}

Use it this way:

 using ExtensionMethods;
 //.....
 Control ctrl = this.FindControlRecursive("myControlID");

But it would be better to use FindControl if you know the NamingContainer because:

  1. it's faster
  2. it's more legible
  3. it's less error-prone: a control's ID just need to be unique inside of it's NamingContainer, hence you might get the wrong control with the recursive method(consider a GridView with it's rows and a Control inside of a TemplateField)
  4. you learn more how an ASP.NET page works and controls are linked

More informations: MSDN How to: Access Server Controls by ID


The control you are attempting to find is most likely nested lower in the control tree. You can use the following code to recursively traverse the tree and find your control no matter how far or close it is nested in the control hierarchy

 Control c =  FindControl<Control>(Page,"controlIDStringLiteral");

 public static T FindControl<T>(ControlCollection controls, string controlId) where T : Control
    {
        if (controls == null)
        {
            throw new ArgumentException("controls null");
        }

        if (string.IsNullOrEmpty(controlId))
        {
            throw new ArgumentException("controlId is null or empty");
        }

        if (controls.Count < 1)
        {
            return null;
        }

        Control retval;
        foreach (Control control in controls)
        {
            if (control.ID == controlId)
            {
                return control as T;
            }
        }

        foreach (Control control in controls)
        {
            retval = FindControl<T>(control, controlId);
            if (retval != null)
            {
                return retval as T;
            }
        }
        return null;
    }


    /// <summary>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="control"></param>
    /// <param name="controlId"></param>
    /// <returns></returns>
    public static T FindControl<T>(Control control, string controlId) where T : Control
    {
        if (control == null)
        {
            throw new ArgumentException("control null");
        }

        if (control.Controls == null)
        {
            return null;
        }

        if (control.Controls.Count < 1)
        {
            return null;
        }

        Control retval;
        retval = control.FindControl(controlId);
        if (retval != null)
        {
            return retval as T;
        }

        foreach (Control childControl in control.Controls)
        {
            retval = FindControl<T>(childControl, controlId);
            if (retval != null)
            {
                return retval as T;
            }
        }

        return null;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜