views:

57

answers:

1

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?

A: 

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.

Mikael Svenson