Formatting DataBinder.Eval data

后端 未结 10 1130
眼角桃花
眼角桃花 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

    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.

提交回复
热议问题