Click here to Skip to main content
Click here to Skip to main content

Tagged as

Page events lifecycle simplified

, 20 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to 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  
 The complete flow: 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

B. Verboven

Belgium Belgium
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 20 Mar 2012
Article Copyright 2012 by B. Verboven
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid