Export (0) Print
Expand All

ASP.NET Dynamic Data Overview

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

ASP.NET Dynamic Data is a framework that lets you create extensible data-driven Web applications easily. It does this by automatically discovering database schema information 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 this framework by changing elements or creating new ones to override the default behavior. Existing applications can easily integrate scaffolding elements by using 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 run-time capabilities provided by Dynamic Data. You can also select specific capabilities to add to existing Web sites.

Dynamic Data offers the following features:

  • Support for multiple data models and data validation.

  • Support for table relationships. Dynamic Data detects relationships between tables and creates a UI that enables the user to view data from related tables. Dynamic Data suppports displaying and editing data in a many-to-many relationship. For more information, see Walkthrough: Displaying and Editing Data with a Many-to-Many Relationship. Also, Dynamic Data detects the inherited fields in the data model and provides scaffolding for the inherited tables. For more information, see Walkthrough: Mapping Table-per-Hierarchy Inheritance.

  • Support for data-model customization and extension. You can add attributes to the data model to provide additional information about data fields. Dynamic Data automatically uses this custom information for data display and validation. Data model extension information is stored in the data layer without affecting the presentation layer. This clear separation allows for simpler and more portable code among Dynamic Data applications.

  • Support for customizing the UI for data display. You can customize how table data is displayed and how data from individual fields is displayed and edited. For more information, see How to: Customize Data Field Appearance and Behavior in the Data Model.

  • Support for customizing the UI for tables display using entity templates. You can customize entity templates to define table specific behavior. These templates give you more control over the markup that is generated than the markup generated by using page templates. For more information, see Walkthrough: Customizing Table Layout Using Entity Templates.

  • Support for customizing 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.

  • Support for data filtering. Data filtering refers to the ability provided to the user to choose (filter) the table rows to display based on a chosen column value, define whether data fields are displayed or in what order they are displayed. Using page markup you instruct Dynamic Data about the columns to use for table row filtering. Dynamic Data then generates the UI for choosing the column value and creates the necessary query information for the data source control in the page. Dynamic Data provides a set of filter templates, in the ~\DynamicData\Filters directory, that are used to create the UI for table row filtering. You can also create custom filter templates for those column types that do not have a default template. For more information, see Walkthrough: Filtering Table Rows in Dynamic Data.

  • Support for table action links. Dynamic Data allows for the creation of dynamic hyperlinks that are based on the table actions enabled by Dynamic Data. When the user clicks an action link, a page is displayed that enables the user to perform the action that is specified by the link, such as listing detail data rows or editing a data item. You can create your own custom links. For more information, see How to: Create Table Action Links in Dynamic Data.

Dynamic Data creates Web pages (UI) that let users view and edit data in a database. Dynamic Data infers the appearance and the behavior of data fields in the UI from the database schema at run time. This mechanism, together with the availability of default page templates, field templates, entity templates, filter templates and many possibilities for customization, gives you a variety of design choices including the following:

  • Quickly build a Web site using scaffolding. Scaffolding refers to the presentation and data layer support for the automatic generation of Web pages for each table in the database. These auto-generated Web pages provide display, insert, delete, and edit capabilities for each table.

  • Add Dynamic Data features to an existing ASP.NET Web site.

  • Add data-field validation and processing customization in the data layer.

  • Customize the UI that is rendered by Dynamic Data.

The following illustration shows the architecture of Dynamic Data. Dynamic Data works with a data model (for example, the Entity Framework), which represents data in the data store. Dynamic Data infers the data schema from the data model and then uses templates to create UI for displaying and editing data. Metadata that is added to the data model specifies how Dynamic Data presents UI and how it manages data that is sent from the presentation layer.

Note Note

The dashed-line boxes represent technologies not implemented but used by Dynamic Data.

Dynamic Data Architecture

Dynamic Data Architecture

Data Model

The data model uses CLR types to represent 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. When you create a Dynamic Data Web application, you can select the desired data model using the appropriate project template.

