-2

I am trying to obtain the text of a label and assign it to a string, however it does not seem to be able to find the control. Please note that this is done in the code behind for my page (the auto generation for the code behind is not functioning properly so I am having to find it manually).

public void ObtainDate()
{
    var controlList = ProductPromotion.Controls;

    foreach (Control control in controlList)
    {
        if (control is TableRow)
        {
            var newControl = control.FindControl("StartDate");
            if (newControl != null)
            {
                Label startControl = newControl as Label;
                startDate = startControl.Text;
            }
        }
    }

    Fabric.SettingsProvider.WriteSetting<string>(startDate, startSetting);
}
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
Hello World
  • 1,379
  • 4
  • 20
  • 41

3 Answers3

2

FindControl method is not recursive. Try the code of a former answer, which has been updated in a Linq style, and as an extention method:

    public static IEnumerable<TControl> FindDescendants<TControl>(this Control parent) 
        where TControl : Control
    {
        if (parent == null) throw new ArgumentNullException("control");

        if (parent.HasControls())
        {
            foreach (Control childControl in parent.Controls)
            {
                var candidate = childControl as TControl;
                if (candidate != null) yield return candidate;

                foreach (var nextLevel in FindDescendants<TControl>(childControl))
                {
                    yield return nextLevel;
                }
            }
        }
    }

Usage:

    if (control is TableRow)
    {
        var newControl = control.FindDescendants<Label>()
            .Where(ctl=>ctl.ID =="StartDate")
            .FirstOrDefault();

        if (newControl != null)
        {

            startDate = newControl.Text;

        }
    }
Community
  • 1
  • 1
Steve B
  • 36,818
  • 21
  • 101
  • 174
  • control doesn't have a FindDescendants Method – Hello World Jun 24 '13 at 14:52
  • I just have updated the code to include the missing `this` keyword to allow calling the code as an extension method. – Steve B Jun 24 '13 at 14:52
  • I'm not quite sure I understand what you've done here, as I can't actually use FindDescenands in the usage. – Hello World Jun 24 '13 at 15:11
  • This is an extension method. You have to put the method in a static class and ensure there is `using` directive that points to the namespace of the class. If you are not at ease with extension methods, you can simply add the method to the current class and call `var newControl = FindDescendants – Steve B Jun 24 '13 at 15:40
  • `Where` is also an extension method. Guessing you miss the `using System.Linq;` directive – Steve B Jun 25 '13 at 10:45
1

I'm going to guess that the "StartDate" control is nested within another control, so .FindControl isn't seeing it.

http://msdn.microsoft.com/en-us/library/486wc64h.aspx

From the documentation on Control.FindControl:

This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.

Further: For information about how to find a control when you do not know its immediate container, see How to: Access Server Controls by ID.

catfood
  • 4,267
  • 5
  • 29
  • 55
0

I suspect the control is nested and FindControl cannot see it. In which case, you would need to recursively check for it in the pages controls collection, e.g.

private Control FindMyControl(Type type, ControlCollection c, string id)
{
     Control result = null;
     foreach (Control ctrl in c)
     {
         //checks top level of current control collection, if found breaks out and returns result
         if (ctrl.GetType() == type && ctrl.ID == id)
         {
             result = ctrl;
             break;
         }
         else//not found, search children of the current control until it is - if it's not found we will eventually return null
         {
             result = FindMyControl(type, ctrl.Controls, id);

             if (result != null)
             {
                 break;
             }
         }
     }

     return result;
 }

Example usage: -

Literal myLiteral = (Literal)FindMyControl(typeof(Literal), this.Controls, "control id here");
DGibbs
  • 14,316
  • 7
  • 44
  • 83