Field Templates are the user interface for displaying and editing DataFields.
They apply the business rules associated with the
DataAnnotations on the DataFields.
Their job is to ensure the Web Form developer does not deal with business rules
issues, but still have control over the user interface.
Field Templates are actually ASP.NET User Control files that inherit from base classes
that handle the business rules. Their own content is the HTML and web controls that
provide the correct user interface.
Field Template files are associated with data
types, such as date, string, decimal, and integer. There can be multiple Field Templates
with different user interfaces for the same data type, such as a DateTextBox in
one and a Calendar in another.
Here is the actual code from the Date_Edit.ascx Field Template. It edits a date
DataField. It's base class, PeterBlum.DES.BLD.DateEditFieldTemplate, has a rich
toolset to support the DateTextBox control
that is part of Peter's Data Entry Suite.
As a result, this file has very little code.
The BLDDataFieldValidators control converts the
ValidationAttributes into Validator web controls,
ensuring your Field Template provides the validation required by the business rules.
BLD always uses the
DES Validation Framework
for its Validator controls, to ensure a rich user interface.
<%@ Control Language="C#" AutoEventWireup="true" Inherits="PeterBlum.DES.BLD.DateEditFieldTemplate" %>
protected void Page_Init(object sender, EventArgs e)
<des:DateTextBox ID="DateTextBox1" runat="server" />
<des:BLDDataFieldValidators ID="BLDDataFieldValidators1" runat="server" />
Your web application has a library of Field Templates in its [web app]\BLD Templates\FieldTemplates
folder. The one selected for a DataField depends on the
or data type of the DataField’s property. For example, a property of System.DateTime will
use the file named “DateTime.ascx” when it does not have a DataTypeAttribute. By
assigning the DateDataTypeAttribute, it switches to the “Date.ascx” file.
The BLDDataField control inserts a Field Template into the page. Its properties
let you override the default Field Template (UIHint), change the default style sheets
(NamedStyles), set property values of the Field Template (TemplateProperties),
introduce Field Template Behaviors (Behaviors) and
customize the error messages (BLDDataFieldValidatorsControlID). With so many ways
to customize the Field Template built into BLDDataField, you will not need to create
a Field Template for each unique case.
You will learn more about BLDDataField and these properties as you go through this Guided Tour.
In the next topic, you'll explore
the Field Templates that are included with BLD.