2010-09-07 71 views
1

我有一個detailsView的單元格中的日期值當前正在顯示在longDateFormat中,我想將此DetailsView中的所有日期值轉換爲短日期。將DetailsView TemplateField單元格值轉換爲短日期字符串

例如,代替2010/6/1 12:00:00 AM,我想顯示只是2010/6/1

對於一個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相同?

回答

2

它可以簡單地實現,如果是在申請模板..

<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" />