Enabling Dynamic Data for Existing Projects

Dynamic Data features that shipped in the .NET Framework 3.5 SP1 brought new features such as the following:

· Field templates – These provide data-type-based templates for data-bound controls. Field templates provide a simpler way to customize the look of data controls than using template fields for each field.

· Validation – Dynamic Data lets you use attributes on data classes to specify validation for common scenarios like required fields, range checking, type checking, pattern matching using regular expressions, and custom validation. Validation is enforced by the data controls.

However, these features had the following requirements:

· The data-access layer had to be based on Entity Framework or LINQ to SQL.

· The only data source controls supported for these features were the EntityDataSource or LinqDataSource controls.

· The features required a Web project that had been created using the Dynamic Data or Dynamic Data Entities templates in order to have all the files that were required to support the feature.

A major goal of Dynamic Data support in ASP.NET 4 is to enable the new functionality of Dynamic Data for any ASP.NET application. The following example shows markup for controls that can take advantage of Dynamic Data functionality in an existing page.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"

DataKeyNames="ProductID" DataSourceID="LinqDataSource1">

</asp:GridView>

<asp:LinqDataSource ID="LinqDataSource1" runat="server"

ContextTypeName="DataClassesDataContext" EnableDelete="True" EnableInsert="True"

EnableUpdate="True" TableName="Products">

</asp:LinqDataSource>

In the code for the page, the following code must be added in order to enable Dynamic Data support for these controls:

GridView1.EnableDynamicData(typeof(Product));

When the GridView control is in edit mode, Dynamic Data automatically validates that the data entered is in the proper format. If it is not, an error message is displayed.

This functionality also provides other benefits, such as being able to specify default values for insert mode. Without Dynamic Data, to implement a default value for a field, you must attach to an event, locate the control (using FindControl), and set its value. In ASP.NET 4, the EnableDynamicData call supports a second parameter that lets you pass default values for any field on the object, as shown in this example:

DetailsView1.EnableDynamicData(typeof(Product), new { ProductName = "DefaultName" });

Posted in: programming asp.net | Tags: asp.net 4.0 linqdatasource dynamic data .net 3.5 entitydatasource

Entity Templates in Dynamic Data

Entity templates offer a new way to customize the layout of data without requiring you to create a custom page. Page templates use the FormView control (instead of the DetailsView control, as used in page templates in earlier versions of Dynamic Data) and the DynamicEntity control to render Entity templates. This gives you more control over the markup that is rendered by Dynamic Data.

The following list shows the new project directory layout that contains entity templates:

\DynamicData\EntityTemplates

\DynamicData\EntityTemplates\Default.ascx

\DynamicData\EntityTemplates\Default_Edit.ascx

\DynamicData\EntityTemplates\Default_Insert.ascx

The EntityTemplate directory contains templates for how to display data model objects. By default, objects are rendered by using the Default.ascx template, which provides markup that looks just like the markup created by the DetailsView control used by Dynamic Data in ASP.NET 3.5 SP1. The following example shows the markup for the Default.ascx control:

<asp:EntityTemplate runat="server" ID="TemplateContainer1">

<ItemTemplate>

<tr

<td>

<asp:Label ID="Label1" runat="server" OnInit="Label_Init" />

</td>

<td>

<asp:DynamicControl runat="server" OnInit="DynamicControl_Init" />

</td>

</tr>

</ItemTemplate>

</asp:EntityTemplate>

The default templates can be edited to change the look and feel for the entire site. There are templates for display, edit, and insert operations. New templates can be added based on the name of the data object in order to change the look and feel of just one type of object. For example, you can add the following template:

\DynamicData\EntityTemplates\Products.aspx

The template might contain the following markup:

<tr>

<td>Name</td>

<td><asp:DynamicControl runat="server" DataField="ProductName" /></td>

<td>Category</td>

<td><asp:DynamicControl runat="server" DataField="Category" /></td>

</tr>

The new entity templates are displayed on a page by using the new DynamicEntity control. At run time, this control is replaced with the contents of the entity template. The following markup shows the FormView control in the Detail.aspx page template that uses the entity template. Notice the DynamicEntity element in the markup.

<asp:FormView runat="server" ID="FormView1"

DataSourceID="DetailsDataSource"

OnItemDeleted="FormView1_ItemDeleted">

<ItemTemplate>

<table class="DDDetailsTable" cellpadding="6">

<asp:DynamicEntity runat="server" />

<tr class="td">

<td colspan="2">

<asp:DynamicHyperLink ID="EditHyperLink" runat="server"

Action="Edit" Text="Edit" />

<asp:LinkButton ID="DeleteLinkButton" runat="server"

CommandName="Delete"

CausesValidation="false"

OnClientClick='return confirm("Are you sure you want to delete this item?");'

Text="Delete" />

</td>

</tr>

</table>

</ItemTemplate>

</asp:FormView>

Posted in: asp.net | Tags: dynamic data entitydatasource entity templates entitytemplates dynamiccontrol formview deletelinkbutton

