The <asp:LinqDataSource> control
provides an easy way to bind any ASP.NET UI control to a LINQ to SQL data
model. It enables UI controls to both retrieve data from a LINQ to SQL
data model, as well as cleanly apply updates/inserts/deletes to it.
In our application above we used the LINQ to
SQL ORM designer to create a clean, object oriented data model. We then
added three ASP.NET UI controls to our page (a GridView, a DropDownList, and a
ErrorMessage Literal), and added three <asp:LinqDataSource> controls to
bind Product, Category, and Supplier data from it:
We then wrote 5 lines of business
validation logic in our data model, and 11 lines of UI error handling
The end result is a simple web application
with custom UI that allows users to dynamically filter product data by
category, efficiently sort and page over the product results, inline-edit the
product data to save updates (providing our business rules pass), and delete
products from the system (also providing our business rules allow it).
In future posts in this series I'll cover more
LINQ to SQL scenarios including optimistic concurrency, lazy and eager loading,
table mapping inheritance, and custom SQL/SPROC usage.
Next week I also plan to start a new series of
blog posts that cover the new <asp:ListView> control - which is a new
control that will ship with the ASP.NET release in .NET 3.5. It provides
total control over the markup generated for data scenarios (no tables, no
spans, no inline styles...), while also delivering built-in support for paging, sorting, editing, and insertion scenarios. For example, we could
optionally use it to replace the default Grid layout look of our application
above with a completely custom look and feel. Best of all, I could
replace it within the above page in my application and not have to change my
Data Model, the <asp:linqdatasource> declaration, or my code-behind UI
error handling logic at all.
Hope this helps,