What is the easiest way to find a control in a parent?

G

gnewsgroup

In my user control, I would like to find a Label control in the parent
page (the page that uses my user control). I need to update that
Label.Text when something happens in the user control.

I don't want to go through the hassle of creating events in the user
control, and then let the parent handle the event.

What is the easiest way to find a control in the parent page? Right
now, I am simply manually traversing it from the user control up to
the parent page and print out the control ID until I find the one I am
looking for.

In other words, in the user control, I am looking for the control in
the parent page like this:

lblControlID.Text = this.Parent.Parent.Parent.ID.ToString();

until I have appended enough ".Parent" and get the control ID.

This is very stupid, any wise approach?

Thank you.
 
C

Coskun SUNALI [MVP]

Hi,

Have you tried writing a recursive method to find the TOP parent control and
return it back to you? I have written a sample code which is belove. I have
not compiled or tried the code so please excuse me if it does not work fine
but I hope it can give you some ideas.


Usage:

Label container = GetTopParentLabel(childControl);
if (container =! null)
{
lblControlID.Text = container.ID;
}


Required function:

private Label GetTopParentLabel(Control childControl)
{
return GetTopParentControl(childControl) as Label;
}

private Label lastParentLabel = null;
private Control GetTopParentControl(Control childControl)
{
if (childControl.Parent == null)
return lastParentLabel;

if (childControl.Parent.GetType() == typeof(Label))
lastParentLabel = (Label)childControl.Parent;

return GetTopParentControl(childControl.Parent);
}



All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
 
M

Michael Nemtsev [MVP]

Hello gnewsgroup,

he meant use the recursive function and control.ClientID to find the controls
which are locates inside other controls

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


g> On Dec 23, 4:57 am, Adlai Maschiach
g> Thanks, but I don't quite understand your strategy.
g>
 
I

Ismail

Guys,

Friend of mine sent me this recently using generics is pretty cool


/// <summary>
/// this is nice little func from ryan pass it page and it
will get controls
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ancestor"></param>
/// <returns></returns>
private List<T> ControlsByTypeUsingAncestor<T>(Control
ancestor) where T : Control{
List<T> controls = new List<T>();

//Append to search results if we match the type
if (typeof(T).IsAssignableFrom(ancestor.GetType()))
{
controls.Add((T)ancestor);
}

//Recurse into child controls
foreach (Control ctrl in ancestor.Controls)
{

controls.AddRange(ControlsByTypeUsingAncestor<T>(ctrl));
}

return controls;

}

}

to get all labels you would do

foreach (Label l in ControlsByTypeUsingAncestor<Label>(this.Page)) {
// do some stuff
}

great thing about it is that is generic you can get anything you like
just pass in the type you want!

Regards

Ismail
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top