HighTechTalks DotNet Forums  

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

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss What is the easiest way to find a control in a parent? in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
gnewsgroup
 
Posts: n/a

Default What is the easiest way to find a control in a parent? - 12-22-2007 , 11:53 PM






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.

Reply With Quote
  #2  
Old   
Adlai Maschiach
 
Posts: n/a

Default RE: What is the easiest way to find a control in a parent? - 12-23-2007 , 03:57 AM






Hi

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

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


"gnewsgroup" wrote:

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


Reply With Quote
  #3  
Old   
gnewsgroup
 
Posts: n/a

Default Re: What is the easiest way to find a control in a parent? - 12-23-2007 , 12:02 PM



On Dec 23, 4:57 am, Adlai Maschiach
<AdlaiMaschi... (AT) discussions (DOT) microsoft.com> wrote:
Quote:
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.


Reply With Quote
  #4  
Old   
Coskun SUNALI [MVP]
 
Posts: n/a

Default Re: What is the easiest way to find a control in a parent? - 12-24-2007 , 05:43 PM



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" <gnewsgroup (AT) gmail (DOT) com> wrote

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


Reply With Quote
  #5  
Old   
Michael Nemtsev [MVP]
 
Posts: n/a

Default Re: What is the easiest way to find a control in a parent? - 12-27-2007 , 03:02 AM



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> <AdlaiMaschi... (AT) discussions (DOT) microsoft.com> wrote:
Quote:
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>




Reply With Quote
  #6  
Old   
Ismail
 
Posts: n/a

Default Re: What is the easiest way to find a control in a parent? - 01-03-2008 , 08:07 AM



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

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.