1

i am using the Microsoft Ajax Toolkit CalendarExtender control, to add calendar drop-down functionality to a regular TextBox:

<asp:TextBox ID="edStartDate" runat="server" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" 
      TargetControlID="edStartDate" />

Which works fine for most client locales. It seems that the control does a server-request in order to convert a DateTime into a localized String.

For example, today (October 1st, 2012) displays fine in Arabic 15/11/33:

enter image description here

And also displays fine in Lower Sorbian 1. 10. 2012:

enter image description here


But some locales do not display properly in .NET 1////10////2012:

enter image description here

In this case i need some sort of OnFormatDate event, that i can supply the correct localization of a date to a string. Which leads to my question:

How to override AjaxToolkit CalendarExtender date to string conversion?


Note: Don't confuse the question with the example.

  • i'm asking how to customize the conversion of a date to a string in a CalendarExtender
  • even if i'm not dealing with a bug in .NET, it doesn't change my question
  • even if i'm not dealing with a CalendarExtender, i'm still asking the question
Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

2 Answers2

1

In your page... at the top of it... you have something like:

<%@ Page Language="C#" AutoEventWireup="true" %>

Adding there something like(spanish for example)...

<%@ Page Language="C#" AutoEventWireup="true" UICulture="es" Culture="es-MX" %>

and in your scriptmanager

EnableScriptLocalization="true" EnableScriptGlobalization="true"

will pretty much overwrite the local settings...

But i guess you only want this property to set in your CalendarExtender:

Format="yyyy-MM-dd" or Format="dd/MM/yyyy" or however you like...

avi
  • 912
  • 8
  • 22
  • The page already is using the locale of the client browser (so no need to hard-code `es-MX` for `de-DE` users). – Ian Boyd Oct 03 '12 at 17:33
0

i used the same solution i used in a native Win32 application.

The CalendarExtender uses the "Short date" format (d). The fix is to work around the bug in .NET:

String format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;

format = FixDotNetDateTimeFormatStringsBug(format);

CalendarExtender1.Format = format; //starts as "d", the "Short date" format

with our helper-fixer:

public String FixDotNetDateTimeFormatStringBug(String format)
{
   //The bug in .NET is that it assumes "/" in a date pattern means "the date separator".
   //What .NET doesn't realize is that the locale strings returned by Windows
   // are the *Windows* format strings. 
   //The bug is exposed in locale's that use two slashes as for their date separator:
   //      dd//MM//yyyy
   //Which .NET misinterprets to give:
   //      30////11////2011
   //when really it should be taken literally to be:
   //      dd'//'MM'//'yyyy
   //which is what this fix does:

   return = format.Replace("/", "'/'");
}

Or, if you like more concisely:

CalendarExtender1.Format = FixDotNetDateTimeFormatStringsBug(
      CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);

Note: Any code is released into the public domain. No attribution required.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219