Databases Reference
In-Depth Information
The Edit field templates are significantly more complex than their read-only counterparts.
Consider Listing 3.5, which shows the Edit version of the
DateTime
field template.
LISTING 3.5
DateTime_Edit Field Template (Markup)
<%@ Control Language=”C#” CodeBehind=”DateTime_Edit.ascx.cs”
Inherits=”WebApplication.DynamicData.FieldTemplates.DateTime_EditField” %>
<asp:TextBox ID=”textBox” runat=”server” Text='<%# FieldValueEditString %>'
Columns=”20” />
<asp:RequiredFieldValidator runat=”server” ID=”requiredFieldValidator”
ControlToValidate=”textBox” Enabled=”false” />
<asp:RegularExpressionValidator runat=”server” ID=”regularExpressionValidator”
ControlToValidate=”textBox” Enabled=”false” />
<asp:DynamicValidator runat=”server” ID=”dynamicValidator”
ControlToValidate=”textBox” />
<asp:CustomValidator runat=”server” ID=”dateValidator” ControlToValidate=”textBox”
EnableClientScript=”false” Enabled=”false”
OnServerValidate=”DateValidator_ServerValidate” />
In addition to the data entry control, the edit field templates typically have one or more
validation controls. The
DateTime_Edit
template includes a
RequiredFieldValidator
, a
RegularExpressionValidator
, a
DynamicValidator
, and a
CustomValidator
control.
Notice that with the exception of the
DynamicValidator
, these controls are not enabled by
default. They are configured dynamically by the
Page_Load
event handler defined in the
code-behind file shown in Listing 3.6.
Each validator control is configured by a call to the
SetUpValidator
method of the
FieldTemplateUserControl
base class. This method enables and configures the validator if
the column metadata contains an appropriate data annotation attribute. In particular, the
RequiredFieldValidator
will be enabled when the column is marked with a
RequiredAttribute
that does not allow empty strings, and the
RegularExpressionValidator
will be enabled when the column is marked with a
RegularExpressionAttribute
. In addi-
tion to enabling the validator, the
SetUpValidator
method also assigns its
ErrorMessage
and
ToolTip
properties with an error message either explicitly specified in the data annotation
attribute or generated based on the column's display name.
DynamicValidator
is a special validation control provided by Dynamic Data. It ensures
all validation attributes applied to the column are invoked to verify its value is correct.
This andincludes not only the well-known
RequiredAttribute
,
RangeAttribute
, and
CustomValidationAttribute
, but also all other descendants of the
ValidationAttribute
that may be applied to the column and are not covered by a specific ASP.NET validator
control in this field template.