Home > Articles > Programming > Windows Programming

ASP.NET Dynamic Data Unleashed: Field Templates

  • Print
  • + Share This
Field template is a special type of ASP.NET user control that encapsulates presentation and validation logic for a single field. In this chapter, Oleg Sych and Randy Patterson show you how to use them.
This chapter is from the book

This chapter is from the book

Enterprise applications deal with employees, managers, emergency contacts, prospects, customers, suppliers, and vendors—in other words, people and organizations, who all have names, addresses, and phone numbers. Implementing data entry logic for these attributes is repetitive, and for years, the ASP.NET developers have been developing user controls to reduce the code duplication. It is common to see user controls with text boxes and validators for entering street, city, state, and postal code in many web applications today.

Although they are a great mechanism for reusing presentation and validation logic, user controls require a certain amount of plumbing before they can be used on a web page. At the very minimum, user controls must be registered, and because even the simplest controls need to provide slightly varying functionality in different contexts, they also usually have one or more properties that need to be configured. As the user controls get more and more granular, the amount of plumbing work required to apply them increases. It quickly reaches the point of diminishing returns, and developers usually stop short of implementing user controls for the individual data entry fields.

Field template is a special type of ASP.NET user control that encapsulates presentation and validation logic for a single field. Unlike the traditional user controls, field templates do not require registration. Instead, a simple DynamicControl or a DynamicField placed on a web page automatically loads and configures an appropriate field template based the column name and metadata.

Metadata information, readily available in Dynamic Data web applications, dramatically lowers the amount of plumbing code developers have to write when working with user controls, enables effective use of smaller user controls, and takes code reuse to an unprecedented new level.

Read-Only Templates

By convention, field templates are located in the DynamicData\FieldTemplates folder of the Dynamic Data web application projects and websites. A single instance of a field template provides user interface for one particular field value. Consider Listing 3.1, which shows markup file of the DateTime field template.

Listing 3.1. DateTime Field Template (Markup)

<%@ Control Language="C#" CodeBehind="DateTime.ascx.cs"
  Inherits="WebApplication.DynamicData.FieldTemplates.DateTimeField" %>

<asp:Literal runat="server" ID="literal" Text="<%# FieldValueString %>" />

This is a read-only field template, the simplest type of field template. It relies on ASP.NET data binding syntax <%# %> to display the field value in a single Literal control. Listing 3.2 shows the code-behind of the DateTime field template. You can see that it inherits from the FieldTemplateUserControl, a special base class provided for field templates by Dynamic Data. This class defines the FieldValueString property to which the Literal control is bound.

Listing 3.2. DateTime Field Template (Code-behind)

using System.Web.DynamicData;
using System.Web.UI;

namespace WebApplication.DynamicData.FieldTemplates
  public partial class DateTimeField : FieldTemplateUserControl
    public override Control DataControl
      get { return this.literal; }

Code-behind files perform most of the work required to make the generated web pages dynamic. In this simplest example, the template overrides the DataControl property defined by the base class. Entity templates (see Chapter 4, “Entity Templates”) use this property when dynamically generating entity forms, to associate each field template with a matching Label control.

  • + Share This
  • 🔖 Save To Your Account