views:

21

answers:

3
+3  Q: 

page child control

asp.net page is also a control. how can I access child control within page control?

this.page.?
+5  A: 

Try this :

Control childControl = Page.FindControl("YourControlsID");
Canavar
Be careful with this method though because it is not recursive to child controls. You will need to be sure that the control your are looking for is actually a child of the page and not a child of a panel or some other child control within the page.
Joel Etherton
+1 and it might also be important to note that this will only work for first order child controls and their siblings. If you want to find nested controls, you would need to use recursion.
Josh
can you provide recursion code?Does Page.Contorls contain first order children?
Novice Developer
+3  A: 

You can access it via the Controls Collection

Page.Controls

Recursive FindControls from Rick Strahl's Blog

public static Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;

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

    return null;
}

Be careful with this however... This is not a method you want to be using inside a loop or anything.

Josh
+4  A: 
  • Page.Controls
  • FindControl method
nickyt