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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With