Groups | Blog | Home
all groups > asp.net > january 2008 >

asp.net : Find Child Control



shapper
1/26/2008 4:34:30 PM
Hello,

I have a control named Parent in my page.
Parent has many child controls under it which also have other child
controls under them.

I need to find a control named "A" which i don't know exactly where it
is.
I just know that is under parent or under any of Parent Child
controls ...

How can I do this?

Thanks,
bruce barker
1/26/2008 5:01:48 PM
trival:

// find all child controls named "A"

Control[] list = ControlWalker(myControl, delegate(Control ctl)
{
return ctl.ID != null && ctl.ID == "A";
});

......

// control walker method

public delegate bool ControlWalkerMatcher (Control ctl);
public Control[] ControlWalker(
Control ctl,
ControlWalkerMatcher matcher)
{
ArrayList list = new ArrayList();
if (matcher(ctl)) list.Add(ctl);
for (int i=0; i < ctl.Controls.Count; ++i)
{
Control[] childList =ControlWalker(ctl.Controls[i],matcher);
if (childList.Length > 0) list.AddRange(childList);
}
return (Control[]) list.ToArray(typeof(Control));
}

-- bruce (sqlwork.com)

[quoted text, click to view]
shapper
1/28/2008 4:24:43 PM
[quoted text, click to view]

Hi,

Your code is a little bit confusing to me. Why a delegate?
Can't a single function perform the action of searching for the
control?

Thanks,
AddThis Social Bookmark Button