views:

45

answers:

1

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.

A: 

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:

<%# Eval("OrderDate", "{0:MM/dd/yyyy}") %>
ChessWhiz
That works! I'd still like to know why it's necessary to do this, just in case the culture is affecting other settings as well, but this helps. Thanks!
Sprockincat
I'm not an expert on .NET culture changes, but I do know how to format dates. Glad this helped you!
ChessWhiz