Skip navigation.

Unfloat Large Elements In PrintAll recent postsBack From MVP Summit

Running Auxiliary Code Before OnInit

Not long ago I bemoaned the lack of context awareness in the Page class. I said it would be helpful to intercept calls to page life cycle events. While browsing through the 2.0 page events I noticed that my wish had been granted (to some extent). I’m talking about the PreInit, Init and InitComplete events.

Just recently I built my own implementation of automatic query string parsing and initialization of page fields and properties with its parameters. One of the challenges of this small project was to make this parsing transparent, i.e. I didn’t want to remember to call a certain method to kick off parsing logic, let alone have others remember to do it. It wanted it to quietly do its job without me kicking it in the butt all the time.

I decided to put query string parsing and everything that goes with it in a class up in the hierarchy. Roughly it looked like this:

public class BaseClass : Page
{
 private void ProcessPageParameters ()
 {
   // ……
 }
}

public class Login : BaseClass
{
  [PageParameter("userid", Required=true)]
  private Guid userId;

  override protected void OnInit(EventArgs e)
  {
     base.OnInit(e);
  }
}

The Login class (or page, if you will) in this example has a private member waiting to be initialized with a query string parameter. The question is: where in the parent class do I call the ProcessPageParameters() method?

Placing this call in OnInit of BaseClass might be too late—you may have some code run before base.OnInit of the derived class (on some pages I do). The only other option in ASP.NET 1.x is the constructor. However, touching page class constructors is sketchy enough—your shouldn’t meddle with page controls (as they may not be fully initialized) or view state. Since I was concerned only with a class field and the query string, it worked for me.

I did notice a weird issue with Server.Transfer calls. When you place a normal page request or invoke Response.Redirect, you have the query string properly initialized and accessible right from the constructor. Not so with Server.Transfer—you get no query string in the constructor on the receiving end. It’s not available till the Init event. Don’t ask me why.

This is why I wish there was an event or some other hook between the constructor call and the Init event in 1.x. I’m hoping, in 2.0, the PreInit event turns out to be the missing bit I was after.

On a related note: I always wondered why there was a PreRender event, but no PreLoad or PreInit.

Comments

Comment permalink 1 Gauthier Segay |
As for 1.x version of asp.net, wouldn't the BeginRequest event of HttpApplication would be the right place?

IMHO the place to get such parameters should be right when your Request.Query dictionnary and IHttpHandler are both accessible.

Would'nt it be preferable to implement a HttpModule to do just this and perform some reflection on the HttpHandler that's getting used for the request? This allow our page to inherit any base class.
Comment permalink 2 Milan Negovan |
I actully need to be able to access members of a web page, and I can't reach it from BeginRequest.

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):