社区那边的开发人员告诉我,他们需要每次为TextBox加上验证控件。最大的表单有49个控件,大部分时间用在加验证控件上,既无技术含量,又容易出错。可不可以出一种带Type的TextBox呢?
听起来很美好。就开始做吧。
经过和他们一段时间的探讨需求,确定ZTypeTextBox的Feature list如下:
1> 有Type属性,支持Double,Integer,Date,String
2> 有Required属性,以支持必填或允许空。
3>有MaximumValue,MinimumValue属性。
4>有RequiredErrorMessage何RangeErrorMessage属性,可以自定义出错信息。
真是想要什么就有什么。刚刚才研究了几个验证控件,就可以直接使用了。
分析一下需求,很明显,我们需要一个ClientRangeValidatorAdapter。反编译一下这个验证控件,原来和CompareValidator同样是继承自BaseCompareValidator,那么类的层次就要修改了。经过修改的两个类,和新加的一个类如下:(
现在是为公司工作,就要换上公司的命名空间啦!)
// ClientBaseCompareValidatorAdapter

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Web.UI;

namespace ZSoft.Platform.Web.UI.Controls

...{
public abstract class ClientBaseCompareValidatorAdapter : ClientBaseValidatorAdapter

...{
protected ClientBaseCompareValidatorAdapter(BaseCompareValidator validator, WebControl parentControl)
: base(validator, parentControl)

...{
}

private BaseCompareValidator GetValidator()

...{
return m_Validator as BaseCompareValidator;
}


public override void AddAttributesToRender()

...{
base.AddAttributesToRender();

if (GetValidator().Type != ValidationDataType.String)

...{
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"type", PropertyConverter.EnumToString(typeof(ValidationDataType), GetValidator().Type), false);
}

NumberFormatInfo currentInfo = NumberFormatInfo.CurrentInfo;
switch (GetValidator().Type)

...{
case ValidationDataType.Double:

...{
string numberDecimalSeparator = currentInfo.NumberDecimalSeparator;
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"decimalchar", numberDecimalSeparator);
return;
}
case ValidationDataType.Currency:

...{
string currencyDecimalSeparator = currentInfo.CurrencyDecimalSeparator;
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"decimalchar", currencyDecimalSeparator);
string currencyGroupSeparator = currentInfo.CurrencyGroupSeparator;
if (currencyGroupSeparator[0] == 'a0')

...{
currencyGroupSeparator = " ";
}
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"groupchar", currencyGroupSeparator);
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"digits", currentInfo.CurrencyDecimalDigits.ToString(NumberFormatInfo.InvariantInfo), false);
int currencyGroupSize = GetCurrencyGroupSize(currentInfo);
if (currencyGroupSize > 0)

...{
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"groupsize", currencyGroupSize.ToString(NumberFormatInfo.InvariantInfo), false);
}
break;
}
case ValidationDataType.Date:

...{
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"dateorder", GetDateElementOrder(), false);
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"cutoffyear", CutoffYear.ToString(NumberFormatInfo.InvariantInfo), false);
int year = DateTime.Today.Year;
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"century", (year - (year % 100)).ToString(NumberFormatInfo.InvariantInfo), false);
break;
}
}
}


private methods for support#region private methods for support

private static int CutoffYear

...{
get

...{
return DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;
}
}

private static int GetCurrencyGroupSize(NumberFormatInfo info)

...{
int[] currencyGroupSizes = info.CurrencyGroupSizes;
if ((currencyGroupSizes != null) && (currencyGroupSizes.Length == 1))

...{
return currencyGroupSizes[0];
}
return -1;
}

private static string GetDateElementOrder()

...{
string shortDatePattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
if (shortDatePattern.IndexOf('y') < shortDatePattern.IndexOf('M'))

...{
return "ymd";
}
if (shortDatePattern.IndexOf('M') < shortDatePattern.IndexOf('d'))

...{
return "mdy";
}
return "dmy";
}

#endregion
}
}

//
// 经过修改的CompareValidator类。
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Web.UI;

namespace ZSoft.Platform.Web.UI.Controls

...{
//
// 这是针对CompareValidator的具体实现。
//
public sealed class ClientCompareValidatorAdapter : ClientBaseCompareValidatorAdapter

...{
public ClientCompareValidatorAdapter(CompareValidator validator, WebControl parentControl)
: base( validator, parentControl )

...{
}

private CompareValidator GetValidator()

...{
return m_Validator as CompareValidator;
}

public override void AddAttributesToRender()

...{
base.AddAttributesToRender();

m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"evaluationfunction", "CompareValidatorEvaluateIsValid", false);

if (GetValidator().ControlToCompare.Length > 0)

...{
string controlRenderID = GetControlRenderID(GetValidator().ControlToCompare);
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"controltocompare", controlRenderID);
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"controlhookup", controlRenderID);
}

if (GetValidator().ValueToCompare.Length > 0)

...{
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"valuetocompare", GetValidator().ValueToCompare);
}
if (GetValidator().Operator != ValidationCompareOperator.Equal)

...{
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(GetValidator().ClientID,
"operator", PropertyConverter.EnumToString(typeof(ValidationCompareOperator), GetValidator().Operator), false);
}
}

}
}

//
// 新增加的ClientRangeValidatorAdapter类
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace ZSoft.Platform.Web.UI.Controls

...{
public sealed class ClientRangeValidatorAdapter : ClientBaseCompareValidatorAdapter

...{
public ClientRangeValidatorAdapter(RangeValidator validator, WebControl parentControl)
: base( validator, parentControl )

...{
}

private RangeValidator GetValidator()

...{
return m_Validator as RangeValidator;
}

public override void AddAttributesToRender()

...{
base.AddAttributesToRender();
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(m_Validator.ClientID,
"evaluationfunction", "RangeValidatorEvaluateIsValid", false);
m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(m_Validator.ClientID,
"maximumvalue", GetValidator().MaximumValue);

m_ParentControl.Page.ClientScript.RegisterExpandoAttribute(m_Validator.ClientID,
"minimumvalue", GetValidator().MinimumValue);

}
}
}

下一章,我们来实现ZTypeTextBox!
发表于 @ 2008年05月21日 19:06:53|评论(loading...)|编辑