Dot-Net

在 ListView 內的 Bind 語句中格式化日期

  • February 7, 2013

有沒有辦法可以格式化綁定在 ListView 中的日期?

我有這個 ListView 的片段

<ListView ID="lvView" runat="server">
   <ItemTemplate>
       //... some bounded data
       <asp:Label ID="lblDate" runat="server" Text='<%# Bind("RequiredDate") %>' />
       //... another bounded data
   </ItemTemplate>
</ListView>

因為RequiredDateDateTime會顯示這樣的東西10/20/2010 11:08:55 AM

我想要的是格式化該日期以輸出類似這樣的內容Oct. 20, 2010。通常,如果它是 DateTime 我可以寫這樣的東西,requiredDate.ToString("MMMM dd, yyyy") 但在 ListView 綁定數據中我不能這樣做。

我不想使用 OnItemDatabound。我只是希望它被格式化為內聯。這可能嗎?

應該像…

Text='<%# Bind("RequiredDate", "{0:MMM dd, yyyy}") %>'

這應該工作

Text='<%# Bind("DateOfBirth", "{0:MMM dd, yyyy}") %>'

引用自:https://stackoverflow.com/questions/3974481