ASP.NET Page Life Cycle

adhocjackpotSecurity

Nov 5, 2013 (3 years and 9 months ago)

85 views

ASP.NET Page Life Cycle

Dev Basics Series

Jay Harris

Overview

What to expect out of this session

Discuss:

ASP.NET Page Life Cycle

Proper use of Data Binding Events

Involves only ASP.NET Events

No MVC. No
Silverlight
.

No Controls or
DataSource

Controls

Tips & Tricks. And Traps.

About the Talk

Life Cycle

Data Binding

Tips

& Tricks

Pain

Points


Covers

Overview

What to expect out of this session

Jay Harris

Software Consultant

.NET Developer

Ask Questions Anytime

Please Give Feedback!

http://www.speakerrate.com/jayharris/

About the Speaker

Life Cycle

Data Binding

Tips

& Tricks

Pain

Points


Covers

The Agenda

Anatomy of the ASP.NET Page Life Cycle

The Agenda

Anatomy of the ASP.NET Page Life Cycle

System Assigns Properties:

Request

Response

UICulture

System determines if the request
is new or
postback

Page Start

Start

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

System Prepares Controls:

Available by
UniqueID

Properties set from code/CIF values

Applies Theme &
MasterPage

Handled in
PreInit

Page Initialization

Start

Initialization

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

If
IsPostBack
:

Restores Properties from State

Page Load

Start

Initialization

Load

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

Saves
ViewState

& Renders

Render

Start

Initialization

Load

Render

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

If
IsPostBack

&
Validators

exist:

Runs Validate() for All
Validators

…even the disabled ones

Control Validation

Start

Initialization

Load


Validation

Render

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

If
IsPostBack
:

Runs any Event Handlers
as needed

Includes events such as:

TextBox.TextChanged

DropDownList.SelectedIndexChanged

Button.Click

PostBack

Events

Start

Initialization

Load


Validation


Events

Render

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

“SILVER!”

(as in medals & bars)


Easy to Remember

S
tart

I
nitialization

L
oad

V
alidation

E
vents

R
ender

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

Wait. “SLIVER?!?”

(no, not wood)


Easy to Remember

S
tart

L
oad

I
nitialization

V
alidation

E
vents

R
ender

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

Huh? “LIVER?!?”

(
eww
. gross. and there’s no onions)


Easy to Remember

S
tart

L
oad

I
nitialization

V
alidation

E
vents

R
ender

Agenda

The Agenda

Anatomy of the ASP.NET Page Life Cycle

“Hi
-
yo
, Silver, away!”

(The Lone Ranger)


Easy to Remember

S
tart

I
nitialization

L
oad

V
alidation

E
vents

R
ender

Agenda

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Debugging Events

Tracing


Page
-
Level

Features

Page
-
Level Tracing:

<%@ Page Trace=“true” %>

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Debugging Events

Tracing*


Page
-
Level*

Features

Page
-
Level Tracing:

<%@ Page Trace=“true” %>



*Not available after Render

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Debugging Events

Tracing*


Page
-
Level*


App
-
Level

Features

Application
-
Level Tracing:

<configuration>


<system.web>


<trace enabled=“true” />


</system.web>

</configuration>

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Debugging Events

Tracing*


Page
-
Level*


App
-
Level*

Features

Application
-
Level Tracing:

<configuration>


<system.web>


<trace enabled=“true” />


</system.web>

</configuration>

*Site
-
Wide Performance Hit

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Event Wiring

Tracing*

Wiring


Manual

Features

Using the Constructor:

public
MyPage
()

{


this.Load

+=
PageLoad
;

}

protected void
PageLoad
(…)

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Wiring

Tracing*

Wiring*


Manual*

Features

Using the Constructor:

public
MyPage
()

{


this.Load

+=
PageLoad
;

}

protected void
PageLoad
(…)

*Wire Control events in Page Init

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Wiring, Auto

Tracing*

Wiring*


Manual*


Auto
-
Wire

Features

Auto
-
Wire via
Page_
EventName
:

protected void
Page_Load
(…)

{


//Do some stuff

}

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Wiring, Auto

