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.
The data from the page is retrieved within the record control class, but the transaction is
committed at the page level. 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.
You can override the SaveData method at the record control or table control to add functionality
before or after saving the data. 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 the transaction is committed. This can be
accomplished by customizing the SaveButton_Click method and calling the underlying methods yourself. Please
note that the SaveButton_Click_Base method at the page level contains a call to Redirect after the data is saved,
so none of the code after a call to the base method will be executied.
If the button action specifies redirection to another page, the Redirect method is called within the
Save button’s click handler. The Redirect method of the base class will automatically change the URL
parameters if they have been specified in the Properties dialog box.
Calling Hierarchy of a Page with a Record Control when Saving Data
The calling hierarchy of a page with a record control when saving data shows the various methods that
are called at the Page and Record Control class levels. When saving data, most of the work is performed
by handling the Click event of the button at the page class level. The Click event is handled by ButtonClick
and ButtonClick_Base methods at the page level. The ButtonClick_Base method then calls the SaveData method for
each of the Record Control classes on the page. Since you may be only editing a subset of fields on the page,
LoadData is called to load the complete record and perform concurrency comparison. This is followed by a call
to Validate the data entered by the user and GetUIData to retrieve the data from the user interface controls into
the database record. The record is then saved in the database. The CommitTransaction is performed at the ButtonClick_Base
to ensure that all of the data is saved within one transaction.
|