I have Main.Master, an inner master page Inner.Master, and abc.aspx. In Inner.Master I have a dropdownlist ddlChildren which I populate on Inner.Master page load.
I would like to, on loading page abc.aspx, I get access to ddlChildren from its master page, Inner.Master:
int x = Int32.Parse(((DropDownList)this.Parent.FindControl("ddlChildren")).SelectedValue);
I am doing this on page load of abc.aspx. The problem is this control is not found. I think this happens becuase the control is not even loaded yet in Inner.Master, because when I do:
this.Parent.Controls.Count
at debugging, I only have 1 control for Inner.Master, which its Main.Master.
I then tried to call ddlChildren onPreRender, but the same Happens.
According to http://msdn.microsoft.com/en-us/library/ms178472.aspx, controls are loaded at pre render, but the on pre render of inner master is called AFTER abc.aspx. So can I do this at another page stage?
Or am I completely off track?
Visual breakdown of the lifecycle, including master pages.
OK, I think there is more going on here.
First of all
FindControlisn’t recursive, unless what you are looking for is a direct child of the object in question, you won’t find it.You’ll need a function something like this (not tested, but I’ve written something like it before):
Second, you’re assuming
Parentof your page is the master page. This may not be true. TryPage.MasterPagedetailed here.So, to conclude, try this:
One more thing. It’s usually good to consider that if you find yourself accessing controls that exist in a level above your current Page scope, you may want to rethink your design.