Saturday, January 19, 2013

The Page Life Cycle with Data Binding in ASP.NET

Data source controls can perform two key tasks:

• They can retrieve data from a data source and supply it to bound controls. When you use this feature, your bound controls are automatically filled with data. You don’t even need to call DataBind().

• They can update the data source when edits take place. In order to use this feature, you must use one of ASP.NET’s rich data controls, like the GridView or DetailsView. For example, if you make an edit in the GridView and click Update, the GridView will trigger the update in the data source control, and the data source control will then update the database.

Before you can use the data source controls, you need to understand the page life cycle. The following steps explain the sequence of stages your page goes through in its lifetime. The two steps in bold (4 and 6) are the steps where the data source controls will spring into action:

1. The page object is created (based on the .aspx file).

2. The page life cycle begins, and the Page.Init and Page.Load events fire.

3. All other control events fire.

4. If the user is applying a change, the data source controls perform their update operations now. If a row is being updated, the Updating and Updated events fire. If a row is being inserted, the Inserting and Inserted events fire. If a row is being deleted, the Deleting and Deleted events fire.

5. The Page.PreRender event fires.

6. The data source controls perform their queries and insert the data they retrieve into the bound controls. This step happens the first time your page is requested and every time the page is posted back, ensuring you always have the most up-to-date data. The Selecting and Selected events fire at this point.

7. The page is rendered and disposed.

In the rest of this chapter, you’ll take a closer look at the SqlDataSource control, and you’ll use it to build the record editor example demonstrated earlier—with a lot less code.

No comments:
Write comments
Recommended Posts × +