0

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

If I use Page.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

blade4
  • 1
  • 1

2 Answers2

1

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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

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;
    }
jdmonty
  • 2,293
  • 1
  • 14
  • 11
  • Thanks, will ty that tomorrow. Does it matter if the control is third party? – blade4 Jul 28 '11 at 23:43
  • No it shouldn't matter if the control is a 3rd party one since all controls will at their base inherit from either WebControl or Control. – jdmonty Jul 29 '11 at 00:02