Tracing*

Wiring*


Manual*


Auto
-
Wire*

Features

Auto
-
Wire via
Page_
EventName
:

protected void
Page_Load
(…)

{


//Do some stuff

}

*Page only. Not for controls.

*Performance Hit.

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Execution

Tracing*

Wiring*

Execution


Tree

Features

Top
-
Down Control

Tree
Execution

First load the Page

…then load the Container Control

…then load the Child Control

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Execution

Tracing*

Wiring*

Execution*


Tree*

Features

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Top
-
Down Control Tree Execution

First load the Page

…then load the Container Control

…then load the Child Control

*Except: Initialization & Unload

First unload Controls, then the Page

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Execution

Tracing*

Wiring*

Execution*


Tree*


Collection

Features

Index
-
based Collection Execution

page.Controls
[0].
OnLoad

page.Controls
[1].
OnLoad

page.Controls
[2].
OnLoad



page.Controls
[n].
OnLoad

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Event Execution

Tracing*

Wiring*

Execution*


Tree*


Collection*

Features

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Index
-
based Collection Execution

page.Controls
[0].
OnLoad

page.Controls
[1].
OnLoad

page.Controls
[2].
OnLoad



page.Controls
[n].
OnLoad

* When was it added to Controls?

Event Execution

Tracing*

Wiring*

Execution*


Tree*


Collection*

Features

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Index
-
based Collection Execution

page.Controls
[0].
OnLoad

page.Controls
[1].
OnLoad

page.Controls
[2].
OnLoad



page.Controls
[n].
OnLoad

* When was it added to Controls?

Loading
ViewState

Tracing*

Wiring*

Execution
*

ViewState

Features

Control State is loaded twice

Once immediately prior to Load

Once immediately following Load

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Loading
ViewState

Tracing*

Wiring*

Execution
*

ViewState
*

Features

Subjected to the terms,
limitations, availability, whims,
will, desires, rules, patterns,
provisions, agreements,
covenants, conditions, assigns,
successors, and approval of the
ASP.NET worker process.

Control State is loaded twice

Once immediately prior to Load

Once immediately following Load

*Not restored if modified prior

Running the Show

Harnessing the ASP.NET Page Life Cycle*

Displaying Data

Effectively Binding Dynamic Data to the Page

Displaying Data

Effectively Binding Dynamic Data to the Page

Control.DataBind
();

Only for the specific
contol

and its child controls

Page.DataBind
();

Binds all controls on the page

DataBind
();

Data

DataBind
();

Displaying Data

Effectively Binding Dynamic Data to the Page

DataBinding

Begins
DataBinding

of a control

Binding Events

DataBind
();

Events

DataBinding

Data

Displaying Data

Effectively Binding Dynamic Data to the Page

RowCreated

/
ItemCreated

Manipulating item markup

Cannot be dependent on control data

Row vs. Item Usage:

RowCreated
:
GridView

ItemCreated
:
DataGrid,ListView
,
Repeater, and everything else.

Binding Events

DataBind
();

Events

DataBinding

RwCreated

Data

Displaying Data

Effectively Binding Dynamic Data to the Page

RowDataBound

/
ItemDataBound

Manipulating item data

Data is available within controls

Row vs. Item Usage:

RowCreated
:
GridView

ItemCreated
:
DataGrid
,
ListView
,
Repeater, and everything else.

Binding Events

DataBind
();

Events

DataBinding

RwCreated

RwDBound

Data

Resources & Questions

Wrap
-
up

Taking the Next Steps with the ASP.NET Page Life Cycle

Resources

MSDN:

http://tinyurl.com/AspNetPageLifeCycle

Blog:

http://www.cptloadtest.com

Email:

jayharris@harrisdesigns.com

Twitter:

@
jayharris

Questions?

Thank You

Wrap
-
up

Taking the Next Steps with the ASP.NET Page Life Cycle

Jay Harris
Online

Blog:

http://www.cptloadtest.com

Email:

jayharris@harrisdesigns.com

Twitter:

@
jayharris

Feedback

Rate:

http://www.speakerrate.com/jayharris