如何将AjaxToolkit CalendarExtender日期重写为字符串转换?

时间:2012-10-01 17:05:21

标签: asp.net localization calendarextender

我正在使用Microsoft Ajax Toolkit CalendarExtender控件,将日历下拉功能添加到常规TextBox中:

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

适用于大多数客户端区域设置。似乎控件执行服务器请求以将DateTime转换为本地化的String

例如,今天(2012年10月1日)在阿拉伯语15/11/33中显示正常:

enter image description here

并且在下索布语1. 10. 2012显示正常:

enter image description here


但是some locales do not display properly in .NET 1////10////2012

enter image description here

在这种情况下,我需要某种OnFormatDate事件,我可以为字符串提供正确的日期本地化。这引出了我的问题:

  

如何将AjaxToolkit CalendarExtender日期重写为字符串转换?


注意:请勿将问题与示例混淆。

  • 我问如何自定义将日期转换为CalendarExtender
  • 中的字符串
  • 即使我没有处理.NET中的错误,它也不会改变我的问题
  • 即使我没有处理CalendarExtender,我仍然会问这个问题

2 个答案:

答案 0 :(得分:1)

在你的页面中......在它的顶部......你有类似的东西:

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

添加类似的东西(例如西班牙语)......

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

并在你的剧本管理员中

EnableScriptLocalization="true" EnableScriptGlobalization="true"

几乎会覆盖本地设置......

但我猜您只想在CalendarExtender中设置此属性:

Format="yyyy-MM-dd"Format="dd/MM/yyyy"或者你喜欢......

答案 1 :(得分:0)

我使用了我在原生Win32应用程序中使用的相同解决方案。

CalendarExtender使用“短日期”格式( d )。解决方法是解决.NET中的错误:

String format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;

format = FixDotNetDateTimeFormatStringsBug(format);

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

使用我们的助手修复器:

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("/", "'/'");
}

或者,如果你更简洁地说:

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

注意:任何代码都会发布到公共域中。无需归属。