Search code examples
asp.netitemtemplate

Formatting dates in ASP.Net data binding expressions


Can you format a date when using a data binding expression in ASP.NET?

DataFormatString works for a BoundField in a GridView, but not in a ItemTemplate label using a data binding expression. Ideally I'd like to use a custom format string like {0:dd-MMM-yyyy} to format the date.

<ItemTemplate>
<asp:Label ID="lblLabel" runat="server" Text='<%# Bind("FIELD_NAME")%>'></asp:Label>
</ItemTemplate>

Solution

  • you could use

    <ItemTemplate>
    <asp:Label ID="lblLabel" runat="server" 
        Text='<%# Eval("FIELD_NAME", "{0:dd-MMM-yyyy}") %>'>
    </asp:Label>
    </ItemTemplate>