If you’ve read my previous post explaining a common pitfall with view state, I’d hope you’re preparing all your controls in the Init event of the page/control lifecycle.
Even if I’m not reusing them through my application much, I like to factor elements like a drop down list of countries into their own control. This centralizes their logic and allows us to write clear, succinct markup like this:
<tat:CountriesDropDownList ID="AddressCountry" runat="server" />
The code for a control like this is quite simple:
[ToolboxData("<{0}:CountriesDropDownList runat=\"server\" />")]
public class CountriesDropDownList : DropDownList
{
protected override void OnInit(EventArgs e)
{
DataSource = Countries;
DataBind();base.OnInit(e);
}
}
The Problem
Once you start using this encapsulation technique, it won’t be long until you want to pass in a parameter that affects the data you load. Before we do, we need to be aware that the Init event is fired in reverse order. That is, the child controls have their Init event fired before that event is fired at the parent. As such, the Page.Init event is too late for us to set any properties on the controls.
The natural solution is to try and use the Page.PreInit event, however when you do you’ll often find that your control references are all null. This happens when your page is implemented using a master page, and it relates to how master pages are implemented. The <asp:ContentPlaceHolder /> controls in a master page use the ITemplate interface to build their contents. This content (child controls) is not usually prepared until the Init event is called, which means the control references are not available. For us, this represents a problem.
The Solution
The fix is remarkably simple; all we need to do is touch the Master property on our Page and it will cause the controls to become available. If we are using nested master pages, we need to touch each master page in the chain.
I often create a file called PageExtensions.cs in my web project and add this code:
public static class PageExtensions
{
/// <summary>
/// Can be called during the Page.PreInit stage to make child controls available.
/// Needed when a master page is applied.
/// </summary>
/// <remarks>
/// This is needed to fire the getter on the top level Master property, which in turn
/// causes the ITemplates to be instantiated for the content placeholders, which
/// in turn makes our controls accessible so that we can make the calls below.
/// </remarks>
public static void PrepareChildControlsDuringPreInit(this Page page)
{
// Walk up the master page chain and tickle the getter on each one
MasterPage master = page.Master;
while (master != null) master = master.Master;
}
}
This adds an extension method to the Page class, which then allows us to write code like the following:
protected override void OnPreInit(EventArgs e)
{
this.PrepareChildControlsDuringPreInit();MyCustomDropDown.MyProperty = "my value";
base.OnPreInit(e);
}
Without the call to the extension method, we would have received a NullReferenceException when trying to set the property value on the MyCustomDropDown control.
You now have one less excuse for preparing your controls during the Load event. 🙂
Thank you, this has helped me
your the man….
Genius!!
Hi Tatham, I’m a bit of a newbie…I’m wondering how to store/query the current value of a control, or it’s visibility, during PreInit/Init.
My problem is that I want to add a trigger if a certain condition is met. I can only seem to query current values in the Load event, but I need to add the control in the Init event. How can I work around this problem?
Thanks and Regards,
Nathan
Hi Nathan,
You should have a read of these two posts:
http://blog.tatham.oddie.com.au/2008/12/18/how-i-learned-to-stop-worrying-and-love-the-view-state/
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
Hope they help,
– Tatham
Hi Tatham,
Iam a newbie too. You please help me understand how the controls would become available, by going up the master page chain.
Thanks and Regards,
-Learner.
Hi Tatham,
This was very useful! Thanks a lot 🙂
Kardo