Click here to Skip to main content
15,393,169 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 19 Mar 2012

Tagged as

Stats

8K views
66 downloads
2 bookmarked

Page events lifecycle simplified

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Mar 2012CPOL
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:

C#
protected internal bool ButtonPressed(string buttonname) {
	return IsPostBack && Request.Form[buttonname] != null;
}

 Concrete example:  

HTML
<input type="submit" name="MyButton" value="Send" />  
C#
if(ButtonPressed("MyButton")) {...}  

Using the code 

The framework consists of 3 baseclasses: 

  • CustomPage 
  • CustomMasterPage
  • CustomControl  
 The complete flow: 

C#
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 --