Databases Reference
In-Depth Information
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.
In addition to the
FieldValueString
property, the
FieldTemplateUserControl
class also
provides another property called
FieldValue
, which returns the raw value object. At a
cursory glance, the
FieldValue
property could be used to bind the
Literal
control as
well. However, it is important to use the
FieldValueString
property as it automatically
takes into account additional display format properties when converting the raw field
value to a string:
.
DataFormatString
specifies a .NET format string that will be passed to the
String.Format
method to convert the field value to a display string.
.
NullDisplayText
specifies a replacement string that will be used when the raw field
value is null.
.
ConvertEmptyStringToNull
is a Boolean value that determines whether an empty
string value should treated as null and if the
NullDisplayText
will be used as a
replacement for empty strings as well as null values. This property is
true
by default.
.
HtmlEncode
is a Boolean value that determines whether any special HTML symbols
in the raw field value, such as
'<'
or
'>'
, will be encoded using escape sequences,
such as
<
and
>
. This property is
true
by default.
Display format properties can be specified in the data model by applying the
DataTypeAttribute
or the
DisplayFormatAttribute
to a particular column, as shown in
Listing 3.3. The
DisplayFormatAttribute
, applied to the OrderDate column, specifies a
long date
{0:D}
format string. The
DataTypeAttribute
, applied to the RequiredDate
column, does not have its own
DataFormatString
property. Instead, it offers the
DisplayFormat
property, which gets a default
DisplayFormatAttribute
generated, based
on the specified data type. For
DataType.Date,
the default data format string will be a
short date
{0:d}
.
LISTING 3.3
Specifying Display Format Using Data Annotations
using System.ComponentModel.DataAnnotations;
namespace DataModel
{
[MetadataType (typeof(Order.Metadata))]
partial class Order
{