Internet Handholding posted on March 09, 2010 19:27

When controls are initialized on an asp.net page, the Page object may not be available. You get an error message that says: Object reference not set.
After initialization, during the Load phase, the Page object is available.
What if you need the Page object during the initialization phase, such as in the CreateChildControls method?
Derive a class from the Page class. During the PreInit event, save a reference to the Page object in HttpContext.Current.Items.
You can then retrieve the reference whenever you need it, even during initialization of controls.
public static string http_context_page = "web_page";
HttpContext.Current.Items.Add(http_context_page, this); // Save
Page page = (Page)HttpContext.Current.Items[http_context_webpage]; // Retrieve
By Andrew Weitzen, Bronze Inc. (c) 2010
Bronze is the publisher of several online Internet journals including: InternetHandholding.com, DomainNames.gs, DotNetNuke.bz, Programmer.bz, Software.vg, WebHosting.vg