The .NET Page Lifecycle, Part II
   Iron Speed Designer extends the standard functionality of the Microsoft .NET Framework’s Page class to create data-bound user interface controls. This functionality is implemented in the BasePage class that is derived from the Page class.
- Razi Mohiuddin, President of Iron Speed, Inc.

February 22, 2006
Iron Speed Designer V3.2
Data Binding Lifecycle of Child Controls
Iron Speed Designer extends the standard functionality of the Microsoft .NET Framework’s Page class to create data-bound user interface controls. This functionality is implemented in the BasePage class that is derived from the Page class. Each of the pages in your application is derived from the BasePage class.

The DataBind method in the BasePage class is overridden to read data from the database and bind this data to each user interface control. The page itself does not read the data, but instead delegates this responsibility to each of the controls within the page.

During the data binding phase, the table and record controls run the query and each child control will be individually data bound from the data that was retrieved by the query. The data binding lifecycle of each of the child controls with respect to their parent controls is shown below:

Saving Data to the Database
In the above sections we have discussed the lifecycle of retrieving and displaying data on a web page. There is an analogous life cycle the page goes through to retrieve the data from the user interface controls into memory and subsequently save this data in the database.

The data is retrieved into memory from each of the user interface controls and validated. The retrieved data is stored in an instance of the data access layer’s Record class.

Unlike reading data from the database, the saving of the data is accomplished at the page level instead of at the Table or Record control levels. This is because data from all records must be saved within a transaction and any foreign key relationships must be taken into account when the data is saved. For example, master records are saved first followed by child records since the child records need the Id of their parent record.

A number of events before and after writing the data to the database and before and after committing the transaction are available to you. If the Id’s of the records are assigned by the database, they will be available after the data is written to the database and before committing the transaction.

If the button action specifies redirection to another page, the Redirect method is called and can be overridden to change the URL parameters if needed. The Redirect method of the base class will automatically change the URL parameters if they have been specified in the Properties dialog box.

Referencing Page Controls in Code
The user interface controls defined on the page can be accessed easily within each of the page, table control and record control classes in the code-behind file for your page. Please note that user interface controls within a table control are initialized and defined when data is loaded into the table control since the number of rows displayed is determine by the result set returned by the query. As such we recommend that you make most of your code customizations at the Record Control class level since this class is available both when displaying a single record on a page as well as for each row within a table.

Page Class

You can access any user interface control from the page class except the controls that are repeated for each row in a table. To access the controls from the page, you simply specify the name of the control that is shown on the layout page. For example, if you have an Add Customer page, you may have a field label called CompanyNameLabel and a field value text box called CompanyName. To access this control, you can:

C#:

this.CompanyName
this.CompanyName.Text             // to access the text entered by the user

VB.NET:

Me.CompanyName
Me.CompanyName.Text             ' to access the text entered by the user

If you are displaying an Add, Edit or Show Record page, you will have a record control on the page as shown in the control hierarchy above. The record control corresponds to the record control class in the page’s code-behind file. If the page is a Show Table page, you will have a table control on the page and a record control for each row in the table. Both the table control and the record control will have corresponding classes in the page’s code-behind file. You can access the record and table controls within the page as follows:

C#:

     this.CustomersRecord // to access the record control
// to access the company name within the
// record control
     this.CustomersRecord.CompanyName
     this.CustomersTable // to access the table control

VB.NET:

Me.CustomersRecord   ' to access the record control
                   ' to access the company name within the
                   ' record control
Me.CustomersRecord.CompanyName
Me.CustomersTable     ' to access the table control

Table Control Class

The table control class corresponds to the table control on the page. You can directly access all of the controls within the table except the rows of a table. To access the rows of a table, you can either override or handle events at the record control class level, or use functions such as GetRecordControls or GetRecords to get an array list that can loop through to get an individual row. The search, filter, pagination and column sorting controls can be accessed directly from the table control class.

Record Control Class

We recommend most of the code customizations to be made at the record control class. The record control class corresponds to the record being displayed on the page. If there is a table displayed on the page, the record control class corresponds to each row within a table. The customizations will be the same regardless of whether the record control class corresponds to a single record control or to a row within a table.

You can access all of the fields within a record control including column values as shown below:

C#:

this.CompanyName
this.CompanyName.Text             // to access the text entered by the user

VB.NET:

Me.CompanyName
Me.CompanyName.Text             ' to access the text entered by the user

Post Back
In ASP.NET, each page is reloaded when a button is pressed or if you set the AutoPostBack property of a control to be True when its value has changed. Since the same overridden methods are called and the same event notifications are sent during a post back, you need to make sure that you check the post back property of a page when writing your custom code. For example, if you want to initialize a field to a value when the page is first displayed then you can handle an event and initialize this value. But if you do not check the post back property in the event handler, your field will be initialized again when the page is being reposted and thus overwrite any user entered value. This is perhaps the most common mistake made by .NET programmers.

C#:

if (!this.IsPostBack)
{
     // done only during initialization
}
else
{
     // done only during postback
}

VB.NET

If (Not Me.IsPostBack) Then
    ' done only during initialization
Else
    ' done only during postback
End If

About the Author
Razi Mohiuddin
Co-Founder, President & CEO of Iron Speed, Inc.

Mr. Mohiuddin co-founded numerous Internet and software companies, including Onsale, Inc. (later Egghead.com and now Amazon.com), Ambia Corporation, an electronic document publishing software company later acquired by Infodata Systems Inc., and Software Partners, Inc., a software consulting company that developed StreetSmart, e.Schwab, FundMap, SchwabLink and Retirement Planner software for Charles Schwab & Co.

Mr. Mohiuddin earned his BS in Computer Science from the University of Illinois, Chicago.



  Privacy Statement