为什么我的 ASP.Net 日期格式变化不规律?
我有一个绑定到 SqlDataSource 的 asp.net 4.0 Formview,托管在 IIS7 上,并且我显示一个用于编辑的日期,如下所示:
<input id="OrderDateTextBox" type="text" value='<%# Eval("OrderDate", "{0:d}") %>' />
这通常按预期工作,但有时日期格式显示 d/m/y 而不是 m/日/年。发生这种情况时,页面刷新将显示正确的日期格式。
到目前为止,我已经尝试在 web.config 中设置默认区域性,如下所示:
<globalization enableClientBasedCulture="false" culture="en-US" uiCulture="en-US" />
我还尝试在初始化时设置区域性:
protected override void InitializeCulture()
{
UICulture = "en-US";
Culture = "en-US";
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture =
new CultureInfo("en-US");
base.InitializeCulture();
}
这些方法似乎都没有任何效果。但是,如果我使用以下语法提取值,则日期格式始终正确:
<%# DateTime.Parse(Eval("OrderDate").ToString()).ToString("d", CultureInfo.CreateSpecificCulture("en-US")) %>
但这不是必需的。感谢您的任何帮助。
I have an asp.net 4.0 Formview bound to a SqlDataSource, hosted on IIS7, and I'm displaying a date for editing like this:
<input id="OrderDateTextBox" type="text" value='<%# Eval("OrderDate", "{0:d}") %>' />
This generally works as expected, but occasionally the date format displays d/m/y instead of m/d/y. When this happens, a page refresh will show the correct date format.
So far I've tried setting the default culture in web.config like this:
<globalization enableClientBasedCulture="false" culture="en-US" uiCulture="en-US" />
I've also tried setting the culture on initialization:
protected override void InitializeCulture()
{
UICulture = "en-US";
Culture = "en-US";
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture =
new CultureInfo("en-US");
base.InitializeCulture();
}
Neither of those methods seem to have any effect. However, if I pull in the value by using the following syntax the date format is correct all the time:
<%# DateTime.Parse(Eval("OrderDate").ToString()).ToString("d", CultureInfo.CreateSpecificCulture("en-US")) %>
But that shouldn't be necessary. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将日期强制采用您希望的格式,而不是使用
{0:d}
并根据区域性来设置日期格式。例子:Instead of using
{0:d}
and depending on the culture to format the date, you can try forcing the date into the formatting you wish. Example: