2

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.

2
  • Are you certain these are random? Anything in common to the occurrences you have seen? Commented Oct 8, 2010 at 18:31
  • Well, it seems to occur after periods of inactivity, maybe after the session expires? Commented Oct 8, 2010 at 19:06

1 Answer 1

1

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}") %>
Sign up to request clarification or add additional context in comments.

2 Comments

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!
I'm not an expert on .NET culture changes, but I do know how to format dates. Glad this helped you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.