You register the data model that you want to use with Dynamic Data in the Web application's Global.asax file. After a data model is registered, Dynamic Data uses the model meta-data to automatically validate data fields, and lets you control appearance and behavior of data at the data layer level.

Scaffolding

Scaffolding is a mechanism that enhances the existing ASP.NET page framework by dynamically displaying pages based on the data model. When the scaffolding mechanism is enabled in a Dynamic Data Web site, ASP.NET analyzes the data model and generates Web pages dynamically for each table. These auto-generated Web pages provide display, insert, delete, and edit capabilities for each table.

For more information, see ASP.NET Dynamic Data Infrastructure.

Page Templates

Dynamic Data uses page templates to provide a default view of data tables. These templates are ASP.NET Web pages and 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). By default, when scaffolding is enabled, Dynamic Data uses only a List view page template.

When you create a Dynamic Data Web project, Visual Studio adds a DynamicData\PageTemplates folder to the project. This folder contains the default page templates.

You can modify the default page templates or you can configure Dynamic Data to use different page templates for different purposes.

For more information, see ASP.NET Dynamic Data Scaffolding and Page Templates Overview.

Entity Templates

Dynamic Data Entity templates provide a mechanism to customize the layout of data without requiring you to create a custom page. For example, the default entity templates show data in a two columns; a label for the field and the actual field value. You can create a custom entity template to show two data fields on each row, or any other layout. The default entity templates can be modified to change the appearance and behavior for the whole site. There are templates for display, edit, and insert operations. Entity templates are consumed by the Details.aspx, Insert.aspx and Update.aspx page templates, any custom pages. Entity templates use field templates to render the actual data. The entity templates are displayed on a page by using the DynamicEntity control. At run time, Dynamic Data replaces this control with the contents of the applicable entity template.

Entity templates map to a MetaColumn, while field templates map to a MetaColumn type. Entity templates display an entire entity (a row from a table), field templates display a single field in a table. Entity templates contain the label (or other HTML element to display the field name) and a field template for the each field displayed. A custom address entity template would map only to the address table, while field templates often map to many columns. The following illustration shows the default layout of an entity (row of data).

Default Entity Template layout

The following illustration shows the same data rendered with a custom entity template.

Cc488546.NDP_CustomEntity(en-us,VS.100).png

You generally create three custom entity templates for each table you wish to control the layout. The custom entity templates must be named after the entity representing the table. For example, using the default LINQ to SQL data model generated for the AdventureWorksLT database, entity templates for the Address table would be named Addresses.acx, Addresses_Edit.acx and Addresses_Insert.acx. Using the default ADO Entity Framework data model generated for the AdventureWorksLT database, entity templates for the Address table would be named Address.acx, Address_Edit.acx and Address_Insert.acx. These custom templates must be in the EntityTemplates directory. For more information, see Walkthrough: Customizing Table layout using Entity Templates

For more information, see Working with Many-to-Many Data Relationships in Dynamic Data.

Field Templates

Dynamic Data uses a set of user controls, referred to as field templates, to render the UI for displaying and editing individual data fields. Dynamic Data determines the appropriate field template based on the data type of the data field that is being displayed or edited. 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 displays 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..

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 modify the default 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. 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.

Filter Templates

Dynamic Data uses a set of default user controls, referred to as filter templates, to render the UI for data filtering. Data filtering refers to the ability provided to the user to choose (filter) the table rows to be displayed based on a chosen column value. This enables the user to display only those rows that contain that particular column value.

Dynamic Data enables you to use page markup to create the UI for data filtering for a table column you identify. This approach is useful when you want to filter table rows for a column type for which Dynamic Data provides a default filter template. If a filter template does not exist for that column type, you can create a custom one and instruct Dynamic Data to use it for creating the UI. Finally, you can create the page markup to let Dynamic Data automatically generate the UI for any column type for which a filter template exists. Remember that by default only the UI for Boolean and foreign-key columns is created.

