By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Dec 23 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Dec 23 '07 #2

P: n/a
On Dec 23, 4:57 am, Adlai Maschiach
<AdlaiMaschi...@discussions.microsoft.comwrote:
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.
Dec 23 '07 #3

P: n/a
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" <gn********@gmail.comwrote in message
news:ca**********************************@1g2000hs l.googlegroups.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.
Dec 24 '07 #4

P: n/a
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
gOn Dec 23, 4:57 am, Adlai Maschiach
g<AdlaiMaschi...@discussions.microsoft.comwrote:
>Hi

How about "ClientID" property ?
---------
Please vote "yes"
Adlai Maschiachhttp://blogs.microsoft.co.il/blogs/adlaim/
gThanks, but I don't quite understand your strategy.
g>
Dec 27 '07 #5

P: n/a
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<TControlsByTypeUsingAncestor<T>(Control
ancestor) where T : Control{
List<Tcontrols = 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>(c trl));
}

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
Jan 3 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.