|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
ASP.NET Dynamic Data Overview
Updated: July 2008
ASP.NET Dynamic Data is a framework that lets you create data-driven ASP.NET Web applications easily. It does this by automatically discovering data-model metadata at run time and deriving UI behavior from it. A scaffolding framework provides a functional Web site for viewing and editing data. You can easily customize the scaffolding framework by changing elements or creating new ones to override the default behavior. Existing applications can easily integrate scaffolding elements with ASP.NET pages.
This topic contains the following sections:
You can use ASP.NET Dynamic Data to create new data-driven Web sites that require minimal code and that take full advantage of the capabilities of the Dynamic Data framework. You can also select specific Dynamic Data capabilities to add to existing Web sites.
Dynamic Data offers the following features:
Web scaffolding that can run a Web application that is based on reading the underlying database schema. Dynamic Data scaffolding can generate a standard UI from the data model.
Full data access operations (create, update, remove, display), relational operations, and data validation.
Automatic support for foreign-key relationships. Dynamic Data detects relationships between tables and creates UI that makes it easy for users to view data from related tables. For more information, see Walkthrough: Creating a New ASP.NET Dynamic Data Web Site Using Scaffolding.
The ability to customize the UI that is rendered to display and edit specific data fields. For more information, see How to: Customize ASP.NET Dynamic Data Default Field Templates and How to: Customize Data Field Appearance and Behavior in the Data Model.
The ability to customize the UI that is rendered to display and edit data fields for a specific table. For more information, see How to: Customize the Layout of an Individual Table By Using a Custom Page Template.
The ability to customize data field validation. This lets you keep business logic in the data layer without involving the presentation layer. For more information, see How to: Customize Data Field Validation in the Data Model.
The dynamic nature of Dynamic Data is that it can infer the appearance and the behavior of data fields from the underlying database schema at run time. This mechanism, together with the availability of default page and field templates and many possibilities for customization, gives you a variety of design choices, including the following:
Building a Web site using scaffolding.
Adding dynamic data to an existing Web site.
Adding data field validation business logic.
Customizing the UI that is rendered in order to display and edit specific data fields or a specific table.
The data model represents the information that is in a database and how the items in the database are related to each other.
Dynamic Data supports the LINQ-to-SQL data model and the ADO.NET Entity Framework data model. You can include multiple instances of data models in a Web application, but the models that are used in Dynamic Data must be of the same type.
You register the data model or models that you want to use with Dynamic Data in the Web application's Global.asax file. After a data model is registered with Dynamic Data, the data model can perform automatic validation of data fields, and it lets you control appearance and behavior of data at the data layer level.
Scaffolding is a mechanism that enhances the existing ASP.NET page framework by dynamically displaying pages based on the data model. Scaffolding provides the following capabilities:
Minimal or no code to create a data-driven Web application.
Quick development time.
Built-in data validation based on the database schema.
Automatic data selection created for each foreign key or Boolean field.
Dynamic Data scaffolding uses page templates to provide a default view of data tables. Page templates are ASP.NET Web pages that are configured to display data from any table that is available to Dynamic Data. Dynamic Data includes page templates for different views of data, such as listing a table (List view), displaying master/detail tables (Details view), editing data (Edit view), and so on. By default, Dynamic Data is configured to use only a List view page template. You can change the default page template or change Dynamic Data to use different page templates for different purposes. For more information, see ASP.NET Dynamic Data Infrastructure.
Dynamic Data uses field templates to render the UI for displaying and editing individual data fields. It determines the appropriate field template from the data field types. Dynamic Data includes separate field templates for displaying and for editing data fields.
For example, for a DateTime data field Dynamic Data uses the following field templates:
DateTime.ascx. This template displays DateTime data type as text (a string) and renders it as a Literal control.
DateTime_Edit.ascx. This template renders a TextBox control. If the field in the database cannot be null or if the data model has been customized to require an entry, this control also renders a RequiredFieldValidator control. The DateTime_Edit.ascxfield template provides a DynamicValidator control that handles any exceptions that are thrown from the data model. It also supports the Regex class.
When you create a Dynamic Data Web project, Visual Studio adds a DynamicData\FieldTemplates folder to the project. This folder contains the default field templates.
You can customize the built-in field templates or create new ones in order to specify how individual data fields are rendered. For example, you can create field templates that render UI for displaying and editing telephone numbers or e-mail addresses. As another example, you can create field templates that render UI for using alternative ways (such as a slider) to let users specify numeric data.
Enhancing Existing Data Controls
Dynamic Data enhances existing data controls to include dynamic behavior in the following ways:
DetailsView and GridView controls can display data dynamically by using predefined Dynamic Data templates instead of requiring you to re-create the same markup and code for data controls in every page. You can customize these templates to change what controls to use and how to render the UI for displaying and editing data fields. This enables you to make changes in one location and affect the appearance and behavior of the data controls across the Web application. This is independent of the specific table, so that a page template can be used for any table in the database. For more information, see How to: Add Dynamic Behavior to Data-Bound Controls by Using a DynamicField.
FormView and ListView controls can implement behavior similar to that for DetailsView and GridView controls by using a DynamicControl control in their templates and by specifying which field in the row to display. Dynamic Data will then automatically build the UI for these controls, based on the templates that you specify. DynamicControl controls do not automatically render the UI for any field; you must bind the controls to a specific data field. For more information, see How to: Use ASP.NET Dynamic Data in Templated Data-Bound Controls.
Dynamic Data examines the data model metadata for a LINQ-to-SQL or Entity Framework data model and provides automatic validation based on the metadata. For example, if a column in the database is marked as not nullable, a RequiredFieldValidator control is automatically rendered for the column. You can also apply custom metadata in order to further customize how data fields are rendered and validated. For more information, see How to: Customize Data Field Validation in the Data Model.
The following table lists the namespaces that contain the ASP.NET Dynamic Data classes.
Provides attribute classes that are used to define metadata for Dynamic Data controls.
Contains classes that provide the core functionality for ASP.NET Dynamic Data. It also provides extensibility features to customize dynamic data behavior.
Suggestions for additional exploration include the following:
Understanding URL routing. For more information, see ASP.NET Routing.
Understanding LINQ-to-SQL Object-Relational mapping. For more information, see LINQ to SQL.
Understanding the ADO.NET Entity Framework. For more information, see ADO.NET Entity Framework.