In the olden days before asp.net 4.5 we could easily bind a date to a Gridview, Formview or other databound control and make it look presentable with a simple format string.
<asp:TextBox
ID="DateFieldTextBox"
runat="server"
Text='<%# Bind("DateField","{0:d}") %>'/>
The new, strongly typed model binding is a bit more difficult.
<asp:TextBox
ID="DateFieldTextBox"
runat="server"
Text='<%# BindItem.DateField %>'/>
will work, but produces the full date/time string rather than just the date.
<asp:TextBox
ID="DateFieldTextBox"
runat="server"
Text='<%# BindItem.DateField.ToShortDateString() %>'/>
.. this should work, but it produces a compile time error "Invalid code syntax for BindItem". Item.DateField.ToShortDateString() works but does not bind on the postback.
For now we've reverted to the old syntax, but we would love to get the modern compile time checking but still be able to format the dates nicely. Anyone else ran into this before?
For displaying data in grid view, You can do Item.DateField.ToShortString() and it will work as it is one way data binding. i.e you are displying what is in the record already. BindItem.DateField.ToDateString() will convert object from DateTime to string which will result in error as you are already seeing. You can keep using Bind(expression,format) in formview while editing field or for accpting new entry.
I struggled with this for quite some time. My solution was to combine Data Annotations in the data model along with the use of a DynamicControl in my FormView. I've assumed this is a WebForms project vs. an MCV project.
Read a little on the realm of data access in WebForms. It's one of MS's better write-ups on WebForms data access/presentation controls and includes references to the DynamicControl.
//I know you want this, and I did too. It seems like it could/should work, but it doesn't
<asp:TextBox
ID="DateFieldTextBox"
runat="server"
Text='<%# BindItem.DateField.ToShortDateString() %>'/>
//This works, but you also need to use the Data Annotations in your model.
<asp:DynamicControl
ID="DateFieldTextBox"
runat="server"
DataField="DateField"
Mode="Edit" />
//sample model annotations in your object class
[Column(TypeName = "date"), DataType(DataType.Date), DisplayFormat(DataFormatString = " {0:MM/dd/yyyy}", ApplyFormatInEditMode = true )]
public DateTime DateField { get; set; }
I'll be using this pattern because I need two-way model binding with the ability to present dates in the right format (as well as currency, etc.). It is really unfortunate that the TextBox control doesn't handle formatting the model data like this fully. DataAnnotations are ignored in this context. Two-way binding works if there's no formatting, but not if you need formatting. It seems like an incomplete WebForms implementation IMO.
On the otherhand, using the DynamicControl works, but you get no Intellisense on the DataField property that would provide you members of the model while coding. What???? Another incomplete, and less preferred, solution.
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