简体   繁体   English

ListView中TextBox中的日期绑定

[英]Date Binding in TextBox in ListView

I have a ListView and in it there is a field with some date coming from the database. 我有一个ListView,并且其中有一个字段,其中某些日期来自数据库。 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: 主视图:

在此处输入图片说明

After clicking: 单击后:

在此处输入图片说明

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. 但这是行不通的。

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: 如果仍然出现错误,可以将其转换为DateTime,看看它是否有效:

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

also we don't have TextMode="date" ?? 我们也没有TextMode =“ date” ?? remove that one as well. 还要删除那个。

You could also try this way, which is way better than Eval in terms of performance: 您也可以尝试这种方式,在性能方面比Eval更好:

 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: 如果要显示日期格式,则应使用占位符,例如:也可以使用type作为日期,如下所示:

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

使用TextMode =“ date”时,您应始终使用yyyy-MM-dd格式( W3C标准

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM