Find Child Control

Find Child Control

am 27.01.2008 01:34:30 von Shapper

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,
Miguel

Re: Find Child Control

am 27.01.2008 02:01:48 von DFS

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)

shapper wrote:
> 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,
> Miguel

Re: Find Child Control

am 29.01.2008 01:24:43 von Shapper

On Jan 27, 1:01 am, bruce barker wrote:
> 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)
>
> shapper wrote:
> > 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,
> > Miguel

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,
Miguel