Some new features in Dynamic Data 4.0

Dynamic Data

Dynamic Data was introduced in the .NET Framework 3.5 SP1 release in mid-2008. This feature provides many enhancements for creating data-driven applications, including the following:

· A RAD experience for quickly building a data-driven Web site.

· Automatic validation that is based on constraints defined in the data model.

· The ability to easily change the markup that is generated for fields in the GridView and DetailsView controls by using field templates that are part of your Dynamic Data project.

Note   For more information, see the Dynamic Data documentation in the MSDN Library.

For ASP.NET 4, Dynamic Data has been enhanced to give developers even more power for quickly building data-driven Web sites.

Declarative DynamicDataManager Control Syntax

The DynamicDataManager control has been enhanced so that you can configure it declaratively, as with most controls in ASP.NET, instead of only in code. The markup for the DynamicDataManager control looks like the following example:

<asp:DynamicDataManager ID="DynamicDataManager1" runat="server"

AutoLoadForeignKeys="true">

<DataControls>

<asp:DataControlReference ControlID="GridView1" />

</DataControls>

</asp:DynamicDataManager>

<asp:GridView id="GridView1" runat="server"

</asp:GridView>

This markup enables Dynamic Data behavior for the GridView1 control that is referenced in the DataControls section of the DynamicDataManager control.

New Field Templates for URLs and E-mail Addresses

ASP.NET 4 introduces two new built-in field templates, EmailAddress.ascx and Url.ascx. These templates are used for fields that are marked as EmailAddress or Url with the DataType attribute. For EmailAddress objects, the field is displayed as a hyperlink that is created by using the mailto: protocol. When users click the link, it opens the user's e-mail client and creates a skeleton message. Objects typed as Url are displayed as ordinary hyperlinks.

The following example shows how fields would be marked.

[DataType(DataType.EmailAddress)]

public object HomeEmail { get; set; }

[DataType(DataType.Url)]

public object Website { get; set; }

Creating Links with the DynamicHyperLink Control

Dynamic Data uses the new routing feature that was added in the .NET Framework 3.5 SP1 to control the URLs that end users see when they access the Web site. The new DynamicHyperLink control makes it easy to build links to pages in a Dynamic Data site. The following example shows how to use the DynamicHyperLink control:

<asp:DynamicHyperLink ID="ListHyperLink" runat="server"

Action="List" TableName="Products">

Show all products

</asp:DynamicHyperLink>

This markup creates a link that points to the List page for the Products table based on routes that are defined in the Global.asax file. The control automatically uses the default table name that the Dynamic Data page is based on.

Support for Inheritance in the Data Model

Both the Entity Framework and LINQ to SQL support inheritance in their data models. An example of this might be a database that has an InsurancePolicy table. It might also contain CarPolicy and HousePolicy tables that have the same fields as InsurancePolicy and then add more fields. Dynamic Data has been modified to understand inherited objects in the data model and to support scaffolding for the inherited tables.

Support for Many-to-Many Relationships (Entity Framework Only)

The Entity Framework has rich support for many-to-many relationships between tables, which is implemented by exposing the relationship as a collection on an Entity object. New ManyToMany.ascx and ManyToMany_Edit.ascx field templates have been added to provide support for displaying and editing data that is involved in many-to-many relationships.

New Attributes to Control Display and Support Enumerations

The DisplayAttribute has been added to give you additional control over how fields are displayed. The DisplayName attribute in earlier versions of Dynamic Data allowed you to change the name that is used as a caption for a field. The new DisplayAttribute class lets you specify more options for displaying a field, such as the order in which a field is displayed and whether a field will be used as a filter. The attribute also provides independent control of the name used for the labels in a GridView control, the name used in a DetailsView control, the help text for the field, and the watermark used for the field (if the field accepts text input).

The EnumDataTypeAttribute class has been added to let you map fields to enumerations. When you apply this attribute to a field, you specify an enumeration type. Dynamic Data uses the new Enumeration.ascx field template to create UI for displaying and editing enumeration values. The template maps the values from the database to the names in the enumeration.

Enhanced Support for Filters

Dynamic Data 1.0 shipped with built-in filters for Boolean columns and foreign-key columns. The filters did not allow you to specify whether they were displayed up or in what order they were displayed. The new DisplayAttribute attribute addresses both of these issues by giving you control over whether a column shows up as a filter and in what order it will be displayed.

An additional enhancement is that filtering support has been rewritten to use the new QueryExtender feature of Web Forms. This lets you create filters without requiring knowledge of the data source control that the filters will be used with. Along with these extensions, filters have also been turned into template controls, which allows you to add new ones. Finally, the DisplayAttribute class mentioned earlier allows the default filter to be overridden, in the same way that UIHint allows the default field template for a column to be overridden.

Posted in: asp.net | Tags: queryextender dynamic data dynamic data 4.0 detailsview dynamicdatamanager syntax gridview urls and e-mail addresses datatype.emailaddress dynamichyperlink data model inheritance many-to-many relationships enumerations filters new attributes