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