Databases Reference
In-Depth Information
LISTING 3.3
Continued
public class Metadata
{
[DisplayFormat(DataFormatString = “{0:D}”)]
public object OrderDate;
[DataType(DataType.Date)]
public object RequiredDate;
}
}
}
Display format properties can be also specified in page markup by setting the correspond-
ing properties of the
DynamicControl
or
DynamicField
classes, similar to the properties of
the “non-dynamic”
BoundField
. Listing 3.4 shows an example where a
DynamicField
is
used in a
GridView
control and specifies a custom date format string
{0:MM/dd/yy}
for the
ShippedDate column.
LISTING 3.4
Specifying Data Format Using DynamicField
<%@ Page Language=”C#”
MasterPageFile=”~/Site.master” CodeBehind=”SamplePage.aspx.cs”
Inherits=”WebApplication.Samples.Ch01.DataFormatString.SamplePage” %>
<asp:Content ContentPlaceHolderID=”ContentPlaceHolder1” runat=”server”>
<asp:GridView ID=”gridView” runat=”server” DataSourceID=”dataSource”
AutoGenerateColumns=”false” AllowPaging=”true”>
<columns>
<asp:DynamicField DataField=”OrderDate” />
<asp:DynamicField DataField=”RequiredDate” />
<asp:DynamicField DataField=”ShippedDate” DataFormatString=”{0:MM/dd/yy}” />
</columns>
</asp:GridView>
<asp:EntityDataSource ID=”dataSource” runat=”server”
ConnectionString=”name=NorthwindEntities”
DefaultContainerName=”NorthwindEntities” EntitySetName=”Orders” />
</asp:Content>
Figure 3.1 shows the web page generated by this code. As you can see, the
OrderDate
values use the long format specified in the
DisplayFormatAttribute
in the model; the
RequiredDate
values use the short format provided by the
DataTypeAttribute
; and the
ShippedDate
values use the custom format specified in the page markup.