Condividi tramite


ASP.NET Dynamic Data

ASP.NET Dynamic Data lets you create extensible data-driven Web applications by inferring at run time the appearance and behavior of data entities from the database schema and deriving UI behavior from it.

Dynamic Data supports scaffolding, which is a way to automatically generate Web pages for each table in the database. Scaffolding lets you create a functional Web site for viewing and editing data based on the schema of the data. You can easily customize scaffolding elements or create new ones to override the default behavior.

You can also enable dynamic behavior in existing or new Web applications without using scaffolding. In that case, you specify how and when Dynamic Data should infer UI elements from the data source without using scaffolding for the entire Web site.

This topic contains the following sections:

  • Dynamic Data Architecture

  • Presentation Layer Elements

  • Data Layer Elements

  • Options for How to Use Dynamic Data

  • Dynamic Data Scenarios

  • Additional Information

Dynamic Data Architecture

Dynamic Data obtains data schema information or metadata from a data model that represents database entities as common language runtime (CLR) types. The following illustration shows the Dynamic Data architecture. The dashed-line boxes represent technologies that are defined in the CLR but that are used by Dynamic Data.

Dynamic Data Architecture

Dynamic Data Architecture

As the illustrations shows, Dynamic Data architecture contains the following layers:

  • Presentation layer, which contains elements that are used to create the UI for displaying and editing data entities.

  • Data layer, which contains the data model that represents the database entities as CLR types.

  • Data source mapping layer, which contains elements that are part of the CLR but are used by Dynamic Data. These elements are technologies such as LINQ to SQL and ADO.NET Entity Framework that are used to generate data models.

Presentation Layer Elements

This section provides an overview of the Dynamic Data elements that are used to create the UI for displaying and editing data entities. For more information, see ASP.NET Dynamic Data Scaffolding.

Page Templates

Page templates are Web pages that render data from any table in the database. Dynamic Data includes page templates for different views of data, such as listing a table (List view), displaying master/detail tables (Details view), and editing data (Edit view). By default, 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 templates or create new ones in order to specify how tables are rendered.

Entity Templates

Entity templates enable you to customize the UI for an entire data entity, such as a row or a table. This provides more flexibility than customizing individual data fields, and is useful if you want to create custom UI and apply it to default or custom page templates.

When you create a Dynamic Data Web project, Visual Studio adds a DynamicData\EntityTemplates folder to the project. This folder contains the default entity templates. There are templates for display, edit, and insert operations. The default entity templates show data in two columns, using a label for the field name and an appropriate control for the field value. You can modify the default entity templates to change the appearance and behavior of data rows for the whole site.

Entity templates are included in the Details.aspx, Insert.aspx and Update.aspx page templates and any custom pages. Entity templates in turn use field templates to render the actual data.

For more information about how to use entity templates, see the related topics listed in the Dynamic Data Scenarios section.

Field Templates

Field templates are user controls that render the UI for individual data fields. By default, Dynamic Data selects a field template based on the data type of the field that is being displayed. For example, to display Boolean data, Dynamic Data uses the Boolean field template; to display text data, Dynamic Data uses a text field template; and so on. There is typically one field template to display data, and a different template that lets users enter or edit field values.

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.

Filter Templates

Filter templates are user controls that render the UI for data filtering, which lets the user select the table rows to display based on a column value. For example, you can display all the rows that contain a certain category. By default, Dynamic Data provides Boolean, foreign-key, and enumeration filter templates.

Dynamic Data enables you to use page markup to create the UI for data filtering for a column. When you create a Dynamic Data Web project, Visual Studio adds a DynamicData\FilterTemplates folder to the project. This folder contains the default filter templates.

If a filter template does not exist for that column type, you can create a custom one and instruct Dynamic Data to use it when it creates the UI. You can also create page markup to let Dynamic Data automatically generate the UI for any column type for which a default filter template exists.

Data Layer Elements

ASP.NET Dynamic supports LINQ to SQL and ADO.NET Entity Framework data models. Dynamic Data uses these types to query the database and to create, read, update, and delete data (that is, to perform CRUD operations). Data models provide an easy way to integrate custom data validation and business logic rules.

At run time, Dynamic Data automatically extracts metadata information from the data model, such as data field properties. From this information, it infers how to create UI for displaying and editing data.

When you create a new project in Visual Studio, you can select the type of model that is used by the project. Dynamic Data scaffolding can support only one of the data model type in the same project.

Options for How to Use Dynamic Data

