Last post Jan 23, 2010 06:23 PM by jkirkerx
Jan 22, 2010 04:43 PM|FancyKetsup|LINK
I have a custom control that is going to attach itself to a control somewhere on the page. There is a property for assigning the ID of the control to attach to. Now the only way I can think to find the control it's attaching to is...
1.) Use this.Parent and check which will work sometimes(and various versions ie this.parent.parent)
2. Recursively search the Page.Controls till I find it.
Now I was thinking of setting it to check 3-4 of the most likely paths 1st and if it isn't found then do option 2 above.
My question is surely someone else has had to tackle this and if you don't mind would you share how you approached it.
Jan 22, 2010 07:40 PM|jkirkerx|LINK
Attach and do what?
Did you write the parent control?
Jan 22, 2010 08:07 PM|FancyKetsup|LINK
It will attach to various asp.net controls and other custom controls they may have built, Grids List, dropdowns etc. Using this.Parent.FindControl will work for most uses, probably 60% or more
of the time. But then there is always the case where someone wants to attach it to a control from say an ascx control and my control may be on a totally different part of the parent page.aspx. In the later case finding the target to attach to gets tricky,
so I was wondering how someone else would approach that. Like I said above the only way I can think of is to recursively walk the control hierarchy till I find it which I really don't like. I could force them to pass a ref of the control they want to attach
to but that defeats the spirit of asp.net server controls.
Jan 23, 2010 06:23 PM|jkirkerx|LINK
I doubt you'll get the anwser here. That's probably a closely held secret by the few that have accomplished it. There's alot of stuff I have that I had to reverse engineer to figure out, or just trial and error for a long time until I get it. One of them
took me 2 years before I figured it out, while working on something else.
I use to use findcontrol, but found it troublesome in some situations, so I dropped it and transfered the feature to the master or parent control that I wrote.