Find Control

S

shapper

Hello,

I have a custom control, MyCustomControl, with a property named Childs
of type Generic.List(Of MyChildControl).

I added a MyCustomControl to my page and added a few MyChildControls
to MyCustomControl Child properties.

How can I later find a MyChildControl in Childs given its ID?

Do I need to implement any custom method in MyCustomControl?

Thanks,

Miguel
 
D

DMG

You'll need a recursive method to go through the child controls, as each of
these can also host children.

I nabbed the following methods for use in my own app a while back (don't
thank me for the code!) :

public static Control FindControl(string id, Control panel)
{
foreach (Control c in panel.Controls)
{
Control child = FindControlRecursive(c, id);
if (child != null)
{
return child;
}
}
return null;
}

private static Control FindControlRecursive(Control root, string
name)
{
if (root.Name == name)
{
return root;
}

foreach (Control c in root.Controls)
{
Control rc = FindControlRecursive(c, name);
if (rc != null)
{
return rc;
}
}
return null;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top