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

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

am 23.12.2007 06:53:09 von 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.

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

am 23.12.2007 10:57:01 von AdlaiMaschiach

Hi

How about "ClientID" property ?
---------
Please vote "yes"

Adlai Maschiach
http://blogs.microsoft.co.il/blogs/adlaim/


"gnewsgroup" wrote:

> 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.
>

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

am 23.12.2007 19:02:55 von gnewsgroup

On Dec 23, 4:57 am, Adlai Maschiach
wrote:
> Hi
>
> How about "ClientID" property ?
> ---------
> Please vote "yes"
>
> Adlai Maschiachhttp://blogs.microsoft.co.il/blogs/adlaim/
>

Thanks, but I don't quite understand your strategy.

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

am 25.12.2007 00:43:10 von Coskun

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





"gnewsgroup" wrote in message
news:caf6b362-58f9-43c0-af92-7f239b120d83@1g2000hsl.googlegr oups.com...
> 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.

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

am 27.12.2007 10:02:59 von nemtsev

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> wrote:
>> Hi
>>
>> How about "ClientID" property ?
>> ---------
>> Please vote "yes"
>> Adlai Maschiachhttp://blogs.microsoft.co.il/blogs/adlaim/
>>
g> Thanks, but I don't quite understand your strategy.
g>

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

am 03.01.2008 15:07:21 von Ismail

Guys,

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


///


/// this is nice little func from ryan pass it page and it
will get controls
///

///
///
///
private List ControlsByTypeUsingAncestor(Control
ancestor) where T : Control{
List controls = new List();

//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(ctrl));
}

return controls;

}

}

to get all labels you would do

foreach (Label l in ControlsByTypeUsingAncestor