Formatting DataBinder.Eval data

后端 未结 10 1091
眼角桃花
眼角桃花 2020-12-08 04:33

How can I format data coming from a DataBinder.Eval statement in an ASPX page?

For example, I want to display the published date of the news items in a particular fo

相关标签:
10条回答
  • 2020-12-08 05:24

    There is an optional overload for DataBinder.Eval to supply formatting:

    <%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
    

    The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:

    <asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:ffffdd d MMMM}") %>'</label>
    
    0 讨论(0)
  • 2020-12-08 05:24

    You can use a function into a repeater like you said, but notice that the DataBinder.Eval returns an object and you have to cast it to a DateTime.

    You also can format your field inline:

    <%# ((DateTime)DataBinder.Eval(Container.DataItem,"publishedDate")).ToString("yyyy-MMM-dd") %>
    

    If you use ASP.NET 2.0 or newer you can write this as below:

    <%# ((DateTime)Eval("publishedDate")).ToString("yyyy-MMM-dd") %>
    

    Another option is to bind the value to label at OnItemDataBound event.

    0 讨论(0)
  • 2020-12-08 05:28
    <asp:Label ID="ServiceBeginDate" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:yyyy}") == "0001") ? "" : DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:MM/dd/yyyy}") %>'>
    </asp:Label>
    
    0 讨论(0)
  • 2020-12-08 05:29

    To format the date using the local date format use:

    <%#((DateTime)Eval("ExpDate")).ToString("d")%>
    

    How to Format an Eval Statement to Display a Date using Date Locale

    0 讨论(0)
提交回复
热议问题