Programming Microsoft ASP.NET 4 - Dino Esposito [247]
protected void Updating(object sender,
ObjectDataSourceMethodEventArgs e)
{
var emp = e.InputParameters[0] as Employee;
if (emp == null) return;
emp.LastName = "WhosThisGuy";
}
The event fires before the update operation climaxes. The InputParameters collection lists the parameters being passed to the update method. The collection is read-only, meaning that you can’t add or delete elements. However, you can modify objects being transported, as the preceding code snippet demonstrates.
This technique is useful when, for whatever reasons, the ObjectDataSource control doesn’t load all the data its method needs to perform the update. A similar approach can be taken for deletions and insertions as well.
Summary
ASP.NET data binding has three faces: classic source-based binding, data source controls, and data-binding expressions. Data-binding expressions serve a different purpose than the other two binding techniques. Expressions are used declaratively and within templated controls. They represent calculated values bindable to any property.
The old data-binding model (the same one introduced with ASP.NET 1.x) is maintained intact with enumerable collections of data bound to controls through the DataSource property and a few others that are related. In addition, a new family of controls has made its debut over the years—data source controls. By virtue of being implemented as a control, a data source component can be declaratively persisted into a Web page without any further effort in code. In addition, data source controls can benefit from other parts of the page infrastructure, such as the view state and ASP.NET cache. Data source controls accept parameters, prepare and execute a command, and return results (if any). Commands include the typical data operations: select, insert, update, delete, and total count.
The most interesting consequence of data source controls is the tight integration with some new data-bound controls. These smarter data-bound controls (GridView, FormView, DetailsView) contain logic to automatically bind at appropriate times on behalf of the page developer, and they interact with the underlying data source intelligently, requiring you to write much less code. Existing data-bound controls have been extended to support data source controls, but only for select operations.
Data source controls make declarative, codeless programming easier and likely to happen in reality. Data source controls, though, are just tools and not necessarily the right tool for the job you need to do. Use your own judgment on a per-case basis.
In the next chapter, we take a look at the ListView control—probably the only data-bound control you would have in ASP.NET if ASP.NET were to be rewritten from scratch today.
Chapter 11. The ListView Control
It’s a job that’s never started that takes the longest to finish.
—J. R. R. Tolkien
The ListView control sums up the features of multiple view controls in a single one. For example, it can be used to create a tabular view of data nearly identical to the view you can obtain from a GridView or DataGrid control. At the same time, the ListView control can be employed to generate a multicolumn layout with the flexibility that only a general-purpose Repeater or, better yet, DataList control can offer.
The ListView control doesn’t only have similarities with other controls; it also has a number of unique features that, when evaluated from a wider perspective, make similarities shine under a different light. ListView uses similarities with other controls as the starting point for building more advanced and unique capabilities that warrant it having its own space in the toolbox of ASP.NET controls.
In this chapter, I’ll focus on exploring the programming interface of the ListView control and its usage in a variety of common scenarios.
The ListView Control
The control