Databases Reference
In-Depth Information
When building custom forms with Dynamic Data components, you can explicitly control
appearance of values by specifying the
DataFormatString
,
NullDisplayText
, and
HtmlEncode
properties of the
DynamicControl
and
DynamicField
controls directly in
markup. With the
TextBox
-based field templates, you can also control appearance of
values in Edit and Insert mode by setting the
ApplyFormatInEditMode
and
ConvertEmptyStringToNull
properties. Here is an example used in Chapter 3, which
covers this topic in detail, to display the
ShippedDate
property of the Order entity in
MM/dd/yy format:
<asp:DynamicField DataField=”ShippedDate”
DataFormatString=”{0:MM/dd/yy}”
/>
You can also specify these values in the metadata, by applying the
DisplayFormatAttribute
to entity properties as shown here:
[MetadataType(typeof(Metadata)]
partial class Order
{
abstract class Metadata
{
[DisplayFormat(DataFormatString = “{0:MM/dd/yy}”)]
public object SubmittedDate { get; set; }
}
}
NOTE
Under the hood, the
DynamicControl
uses properties of the
DisplayFormatAttribute
applied to the column to initialize its own. In other words, if
you have a
DataFormatString
annotation specified in the metadata and in markup,
the value specified in markup takes precedence, as you would expect.
If you need values of a particular property to appear consistently on multiple pages of
your web application, the
DisplayFormatAttribute
works great because it allows you to
specify the format string in a single place. However, because this logic falls in to the
presentation category, you should use this attribute with caution. You can often avoid
using the
DisplayFormatAttribute
and embedding the presentation logic in the business
layer by using the
DataTypeAttribute
instead. For
Date
,
Time
, and
Currency
data types,
this attribute provides default format strings, which are sufficient in many cases. Here is
how you could modify metadata of the
SubmittedDate
property to use the
DataType
anno-
tation instead:
[MetadataType(typeof(Metadata)]
partial class Order