16
Beginning ASP.NET (Part 3) .NET 2.0 Core Libraries Tuc Goodwin

Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Embed Size (px)

Citation preview

Page 1: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Beginning ASP.NET (Part 3).NET 2.0 Core LibrariesTuc Goodwin

Page 2: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Agenda

Overview of ASP.NET Events Quick Demo Strings / Formatted or Otherwise Quick Demo Review Questions

Page 3: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Overview of ASP.NET Events

SILVER - An easy way to remember asp.net lifecycle

S – Start I – Initialize L – Load V – Validate E – Event Handling R – Render

Adapted from the Code Project article at http://69.10.233.10/KB/aspnet/ASPNET_Page_Lifecycle.aspx

Page 4: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

1. Start

This is where page properties such as Request, Response, IsPostBack and UICulture are set.

If you need to access or override behavior for this step, use the PreInit method to create or re-create dynamic controls, set a master page or theme or read or set profile property values.

Note: If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.

Page 5: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

2. InitializeThis stage is where themes are applied, and unique ids are generated and set for controls.

You have access to the Init, InitComplete and PreLoad methods in this stage. Microsoft's recommended usage for these methods is as follows:

Init – This event is raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.

InitComplete – This event is raised by the Page object. Use this event for processing tasks that require all initialization be complete.

PreLoad - Use this event if you need to perform processing on your page or control before the Load event. After the Page raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

Page 6: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

3. Load

Probably the most utilized by developers. In this stage controls are loaded with information retrieved from view and control states. The OnLoad is the event method that occurs during this stage.

This is where you set properties for all of the server controls on your page, request query strings, and establish database connections.

Page 7: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

4. Validate

If there are controls that require validation, they are validated here and you can now check the IsValid property of the control.

The event associated with this is Validate, which contains one overloaded method that accepts a validation group string.

The overloaded method instructs the controls in the specified group to validate.

Page 8: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

5. Event HandlingThe event handling for server controls occurs during this stage. This means that events such as Click, SelectedIndexChanged, etc are applied to your server controls, and, in the case of a postback, these event handlers are fired by the control.

Page 9: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

5. Event Handling (Part II)The accessible events of note in this stage are as follows:

LoadComplete – At this step, all of the controls for the page have been loaded.

PreRender – A few things of import happen here.

First, the page object will call EnsureChildControls for each control, and finally for the page. Additionally, any data bound control that has a DataSourceID set will call its DataBind method. It is important to note that the PreRender event occurs for each control on the page.

At the conclusion of this event, ViewState will be saved for the page and all of the controls.

SaveStateComplete – ViewState has been saved. If you have actions that do not require changes to controls but require ViewState to have been saved, you can handle the SaveStateComplete event.

Page 10: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

6. RenderRender is not really an event.

The page object calls this method on each control, which in turn writes out the HTML markup for the control to the browser.

For More information see Microsoft's Developing Custom ASP.NET Server Controls. (http://msdn2.microsoft.com/en-us/library/zt27tfhy.aspx)

Page 11: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

7. Unload

This final event occurs first for each control, then, finally, for the page.

At this point, all controls have been rendered to the output stream and cannot be changed.

During this event any attempt to access the response stream will result in an exception being thrown.

This event is primarily for cleanup routines such as closing open database connections, and open file streams, or, event logging and other tasks.

Page 12: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Quick Demo

I will show several events being fired… I will show the Trace Page level attribute…

Page 13: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Strings / Formatted or Otherwise

Formatting strings is a very powerful tool. It allows you to concatenate strings, conversions and provide super formatting easily

string.Format("{0:c}",100); //$100.00string.Format("{0:e}", 32768); //3.276800e_004string.Format("{0:f}", 345.678); //345.68string.Format("{0:g}"32768); //32768string.Format("{0:n}", 32768); //32,768string.Format("{0:p}", 32768); //32,768%

Page 14: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Review

What did we cover?

Page 15: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Questions

Page 16: Beginning ASP.NET (Part 3).NET 2.0 Core Libraries Tuc Goodwin

Next Month

Date Title

3/14/2009 Beginning ASP.NET (Part 4) Consuming WCF Services

4/11/2009 LINQ

5/9/2009 Lessons Learned

6/13/2009 Building the web Layer using ASP.NET AJAX

7/11/2009 Building the Data and Business Layers using .NET 3.5 (Part 1)

8/8/2009 Building the Data and Business Layers using .NET 3.5 (Part 2)

9/12/2009 ASP.NET Deployment