asp.net – 是否可以为MaskedEditExtender设置掩码,以便在任何文
发布时间:2020-12-16 07:44:44 所属栏目:asp.Net 来源:网络整理
导读:正如标题所说,我遇到的问题是我需要使用短月份名称格式和年份,日期由AjaxControlToolkit中的MaskedEditExtender验证. DateTime的MMM-yyyy与MaskedEditExtender.Mask =“LLL-9999”不同. 如果我使用en-US语言环境,那就像魅力一样,但是如果我切换到fr-FR那么它
正如标题所说,我遇到的问题是我需要使用短月份名称格式和年份,日期由AjaxControlToolkit中的MaskedEditExtender验证. DateTime的MMM-yyyy与MaskedEditExtender.Mask =“LLL-9999”不同.
如果我使用en-US语言环境,那就像魅力一样,但是如果我切换到fr-FR那么它将无法工作,因为法语中的短月表示使用4到5个字母(包括点). 有什么想法吗? 解决方法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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 重定向到另一个动作发布动作
- asp.net – 在web.config中创建一个字符串并在web.api中使用
- asp.net-mvc – 如何在asp.net mvc中检查模型验证错误?
- Asp.Net MVC3到MVC4升级工具?
- asp.net – 为什么几次调用HttpApplication构造函数
- asp.net-mvc – 如何在使用类型化视图时在ActionFilterAttr
- asp.net – “创建应用服务”对话不检索信息,禁用创建按钮
- asp.net – 获取当前请求的凭据以便在WebRequest中使用
- asp.net-mvc-2 – asp.net-mvc2 – 不使用Model的强类型助手
- asp.net – 我需要html编码标题属性(工具提示)吗?