Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Date in FormView EditItemTemplate

Tags:

asp.net

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!

like image 965
pseudocoder Avatar asked Jul 08 '11 15:07

pseudocoder


1 Answers

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
like image 181
Tim Schmelter Avatar answered Oct 15 '22 00:10

Tim Schmelter