Page events lifecycle simplified





0/5 (0 vote)
Custom baseclasses to simplify the page events lifecycle
Introduction
Because most people only use the Page_Load event, the flow of your code can become very complicated. Especially if you are using postback events.
Background
Often you want to execute code after a click event at the beginning of a page. This example will show you how. Forget the fancy ASP.NET button controls, go back to basics and use a simple html submit button.
Check if a button is clicked by using the following code:
protected internal bool ButtonPressed(string buttonname) {
return IsPostBack && Request.Form[buttonname] != null;
}
Concrete example:
<input type="submit" name="MyButton" value="Send" />
if(ButtonPressed("MyButton")) {...}
Using the code
The framework consists of 3 baseclasses:
- CustomPage
- CustomMasterPage
- CustomControl
Master.InitPage();
Page.InitPage();
Control.InitControl();
//From here you have access to the HttpContext (Session, Cache, Request, ...) objects
Master.PreLoad();
Page.PreLoad();
Control.PreLoad();
Master.PageLoad();
Page.PageLoad();
Master.AfterLoad();
Page.AfterLoad();
Control.AfterLoad();
Master.LoadCompleted();
Page.LoadCompleted();
All objects have a PreLoad and an AfterLoad method. You can, for example, use the PreLoad event from a usercontrol for handling posted data and redirect the page without having to worry about further events.