Nimit Joshi
Nimit Joshi

Reputation: 1046

Date Binding in TextBox in ListView

I have a ListView and in it there is a field with some date coming from the database. Now when I click on the Edit button, in the next Edit Page I want to show the exact date in the particular date field.

See the following screenshots:

Main View:

enter image description here

After clicking:

enter image description here

I have written the following code to bind the date:

<asp:TextBox runat="server" Text='<%#Bind("FirstVisitDate", "{0:MM/dd/yyyy}") %>' ID="FirstVisitDate" CssClass="form-control" TextMode="date" />

But this is not working.

Upvotes: 0

Views: 1933

Answers (2)

Vincenzo Costa
Vincenzo Costa

Reputation: 960

When using TextMode="date" you should always use the format yyyy-MM-dd (W3C standard)

<asp:TextBox Text='<%# Bind("data", "{0:yyyy-MM-dd}")%>' runat="server" ID="dataTextBox" TextMode="Date" CssClass="form-control"/>

Upvotes: 0

Ali
Ali

Reputation: 2592

Use this one instead of yours:

<asp:TextBox runat="server" 
             Text='<%# String.Format("{0:MM/dd/yyyy}", Eval("FirstVisitDate")) %>' 
             ID="FirstVisitDate" CssClass="form-control" TextMode="date" />

If you are still getting error you can convert it to DateTime see if it works:

 Text='<%# String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(Eval("FirstVisitDate")) %>' 

also we don't have TextMode="date" ?? remove that one as well.

You could also try this way, which is way better than Eval in terms of performance:

Text='<%# String.Format("{0:MM/dd/yyyy}", ((System.Data.DataRowView)Container.DataItem)["FirstVisitDate"] %>' 

if you want to show the format of the date, you should use placeholder instead like this: also you could use type as a date like below:

 <asp:TextBox runat="server" placeholder="mm/dd/yyyy" type="date" ... />

Upvotes: 1

Related Questions