LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
page 7 of 8
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 46089/ 72


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:

Figure 33

We then wrote 5 lines of business validation logic in our data model, and 11 lines of UI error handling logic. 

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,


View Entire Article

User Comments

Title: Add new   
Name: Aditya
Date: 2010-05-29 1:38:06 AM
Hi boss this is really nice.
Title: Add column on selecting   
Name: Peter
Date: 2010-04-18 7:19:36 PM
Can you actually add column on the linq data source while selecting?
Title: Nice   
Name: vijay
Date: 2009-03-02 5:52:16 AM
Nice detailed article.
Title: Adding new item   
Name: Evans
Date: 2009-02-25 1:28:39 PM
Can we add new item directly with linq?
Title: Cleaning Up SupplierID and CategoryID   
Name: brian
Date: 2008-10-12 4:03:05 PM
If the associated category or supplier no longer exists, you will get a NullReferenceException if you attempt to sort on the associated column in the GridView. Is there some way to handle this?

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-14 2:04:16 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search