ASP.NET Dynamic Data - Customizing Pages and Validation
by David Hayden, Tags: ASP.NET 3.5, Code Generation
One of the cool new technologies shown off in the ASP.NET 3.5 Extensions Preview is Dynamic Data, which together with LINQ To SQL offers some wonderful rapid application development for ASP.NET websites. I wrote a tutorial earlier that guides you through creating your first ASP.NET Dynamic Data Website in minutes:
ASP.NET Dynamic Data Website Getting Started - ASP.NET RAD Development for Data-Driven Websites
You can have a database-driven website without writing any code:

Page Templates
By default, all pages in the Dynamic Data Website are generated from the default templates in the App_Shared -> DynamicDataPages Folder in your Visual Studio 2008 Solution:

Settings in the web.config confirm the use of the ListDetailsTemplate for all tables by default:
<dynamicData dataContextType="" enableTemplates="true">
<mappings
queryStringKeyPrefix=""
pattern="~/{table}/{viewName}.aspx">
<add
actions="list,details"
viewName="ListDetails"
templateFile="ListDetailsTemplate.aspx"/>
< SPAN>mappings>
< SPAN>>
Custom ListDetails Page for Database Table
There will be times when the ListDetailsTemplate won't cut it for one or more tables and you need to create a custom ListDetails Page for a table. Perhaps you want to specify that only certain fields need to be displayed or you want to customize the ItemTemplates and EditTemplates for a table.
To create a custom ListDetails Page for a table, create a new folder off the main solution and give it the name of the table. Create a copy of the ListDetailsTemplate in the DynamicDataPages Folder and paste it into the new folder, renaming it to just ListDetails ( drop the word “template” ). Shown below is a custom ListDetails Page for the Products Table in the Northwind Database:

Now you can customize the page how you see fit and those customizations will only be for the Products Table. Rather than showing all columns in the DynamicGridView, I can show only those columns that I see fit by specifying specific DynamicFields and turning AutoGenerateColumns off:
<Columns>
<asp:DynamicField DataField="ProductName" HeaderText="Name" />
<asp:DynamicField DataField="Supplier" HeaderText="Supplier" />
<asp:DynamicField DataField="UnitPrice" HeaderText="Price" />
<asp:DynamicField DataField="UnitsInStock" HeaderText="In Stock" />
< SPAN>>

Custom Validation Of LINQ To SQL Class Properties
One of the really cool features of ASP.NET Dynamic Data is that you can specify custom validation of properties on the LINQ To SQL Classes and that validation will be exposed on the UI.
We add validation to the properties by 1) creating partial classes of the existing LINQ To SQL Classes and 2) adding attributes on those classes. For example, if we want to specify that the Product's UnitsInStock Property must be within the range of 10 to 50, we can do so by creating a partial class of Product and adding a RangeAttribute on it:
[Range("UnitsInStock",10,50,
ErrorMessage="Must be between {1} and {2}")]
public partial class Product
{
}
Now when we try to edit the UnitsInStock Property we get our error telling us UnitsInStock must be between 10 and 50:

Does that functionality remind you of anything? Perhaps Enterprise Library's Validation Application Block in conjunction with the PropertyProxyValidator and ServerSideValidationExtender :) A lot of similarity which makes this easy to grok!
Conclusion
A lot of potential in the ASP.NET Dynamic Data Support to create database-driven websites very quickly.
Authored by David Hayden
Site: http://www.davidhayden.com/