Dynamic Data supports the following design options:

  • Create a Web site using scaffolding. You can create a basic Web application that uses ASP.NET Dynamic Data that requires little or no coding. When scaffolding is enabled, ASP.NET analyzes the data model and generates Web pages dynamically based on the metadata information. These auto-generated Web pages provide CRUD capabilities for each table. For more information, see How to: Add Dynamic Behavior to Data-Bound Controls.

  • Enable dynamic behavior for individual data-bound controls (such as the GridView and ListView controls) in an existing ASP.NET Web site. This enables you to use Dynamic Data features such as run-time data field validation, predefined pages to perform CRUD operations, and data field customization. For more information, see How to: Add Dynamic Behavior to Data-Bound Controls.

  • Add Dynamic Data scaffolding to an existing (or new) ASP.NET Web site. This approach gives you more control over customization than enabling dynamic behavior for individual controls. For more information, see Walkthrough: Adding Dynamic Data Scaffolding to Existing ASP.NET Web Sites.

Customizing Dynamic Data

An important Dynamic Data feature is customization. Customization options can be grouped into the following categories:

  • Customizing general (presentation layer) behavior. You can modify how Dynamic Data performs tasks that do not rely on a specific database schema. You make these modifications in the presentation layer by modifying the default templates or by creating custom templates to display and interact with the database. You can then use these modifications in any Dynamic Data project. For more information, see ASP.NET Dynamic Data Presentation Layer Customization.

  • Customizing schema-specific behavior. You can customize Dynamic Data behavior for individual tables or fields based on the data model or database schema. In this case, the customization is specific to the instance of the data model. For example, you can customize the UI for an individual data field, or you can create a custom page template or use entity templates to render an individual table. You can modify the appearance and behavior of individual data fields by using the System.ComponentModel.DataAnnotations attributes. You can also customize the behavior of an application, which lets you create custom validation and bind tables or fields in the data model to specific templates. For more information, see ASP.NET Dynamic Data Layer Customization.

Dynamic Data Scenarios

The following table lists Dynamic Data scenarios and topics that describe how to accomplish them.

Scenario

Topic

Description and Examples

Enable dynamic behavior in data-bound controls in an existing Web site

How to: Add Dynamic Behavior to Data-Bound Controls.

This approach is useful if you have an existing Web site and you want to add Dynamic Data validation and templates using little or no coding.

Customize appearance and behavior of an individual data field

Walkthrough: Customizing Data Field Appearance and Behavior in the Data Model

This approach lets you customize how a specific field in the data model is rendered. It is a schema-specific customization in which you create a custom field template.

See a video that shows this feature: Watch Customize Data Field Appearance and Behavior.

Customize appearance and behavior for a data field type

How to: Customize ASP.NET Dynamic Data Default Field Templates

This approach lets you customize the UI of a data field type in the whole application. It is a general behavior customization in the presentation layer in which you customize the default field templates.

See a video that shows this feature: Watch Customize Default Field Templates.

Customize appearance and behavior for a non-primitive data field type

How to: Customize Data Field Appearance and Behavior For Non-Intrinsic Data Types in the Data Model

This approach lets you customize the UI for an individual data field. It is a schema-specific customization in which you map the field to a field template that is more specific than the default template for that data type. For example, you can mark a text field that contains e-mail addresses as an e-mail type.

Customize the appearance and behavior of an individual table by using a custom page template

How to: Customize the Layout of an Individual Table By Using a Custom Page Template

This approach lets you customize an individual table by modifying a default template, (such as List.aspx) for a specific data table. It is a schema-specific customization.

Customize the appearance and behavior of an individual table by using custom entity templates

Walkthrough: Customizing Table Layout Using Entity Templates

This approach lets you customize an individual table. It is a general behavior customization in which you create custom entity templates. It is an alternative to the custom page template approach and gives you a better control over the table layout.

A Visual Studio project with source code is available to accompany this topic: Dynamic Data Templates.

Customizing data field validation by using attributes, or by customizing method that process field changes, or by customizing the validation event handler

How to: Customize Data Field Validation in the Data Model

This approach lets you customize the validation of an individual data field. It is a schema-specific customization that is obtained for example by overriding the OnValidate event handler.

Run an online example of this feature: Run.

Customizing data field validation by using a custom validation attribute

How to: Customize Data Field Validation in the Data Model Using Custom Attributes

This approach lets you customize the validation of an individual data field. It is a schema-specific customization in which you apply a custom validation attribute to the data field in the data model. You use this approach when the available data-validation attributes do not satisfy your requirements.

See a run-time code example of this feature: Run.

Filtering table rows to display in a child table by using a foreign-key value from a parent table

Walkthrough: Filtering Rows in Tables That Have a Parent-Child Relationship

This approach lets you filter the table rows to display based on a foreign-key value from a parent table.

A Visual Studio project with source code is available to accompany this topic: Scaffolding Dynamic Data. .

Additional Information

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

See Also

Tasks

Walkthrough: Creating a New Dynamic Data Web Site Using Scaffolding

Walkthrough: Adding Dynamic Data Scaffolding to Existing ASP.NET Web Sites

Concepts

ASP.NET Dynamic Data Scaffolding

Customizing ASP.NET Dynamic Data