ASP.NET Page Life Cycle

Agenda ` `

Programming Basics Page Life Cycle

A Page g Example p

A Page g Example p Currency C y Converter y Convert:  

  & b US d U.S. dollars ll to Euros.


/

Server Controls `

This web form includes ordinary HTML with a difference. Some tags have a runat runat="server" server attribute, which changes them into server controls. `

Server controls are objects j yyou can interact with in yyour code.

`

Every server control also needs an ID, which you use to i interact with i h iit iin your code. d

`

All server controls must be placed in the section.

Control Events `

Server controls are full-fledged .NET objects, with properties, methods, and events.

`

Control tags connect events to event handlers using the OnEventName attribute.

`

This tag connects the ServerClick event of the HtmlInputSubmit control to an event handling method named Convert_ServerClick:

>

Control Events `

This event handler reacts to the event and modifies the content of the object named Result. Result

protected void Convert_ServerClick(Object sender, EventArgs e) { decimal USAmount = decimal.Parse(US.Value); decimal Parse(US Value); decimal euroAmount = USAmount * 0.85M; Result.InnerText = USAmount.ToString() + " U.S. dollars = " + euroAmount.ToString() + " Euros."; }

The Code Behind File `

All the event handlers for a page are arranged in a single page class.

`

The p page g class is stored in the code-behind ffile.

Page1.aspx

Page1.aspx.cs public class WebForm1 { private void cmd1_Click() p _ () { … } }

Key ASP.NET File Types `

Ends with .aspx `

`

Ends with .ascx `

`

These are ASP.NET web p pages. g Theyy contain the tags g for the web page user interface (and, optionally, the web page code). Users navigate directly to one of these pages to start using your web application.

These are ASP.NET ASPNET user controls—groups of controls that can be reused in more than one web page.

Ends with .asmx `

These are ASP.NET web services, with are self-contained units of functionality that other applications can call over a network t k or the th Internet. I t t

Key ASP.NET File Types Contd., `

web.config `

`

Global.asax `

`

This is the XML-based configuration file for your ASP.NET application. li i IIt includes i l d settings i ffor customizing i i security, i state management, memory management, and much more. This is the global application file.You can use this file to define global variables (variables that can be accessed from any web page in the web application) and react to global events (such as when a web application first starts).

Ends with .cs or .vb `

10

These are code-behind files that contain C# or VB code for a web page, web service, or other resource.

Key ASP.NET Folders `

Bin `

`

App_Code

`

`

Contains source code C d files f l that h are dynamically d ll compiled l d for f use in your application.You can use this directory in a similar way to the Bin directory; the only difference is that you place source code files here instead of compiled assemblies. assemblies

App_Data

`

`

Contains all the compiled .NET components (DLLs) that the ASP.NET web application li i uses. FFor example, l if you d develop l a custom d database b component, you’ll place the component here.

This directory is reserved for data storage, including SQL Server 2005 database files and XML files. files Of course, course you you’re re free to store data files in other directories.

App_Themes

`

St Stores the th themes th th thatt are used d bby your web b application. li ti

What is an ASP.NET Application? `

A combination of files that can be accessed through a virtual directory on a web server.

`

May include ASP.NET web pages, web services, handlers, modules, and other resources. HTTP request

Web Browser

IIS Web Server

ASP.NET Engine

HTTP response

Web Page Code

The Application Domain `

Each web application pp runs in a separate, protected area of memory called an application domain.

`

Each application domain has its own configuration settings, cache, and session data.

`

An application domain is a .NET concept. You can think of it as a “lightweight process.”

Application Updates `

ASP.NET files are never locked.

`

Whenever you change Wh h anything hi in i your application—for li i f example, your page code or a configuration setting—a new application domain is created.

`

The new application domain serves all subsequent requests. Existing requests finish processing in the old application domain.

`

When all old requests q are finished,, the old application pp domain is released from memory.

Agenda ` `

Programming Basics Page Life Cycle

The Page Lifecycle

Page.Init Control events

Page.Load

Change Events

Textbox1.ServerChange

Action Events

Button1 ServerClick Button1.ServerClick Page.Unload Page is disposed

Page Life Cycle Page Loading `

Page_Load fires at beginning of request after controls are initialized `

Input control values already populated

protected void Page_Load(Object s, EventArgs e) { message.Text = textbox1.Text; }

Page Life Cycle Wiring Up Control Events

`

Control event handlers are identified on the tag