Each time a request arrives at a Web server for an ASP.NET Web page, the Web server hands over the request to the ASP.NET engine.
The ASP.NET engine takes the request through a pipeline composed of numerous stages.
At the end of the pipeline, a class corresponding to the requested ASP.NET Web page is instantiated and the ProcessRequest() method is invoked .
Page Life ycle
This life cycle of the ASP.NET page starts with a call to the ProcessRequest() method.
This method begins by initializing the page's control hierarchy.
Next, the page and its server controls proceed through various phases that are essential to executing an ASP.NET Web page. These steps include managing view state, handling postback events, and rendering the page's HTML markup.
The life cycle ends by handing off the Web page's HTML markup to the Web server, which sends it back to the client that requested the page.
Various Stages in Page Life Cycle
Stage0-Instantiation
1 When an ASP.NET Web page is visited for the first time the ASP.NET engine auto-generates a class.
2 This autogenerated class is derived from the page's associated code-behind class.
3 This autogenerated class, along with a compiled instance of the class, is stored in ASP.NET Files folder.
4 ASP.NET Web pages are made up of HTML portion and a code portion, with the HTML portion containing HTML markup and Web control syntax.
5 The purpose of this autogenerated class is to programmatically create the page's control hierarchy.
6 This class is responsible for programmatically creating the Web controls specified in the page's HTML portion.
7 This is done by translating the web control syntax to class’s programming language and HTML markup is translated to Literal controls.
8 When the control hierarchy is constructed, the properties that are explicitly set in the declarative syntax of the Web control are assigned in the code.
Welcome to my Homepage!
Control Hierarchy
Stage 1 – Initialization
1 This stage is marked by having the Page and controls fire their Init events .
2 At this point in the page life cycle, the control hierarchy has been constructed, and the Web control properties that are specified in the declarative syntax have been assigned .
3 First, the Init event sets the page to its initial state as described by the tags in the ASPX file. If the page is posting back to itself, Init also restores any page state that may have been stored in "viewstate".
4 The initialization event can be overridden using the OnInit method.
Stage 2 - Load View State
1 The load view state stage only happens when the page has been posted back.
2 During this stage, the view state data that had been saved from the previous page visit is loaded and recursively populated into the control hierarchy of the Page.
3 The page viewstate is managed by ASP.NET and is used to persist information over a page roundtrip to the server.
4 The viewstate is held in the value property of a hidden control that is passed from page request to page request.
5 Example
6 This event can be overridden using the LoadViewState method and is commonly used to customize the data received by the control at the time it is populated.
Stage 3 - Load Postback Data
1 The load postback data stage happens only when the page has been posted back.
2 When the user submits the form, the browser will make a request to ASP.NET Web page, passing the form field values back in the HTTP POST headers.
3 In this stage, the Page class enumerates the posted back form fields, and searches for the corresponding server control.
4 If it finds the control, it checks to see if the control implements the IPostBackDataHandler interface.
5 If it does, it hands off the appropriate postback data to the server control by calling the control's LoadPostData() method .The server control would then update its state based on this postback data.
6 This stage does not use view state information.
7 This stage is responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback,as the values are identified via posted back form field values, and assigned in the LoadPostData() method for those controls that implement IPostBackDataHandler.
Stage 4 – Load
1 Objects take true form during the Load event .
2 The Load event is where you can check to see if this is the first time this page has been loaded, or whether this is a post back from the user hitting a button or other control on that page.
3 Initialization is performed only on the first page load, for example bind data into the controls. Next, and only if the page is posted back, control events are fired.
4 If the page has been posted back and when the Load event fires, the view state has been loaded (from stage 2, Load View State), along with the postback data (from stage 3, Load Postback Data).
Stage 5 - Raise Postback Event
1 There are two flavors of postback events ,changed events and raised events.
2 Changed event - This event fires when some piece of data is changed between postbacks. Server controls that provide changed events must implement the IPostBackDataHandler interface. (eg: Dropdown list web control has a SelectedIndexChanged event)
3 Raised event - These are events that are raised by the server control for whatever reason the control sees fit (eg:button web control raises the click event). Controls that fire raised events must implement the IPostBackEventHandler interface.
4 This stage does not use view state information at all.
Stage 6 – Pre render
1 This is the final stage of saving anything in the viewstate.
2 This stage is a good place to make final modifications, such as changing properties of controls or changing Control Tree structure, without having to worry about ASP.NET making changes to objects based on the database calls or viewstate updates.
3 After the PreRender phase the changes to objects are locked in and can no longer be saved to the page viewstate.
Stage 7 - Save View State
1 when the page is rendered to the browser. Some state information ("viewstate") is included in a hidden field in the page so when the page is called again through a post back, ASP.NET can restore the page to its previous state.
2 In this stage the Page class constructs the page's view state, which represents the state that must persist across postbacks.
3 The page accomplishes this by recursively calling the SaveViewState() method of the controls in its control hierarchy.
Stage 8 - Render
1 In the render stage the HTML that is emitted to the client requesting the page is generated.
2 The Page class accomplishes this by recursively invoking the RenderControl() method of each of the controls in its control hierarchy.
Stage 9 - Disposal
1 After the page's HTML is rendered, the objects are disposed of. During the Dispose event, you should destroy any objects or references that are created in building the page.
2 This event is typically used to perform cleanup tasks.
3 Finally the page is unloaded from server memory.
Which property on a Combo Box do you set with a column name, prior to setting the DataSource, to display data in the combo box?
adapter.Fill(data, "stores");
storeList.DataSource = data;
storeList.DataMember = "stores";
storeList.DataTextField = "stor_name";
ddlExpenseCode.DataSource = expenseCodeDataTable;
ddlExpenseCode.DataTextField = MRConstants.MRtextExpenseCode;
ddlExpenseCode.DataValueField = MRConstants.MRvalueExpenseCode;
ASP .NET Controls
Introduction
1 A Web form’s user interface is “declared” using a combination of HTML and Server controls.
2 Server controls fire events that can be handled by server-side scripts. In effect, ASP.NET abstracts the divide between client and server by creating the illusion that events are fired and handled on the same machine. In reality, events fire on the server when an external stimulus (such as the click of a button) causes the form to post back to the server.
3 ASP.NET pages incur more processing overhead than static HTML pages, they tend to execute much faster than ASP pages.
Web Controls
1 All Web Form controls inherit from a common base class, namely the System.Web.UI.WebControls class. This base class implements a set of common properties that all of these controls will have.
2 Web controls family includes almost 30 different control types that you can use in ASP.NET Web forms.
3 Different Web Controls are
Class Name Description
AdRotator Displays rotating banners in Web forms
Button Generates submit buttons
Calendar Displays calendars with selectable dates
CheckBox Displays a check box in a Web form
CheckBoxList Displays a group of check boxes
CompareValidator Validates user input by comparing it to another value
CustomValidator Validates user input using the algorithm of your choice
DataGrid Displays data in tabular format
DataList Displays items in single-column or multicolumn lists
DropDownList Generates HTML drop-down lists
HyperLink Generates hyperlinks
Image Displays images in Web forms
Table Generates HTML tables
AdRotator Displays rotating banners in Web forms
Button Generates submit buttons
Calendar Displays a check box in a Web form
CheckBoxList Displays a group of check boxes
CompareValidator Validates user input by comparing it to another value
CustomValidator Validates user input using the algorithm of your choice
DataGrid Displays data in tabular format
DataList Displays items in single-column or multicolumn lists
DropDownList Generates HTML drop-down lists
HyperLink Generates hyperlinks
Image Displays images in Web forms
Table Generates HTML tables
Html Controls
ASP.NET supports a second type of server control called HTML controls.
HTML controls are instances of classes defined in the FCL’s System.Web.UI.HtmlControls namespace.
They’re declared by adding RunAt=“server” attribute to ordinary HTML tags.
For example - declares a standard HTML text input field. However, the statement
declares an HTML control
Tag Name Description
HtmlAnchor