Tracking the Data Source with Events

Some programmers get nervous when a control does so much work invisibly. After all, when all goes well, it is great not to have to sweat the details, but if something does go wrong, how can you tell whether your connection failed, no records were updated, an exception was thrown, or exactly what happened? Related to that, what if you want to modify the behavior of the control in some way?

The ASP.NET controls in general, and the data controls in particular, overcome these concerns by providing numerous events that you can handle. For example, the SqlDataSource control described in this chapter has nine events you can handle that are not inherited from its parent control. There is an event you can handle when the DataSource is about to run a SELECT statement (Selecting) and one that you can handle immediately after the SELECT statement has finished executing (Selected). Indeed, there are three other pairs of events for each of the other SQL statements that a DataSource works with, as shown in Table 7-4. The ninth event, Filtering, can be used to validate or alter the values being used in WHERE clauses before they are used with a statement.

Table 7-4. SQL statements and their respective DataSource events

SQL statement

Event occurring before statement is run

Event occurring after statement has run

SELECT

Selecting

Selected

INSERT

Inserting

Inserted

UPDATE

Updating

Updated

DELETE

Deleting

Deleted

WHERE clause

Filtering

N/A

To see this at work, let’s use the Selected ...

Get Programming ASP.NET 3.5, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.