For most people this will not be an issue at all. Most people don’t care about the control hierarchy of the Page, but to make highly-customized pages, you’ll get to know this very intimately. In v1.1, every control on the page was in the Page’s Controls collection, so if you needed to get a reference to a user control, for instance, you could declare the user control as a member variable or you could find it at runtime:
(MyUserControl)Page.FindControl(”ucMyUserControl”);
I’ve seen this method in plenty of code samples around the .Net, and I’ve even used it myself.
In version 2.0 (as it stands in Beta 1), if you user a Master Page along with your Page, this will not work. Let me tell you why. When you apply a master page, that master page has a ContentPlaceHolder control, and your page has only an <asp:Content/> control. This Content control get stripped and discarded and never makes it to the final Page control structure. The contents of the Content control are merged into the ContentPlaceHolder. The result is that the page ends up with only 1 control in its Controls collection: a ContentPlaceHolder from the master page. That leaves you to modify the above code to :
(MyUserControl)Page.Controls[0].FindControl(”ucMyUserControl”);
or you could otherwise find the ContentPlaceHolder first and then do the FindControl().
I don’t know the reasoning for this change. I would reason that the <asp:Content/> controls should merely be added to the controls collection of the master’s ContentPlaceHolder. This would result in less rearranging of the Page’s control hierarchy.
Overall, the master page feature is very nice. The IDE provides great RAD features for this, but my team develops an enterprise application that HAS to be maintainable, so RAD goes out the window. The master page is set on the PreInit method in our base page, and by the Load method, everything is where it should be ready to be used.
How to find control if having nested master page?
if required to find linkbutton on page A.aspx which uses M1 as master page and this master page M! again uses Main master page.
This looks close to what Im trying to do, but its still not working. Im using a user control with a data grid in a web part on my main form. During the ItemBound event, id like to dim an instance of an image thats in my grid, then based on some criteria, make the image visible or not. Only problem is, I cant find the image in ANY control collection anywhere! This is what Im doing.
Protected Sub dgrOutOfOffice_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dgrOutOfOffice.ItemDataBound
Dim imgOutofOffice As Image = CType(e.Item.FindControl(“imgInfo”), Image)
Could you help me figure out where in the control hierarchy I can find my image? I tried:
e.item.Control(0).FindControl(“imgInfo”)
as well, but no luck.
Id appreciate any help you could offer. I know you prefer C#, I appologize.
Thank you very much for this information!
In VS2005 RTM i tried this and it works too:
MyUserControl mUc = (MyUserControl)this.myUserControl;
The control e. g. myUserControl will be displayed in intellisense list.
Well in Beta 2 seems to have changed again. To find a control on the real page I found this solution:
Page.Controls[0].FindControl(ContentPlaceHolderID).FindControl(Control)
hrm.
thanks for the tip, this was beggining to drive me batty.
it makes sense but it would be nice if the msdn pages that explain master pages and how to implement made reference to this (i started trying them out after reading a page regarding new features in ado v2)!
okay, i spoke way too soon.
i couldn’t access any of the real pages controls from the 0 control of page.
i had to use Parent.FindControl(ControlToPaginate) which actually makes a lot more sense.
however it would be nice then to have some generic function like in win32 i can find child windows recursively with switches. its easy to write but….
i care. thanx man.
so much for generic control compatibility.
i’m enjoying 2.0 there’s no looking back,
anyway.