Accessing and Updating Data in ASP.NET 2.0: Examining the Data Source Control's Events
Posted by: 4GuysFromRolla.com Headlines,
on 22 Nov 2006 |
View original | Bookmarked: 0 time(s)
|A Multipart Series on ASP.NET 2.0's Data Source Controls|
ASP.NET 2.0 introduced a number of new Web controls designed for accessing and modifying data.
These controls allow page developers to declaratively access and modify data without writing any
code to perform the data access. This article is one in a series of articles on ASP.NET 2.0's new data
Data Source Control Basics - explores the concepts
and advantages of data source controls, and compares their usage in ASP.NET 2.0 to data access techniques in ASP.NET 1.x.Accessing Database Data - shows how to use the
SqlDataSource and AccessDataSource controls to query data from a relational database.Filtering Database Data with Parameters - learn how to
retrieve just a subset of database data based on hard-coded values and values from the querystring, other Web controls on the page,
session variables, and so on.Retrieving XML Data with XmlDataSource Control - see how
to retrieve both remote and local XML data and display it in a data Web control.Creating Custom Parameter Controls - learn how
to create your own custom, declarative
Parameter controls for use in the data source controls' parameters collections.Examining the Data Source Control's Events - explore the
events raised during a data source control's lifecycle.
(Subscribe to this Article Series!
The data source controls in ASP.NET 2.0 provide a simple, declarative approach
for accessing and modifying data. The Data Source Control Basics
article that kicked off this article series examined how to add data source controls to a page, specify the data to retrieve and/or
modify, and bind that data to a data Web control (such as a GridView, DetailsView, or FormView). While point-and-click data access
is useful for prototyping and for beginners or hobbyists who have little to no programming experience, wizards and the like
are generally frowned upon by more seasoned developers since they typically offer ease of use at the cost of extensibility and
customizability. The ASP.NET 2.0 data source controls, however, provide the best of both worlds, in my opinion - they can be quickly
and easily configured to work with data, yet through a myriad of events that are raised during the data source
controls' lifecycles, provide the flexibility for use in more advanced scenarios.
The SqlDataSource and ObjectDataSource controls are the two most commonly used data source controls, and they provide a means
for retrieving and modifying data from either a database or object library, respectively. Both controls raise pre- and post-action
events when selecting, inserting, updating, and deleting data. For example, the SqlDataSource and ObjectDataSource raise their
Selecting events immediately before performing the SQL query or invoking the object method to retrieve data.
After the data has been retrieved, the
Selected events fires.
By creating a
Selecting event handler, you can examine and massage the parameters used in selecting data; the
Selected event indicates, among other things, if an exception occurred during the operation. Similarly named
pre- and post-action events fire during the updating, inserting, and deleting processes, as well.
Having a solid understanding of the data source events and event lifecycle has a slew of benefits. Many real-world scenarios
require that the parameters being used for selecting, inserting, updating, or deleting be programmatically assigned or altered,
which can be accomplished in the appropriate pre-action event. Moreover, to gracefully handle database- or object library-level
exceptions in the page, the post-action event handlers should be used. And when debugging, the pre-action events provide an
opportunity for examining what parameters (if any) are being used to query the data. Read on to learn more!
Read More >