Dynamic Data performs data filtering as follows:

  • The filter template modifies the basic query for the data source control in the page to include the column value chosen by the user.

  • The data source control executes the modified query and returns the filtered data to the associated data-bound control, which displays the filtered data.

When you create a Dynamic Data Web project, Visual Studio adds a DynamicData\Filters folder to the project. This folder contains the default filter templates.

You can modify the default filter templates or create new ones in order to specify how data filtering is handled.

Enhancing Existing Data Controls

You can add Dynamic Data behavior to existing data-bound controls such as the DetailsView and GridView controls. This lets you add the following capabilities to data controls:

  • DetailsView and GridView controls can display data dynamically by using Dynamic Data templates instead of requiring you to re-create the same markup and code for data controls in every page. 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.

  • The FormView control and its child DynamicEntity control can render entity templates.

  • All data controls can use Dynamic Data validation, which infers validation rules based on the data model . 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 customize how data fields are validated. For more information, see How to: Customize Data Field Validation in the Data Model.

    You can customize the field templates to change how to render the UI for displaying and editing data fields in the data-bound controls. This enables you to make changes in one location and affect the appearance and behavior of the data controls across the Web application.

Dynamic Data Customization

ASP.NET Dynamic Data lets you customize its behavior in many ways, including the following:

  • Customizating general behavior, which applies to any table or data field.

  • Customizating schema specific behavior, which applies to specific tables or data fields in a database.

Customizing General Behavior

You can modify how Dynamic Data performs tasks that do not rely on a specific database schema. You can create these modifications one time and then use them for any number of projects. This type of customization lets you define a consistent behavior for any schema without having to redefine the customization every time that you add or modify a table.

You can customize general Dynamic Data behavior in the following ways:

  • Page templates. You can modify the built-in page templates, such as List.aspx and Edit.aspx. These templates define the layout and appearance and behavior of pages that are rendered by Dynamic Data. For example, the List.aspx template uses a GridView control and a LinqDataSource control or EntityDataSource control. (The GridView control does not define columns. Instead, Dynamic Data infers the columns to display at run time based on the table that is being accessed.) You can change the appearance of the GridView control, or you can use a different control. In addition, you can change the code in the page class for the template.

  • Field templates. You can modify the built-in field templates, which are used to render UI for displaying and editing individual data fields.

  • Field generation. The GridView control in a page template does not specify a column set. Instead, Dynamic Data uses a field generator which creates the set of columns to use for the current table. The idea is that the field generator can use an arbitrary set of rules to decide which column to include, and in what order to include them.

Customizing Schema-Specific Behavior

You can also customize Dynamic Data behavior for individual tables or fields in a data model. In that case, the customizations you make are specific to that instance of the data model. You can make the following types of schema-specific customizations:

  • Creating custom UI for individual tables. If you want to create custom UI for an individual table (not for all tables), you can create a custom page template. For example, you can create a custom Edit page template for only the Product table in a data model.

  • Customizing behavior for the data model. You can customize the behavior of the data model for an application, which lets you create custom validation and bind tables or fields in the data model to specific templates. You perform this type of customization by extending the data model with metatable classes and then adding attributes to the classes or to members of the classes. For example, you can add Range(0,100) attribute to an integer field to specify that it should accept only values between 0 and 100. Dynamic Data provides several default annotation attribute types, and you can create your own attributes to define additional ways to customize the data model.

The following table lists the namespaces that contain the ASP.NET Dynamic Data classes.

Namespace

Description

System.ComponentModel.DataAnnotations

Provides attribute classes that are used to define metadata for Dynamic Data objects.

System.Web.DynamicData

Contains classes that provide the core functionality for ASP.NET Dynamic Data. It also provides extensibility features to customize dynamic data behavior.

System.Web.DynamicData.Design

Contains classes that provide design-time support for the classes in the System.Web.DynamicData namespace.

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.

Back to top

Show:
© 2014 Microsoft