Reputation: 4402
This seems like it should be simple but I am at a total loss and the MSDN example on how to implement FormView.EditItemTemplate
does not address formatting at all. I have a TextBox
I am able to bind to a field of type Date
like so:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CheckDate") %>' />
However I am not using the time component and would like the existing value in edit mode to show up as the short date format. Right now I get "MM/dd/yyyy hh:mm:ss".
Attempts to add formatting code to the Bind
expression result in a compilation error, but I don't know any other way to set up a binding in the FormView.EditItemTemplate
. Help!
Upvotes: 7
Views: 13454
Reputation: 460238
Text='<%# Bind("CheckDate", "{0:MM/dd/yyyy}") %>'
should work
But you could also do this in codebehind in a more readable way(IMO ToShortDateString
is more readable than 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
Upvotes: 17
Reputation: 493
You can use {0:d}
in date format while binding or define the culture in Web.config to use MM/dd/yyyy format.
Upvotes: 1
Reputation: 21864
you can use a format string as a second parameter in your bind
property
<%# Bind("CheckDate", "{0:MM/dd/yyyy}") %>
a nice set of patterns you can use to format the date
Upvotes: 4