2012-10-01 32 views
1

我使用微軟的Ajax工具包CalendarExtender控制,下拉式日曆功能添加到常規的文本框:如何覆蓋AjaxToolkit CalendarExtender日期到字符串轉換?

<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 .NET1////10////2012

enter image description here

在這種情況下,我需要某種OnFormatDate事件,我可以提供一個日期的正確定位爲一個字符串。這導致我的問題:

如何重寫AjaxToolkit CalendarExtender日期到字符串轉換?


注:不要混淆的例子的問題。

  • 我要問如何定製一個日期轉換爲字符串在CalendarExtender
  • 即使我不處理在.NET中的一個錯誤,它不會改變我的問題
  • 即使我不處理一個CalendarExtender,我還在問這個問題

回答

0

我使用了本機Win32應用程序中使用的相同解決方案。

CalendarExtender使用「短日期」格式(d)。解決方法是要解決的bug在.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); 

注意:任何代碼被髮布到公共領域。無需歸屬。

1

在你的網頁......在它的頂部...你有這樣的:

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

添加有什麼樣(西班牙語爲例)...

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

,並在您的ScriptManager

EnableScriptLocalization="true" EnableScriptGlobalization="true" 

將幾乎覆蓋了當地的設置...

但我猜您只希望將此屬性設置在您的CalendarExtender中:

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

+0

該頁面已經在使用客戶端瀏覽器的語言環境(因此不需要爲de-DE用戶硬編碼es-MX)。 –