在FormView控件的EditItemTemplate格式日期 [英] Format Date in FormView EditItemTemplate
问题描述
这似乎应该是简单,但我在一个总损失并就如何贯彻落实MSDN例如 FormView.EditItemTemplate
并没有解决格式在所有。我有一个文本框
我能够绑定到类型的字段日期
像这样:
< ASP:文本框ID =TextBox1的=服务器文本='<%#绑定(了checkdate)%>' />
不过我不使用时间部分,并想在编辑模式下存在的价值,显示为短日期格式。现在,我得到MM / DD / YYYY HH:MM:SS。
尝试添加格式code到绑定
前pression导致编译错误,但我不知道任何其他方式来设置一个在 FormView.EditItemTemplate
绑定。救命啊!
文本='<%#绑定(了checkdate,{0:MM / DD / YYYY} )%GT;'
应该工作
但你也可以做到这一点在codebehind更可读的方式(IMO ToShortDateString
比更可读的绑定(了checkdate ,{0:MM / DD / YYYY})
:
私人小组FormView1_DataBound(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手FormView1.DataBound
选择案例FormView1.CurrentMode
案例FormViewMode.Edit
昏暗的博士= DirectCast(FormView1.DataItem,DataRowView的)
昏暗的TextBox1中= DirectCast(FormView1.FindControl(TextBox1中),文本框)
暗淡了checkdate = DirectCast(DR(了checkdate),日期)
TextBox1.Text = CheckDate.ToShortDateString()
结束选择
结束小组
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
这篇关于在FormView控件的EditItemTemplate格式日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!