文本框中的日期更改格式

发布于 2024-09-05 11:47:05 字数 469 浏览 2 评论 0原文

我有一个支持 4 种不同语言的应用程序 (asp.net 3.5)。与其他文化变化一起,日期格式必须与报告页面上的当前文化相匹配。

我们将每个文本框的日期格式设置为:

string date = DateTime.Today.ToString("d"); //returns the date portion only

textbox1.Text = date;
textbox2.Text = date;

etc...

当用户选择西班牙语或英国英语时,格式应为 dd/mm/yyyy。但是,然后我导航到它以 mm/dd/yyyy 格式显示的页面。回发后,它会显示 dd/mm/yyyy。另一次回发后,它会切换到 mm/dd/yyyy 格式,然后不断地切换。

我已经对此进行了调试,我发现应用程序的区域性是正确的,并且日期格式正确地返回给我,但是当它显示时,它显示不正确。

有谁见过这个或知道发生了什么?

I have an application (asp.net 3.5) that support 4 different languages. Along with other cultural changes, the date formats must match the current culture on out reporting pages.

We set the date formats of each of the textboxes like:

string date = DateTime.Today.ToString("d"); //returns the date portion only

textbox1.Text = date;
textbox2.Text = date;

etc...

When the user selects Spanish or British English the format should be dd/mm/yyyy. However, then I navigate to the page it displays in mm/dd/yyyy. After a postback it then displays dd/mm/yyyy. After another postback it switches to the mm/dd/yyyy format and on and on.

I have debugged through this and I see that the culture is correct for the application and the date formats are returned to me correctly, yet when it displays, it displays incorrectly.

Has anyone ever seen this or know what is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-09-12 11:47:05

如果您仅更改一页的区域性,则应覆盖问题中的 aspx 页面的 InitializeCulture

protected override void InitializeCulture()
{
    // set your culture, or pick it from maybe the Request object
    string selectedLanguage = "en-US"; 
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
    base.InitializeCulture();
}

如果您想为整个应用程序设置区域性,请在全局中使用 Application_BeginRequest。阿萨克斯。

void Application_BeginRequest(Object sender, EventArgs e)
{
   string selectedLanguage = "en-US"; 
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
   Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}

您当然可以将 en-US 更改为适合您的用户的正确文化。

If you are changing culture for just one page you should override InitializeCulture for the aspx pages in questions:

protected override void InitializeCulture()
{
    // set your culture, or pick it from maybe the Request object
    string selectedLanguage = "en-US"; 
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
    base.InitializeCulture();
}

If you want to set culture for the whole app, use Application_BeginRequest in global.asax.

void Application_BeginRequest(Object sender, EventArgs e)
{
   string selectedLanguage = "en-US"; 
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
   Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}

You would of course change en-US to the correct culture for your users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文