Question

I have one Repeater

 <asp:Repeater ID="repID" runat="server">
           <ItemTemplate>
                 <tr>
                       <td>
   <%# DataBinder.Eval(Container, "DataItem.DateCreated.ToShortDateString()")%>
                       </td>
                       <td>
   <%# DataBinder.Eval(Container, "DataItem.Comment")%>
                        </td>
                </tr>
           </ItemTemplate>
</asp:Repeater>

I want to show field DateCreated ToShortDateString(), but it gives me this error:

DataBinding: 'ToShortDateString()' is not a valid indexed expression.

how transform DateTime to ToShortDateString() ?

Was it helpful?

Solution

There is an overloaded method that accepts a third parameter - a format string:

<%# DataBinder.Eval(Container, "DataItem.DateCreated", "{0:d}") %>

The format specifier "d" outputs the date using the short date pattern. You can find other specifiers on MSDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top