You are here:   Articles
Register   |  Login

Programming Articles

Minimize
09

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

Posted in: Asp.net

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Name (required)

Email (required)

Website

Programmer Newsletter

Minimize

Subscribe to the Internet Handholding newsletter



Reccomend Programmer.bz

Minimize

Share/Bookmark Bookmark and Share