Date Format in FormView EditItemTemplate

It seems like it should be simple, but I am in full measure, and the MSDN example on how to implement FormView.EditItemTemplatedoes not concern formatting at all. I have an TextBoxI can associate with a type field Dateas follows:

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CheckDate") %>' />

However, I do not use the time component and want the existing value in editing mode to be displayed as a short date format. Right now I get "MM / dd / yyyy hh: mm: ss".

Attempts to add formatting code to the expression Bindresult in a compilation error, but I don’t know of any other way to configure the binding in FormView.EditItemTemplate. Help!

+5
source share
3 answers
Text='<%# Bind("CheckDate", "{0:MM/dd/yyyy}") %>'

must work

codebehind (IMO ToShortDateString , Bind("CheckDate", "{0:MM/dd/yyyy}"):

Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        Select Case FormView1.CurrentMode
            Case FormViewMode.Edit
                Dim dr = DirectCast(FormView1.DataItem,DataRowView)
                Dim TextBox1 = DirectCast(FormView1.FindControl("TextBox1"), TextBox)
                Dim CheckDate = DirectCast(dr("CheckDate"), Date)
                TextBox1.Text = CheckDate.ToShortDateString()
        End Select
   End Sub
+15

bind

<%# Bind("CheckDate", "{0:MM/dd/yyyy}") %>

,

+4

You can use {0:d}the date format when binding or define the culture in Web.config to use the MM / dd / yyyy format.

+1
source

All Articles