5

Vấn đề tôi gặp phải, như tiêu đề nói, là tôi cần sử dụng định dạng tên tháng ngắn với năm dành cho Ngày được xác nhận bởi MaskedEditExtender từ AjaxControlToolkit. MMM-yyyy cho một DateTime không giống với MaskedEditExtender.Mask = "LLL-9999".Có thể đặt mặt nạ cho MaskedEditExtender cho định dạng ngày MMM-yyyy có thể hoạt động trong bất kỳ nền văn hóa nào không?

Nếu tôi sử dụng ngôn ngữ en-US, thứ hoạt động như nét duyên dáng, nhưng nếu tôi chuyển sang fr-FR thì nó sẽ không hoạt động vì biểu diễn ngắn trong tiếng Pháp sử dụng từ 4 đến 5 chữ cái (bao gồm dấu chấm).

Mọi ý tưởng về điều này? Cảm ơn.

Trả lời

0

Bạn có thể đặt mặt nạ động bằng cách sử dụng một cái gì đó như thế này?

string shortDateFormat = 
    System.Globalization.CultureInfo.DateTimeFormat.ShortDatePattern 
+0

Cố gắng mà trước đó, nhưng không, bởi vì định dạng nó sử dụng khác biệt so với các mẫu. –

1
 
Here you need to define the mask and mask type explicitly instead of declaring in the control itself. 

Follow the steps 

1. Create a method to get n set mask 


public string Mask 
     { 
      get 
      { 
       return GetPropertyValue("Mask", ""); 
      } 
      set 
      { 

       SetPropertyValue("Mask", value); 
      } 
     } 



2. Create a method to get n set mask type 

    public MaskedEditType MaskType 
     { 
      get 
      { 
       return GetPropertyValue("MaskType", MaskedEditType.None); 
      } 
      set 
      { 
       SetPropertyValue("MaskType", value); 

       AcceptAMPM = false; 
       AcceptNegative = MaskedEditShowSymbol.None; 
       DisplayMoney = MaskedEditShowSymbol.None; 
       InputDirection = MaskedEditInputDirection.LeftToRight; 
       break; 


      } 
     } 


3. Create a method to get the culture and culture date format 



public string CultureName 
     { 
      get 
      { 
       return GetPropertyValue("Culture", ""); 
      } 
      set 
      { 
       try 
       { 
        if (!String.IsNullOrEmpty(value)) 
        { 
         System.Globalization.CultureInfo CultControl = System.Globalization.CultureInfo.GetCultureInfo(value); 
         SetPropertyValue("Culture", CultControl.Name); 
         CultureDatePlaceholder = CultControl.DateTimeFormat.DateSeparator; 

         char sep = System.Char.Parse(CultControl.DateTimeFormat.DateSeparator); 
         string[] arrDate = CultControl.DateTimeFormat.ShortDatePattern.Split(sep); 
         string ret = arrDate[0].Substring(0, 1).ToUpper(CultControl); 
         ret += arrDate[1].Substring(0, 1).ToUpper(CultControl); 
         ret += arrDate[2].Substring(0, 1).ToUpper(CultControl); 
         CultureDateFormat = ret; 


        } 
        else 
        { 
         SetPropertyValue("Culture", ""); 
         CultureDatePlaceholder = ""; 

        } 
       } 
       catch 
       { 
        throw new ArgumentException("The Culture is invalid!"); 
       } 
      } 
     } 


4. Create a method to get the culture date format 



public string CultureDateFormat 
     { 
      get 
      { 
       return GetPropertyValue("CultureDateFormat", ""); 
      } 
      set 
      { 
       SetPropertyValue("CultureDateFormat", value); 
      } 
     } 


5. Now load the value on pre render 



protected override void OnPreRender(EventArgs e) 

     { 
      base.OnPreRender(e); 
      switch (MaskType) 
      { 
       case MaskedEditType.Date: 
        { 
         AcceptAMPM = false; 
         AcceptNegative = MaskedEditShowSymbol.None; 
         DisplayMoney = MaskedEditShowSymbol.None; 
         InputDirection = MaskedEditInputDirection.LeftToRight; 
         break; 
        } 

      } 
      System.Globalization.CultureInfo CultControl = System.Globalization.CultureInfo.CurrentCulture; 
      if (!String.IsNullOrEmpty(CultureName)) 
      { 
       CultControl = System.Globalization.CultureInfo.GetCultureInfo(CultureName); 
      } 
      CultureDatePlaceholder = CultControl.DateTimeFormat.DateSeparator; 

      char sep = System.Char.Parse(CultControl.DateTimeFormat.DateSeparator); 
      string[] arrDate = CultControl.DateTimeFormat.ShortDatePattern.Split(sep); 
      string ret = arrDate[0].Substring(0, 1).ToUpper(CultControl); 
      ret += arrDate[1].Substring(0, 1).ToUpper(CultControl); 
      ret += arrDate[2].Substring(0, 1).ToUpper(CultControl); 
      CultureDateFormat = ret; 

     } 


6. Last create a function to validate the user input 

    private bool validateMaskType() 
      { 
       string mask = Mask; 
       MaskedEditType maskType = MaskType; 
       if (!string.IsNullOrEmpty(mask) && (maskType == MaskedEditType.Date)) 
       { 
        string validMask = MaskedEditCommon.GetValidMask(mask); 
        switch (maskType) 
        { 
         case MaskedEditType.Date: 
          return Array.IndexOf(new string[] { "99/99/9999", "99/9999/99", "9999/99/99", "99/99/99" }, validMask) >= 0; 

          break; 
        } 
       } 
       return true; 
      }