【问题标题】:converting a DetailsView TemplateField cell value to short date string将 DetailsView TemplateField 单元格值转换为短日期字符串
【发布时间】:2010-09-07 08:42:46
【问题描述】:

我有一个 detailsView,其单元格中的日期值当前以 longDateFormat 显示,我想将此 DetailsView 中的所有日期值转换为短日期。

例如,我只想显示 6/1/2010

,而不是 6/1/2010 12:00:00 AM

对于 Gridview,我可以通过代码来实现

  Protected Sub DetailsView4_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView4.DataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        For i As Integer = 0 To e.Row.Cells.Count - 1
            Dim cellDate As Date
            If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
                e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
            End If
        Next
    End If

End Sub

如何使用 DetailsView 实现相同的功能?

【问题讨论】:

    标签: c# asp.net detailsview date-format string.format


    【解决方案1】:

    可以很简单的实现,如果是在模板文件里的话..

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("tDate", "{0:MM/dd/yyyy}") %>'></asp:TextBox>
    

    如果不是模板字段则

    <asp:BoundField DataField="tDate" HeaderText="tDate" SortExpression="tDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" />
    

    【讨论】:

      猜你喜欢
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2013-12-28
      • 2019-03-29
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多