【发布时间】:2013-09-19 08:33:21
【问题描述】:
在 Page.Load 中,我动态添加了一个标签、我应该验证的文本框和自定义验证器
Label myLabel3 = new Label();
myLabel3.ID = "lblEGN" + i.ToString();
myLabel3.Text = "EГН";
TextBox myTextBox3 = new TextBox();
myTextBox3.ID = "txtEGN" + i.ToString();
pnlPersonalCard.Controls.Add(myLabel3);
pnlPersonalCard.Controls.Add(new LiteralControl("<br />"));
CustomValidator cvEGN = new CustomValidator();
cvEGN.ID = "cvtxtEGN" + i.ToString();
cvEGN.ControlToValidate = "txtEGN" + i.ToString();
// cvEGN.ClientValidationFunction = "checkEgn";
cvEGN.ServerValidate += serverCheckEgn;
cvEGN.ErrorMessage = "Невалидно егн";
pnlPersonalCard.Controls.Add(cvEGN);
pnlPersonalCard.Controls.Add(myTextBox3);
pnlPersonalCard.Controls.Add(new LiteralControl("<br />"));
当然我提供了应该执行的自定义验证功能
protected void serverCheckEgn(object sender, ServerValidateEventArgs args) {
string egn = args.Value;
if (egn.Length != 10)
args.IsValid = false;
int year = Int32.Parse(egn.Substring(0, 2));
int month = Int32.Parse(egn.Substring(2, 4));
int day = Int32.Parse(egn.Substring(4, 6));
if (month >= 40) {
year += 2000;
month -= 40;
} else if (month >= 20) {
year += 1800;
month -= 20;
} else {
year += 1900;
}
string date = year + "/" + month + "/" + day;
if (!CheckDate(date))
args.IsValid=false;
int checkSum = 0;
int[] weights = new int[9] {2,4,8,5,10,9,7,3,6};
for (var ii = 0; ii < weights.Length; ++ii) {
checkSum += weights[ii] * Int32.Parse(egn.Substring(ii,1));
}
checkSum %= 11;
checkSum %= 10;
if (checkSum != Int32.Parse(egn.Substring(9,1)))
args.IsValid=false;
args.IsValid = true;
}
但是当我按下按钮时,所有其他验证器(我有两个其他必填字段验证器和两个其他正则表达式验证器)正在工作,只有这个自定义验证器似乎我提供的功能没有执行!
【问题讨论】:
-
你能说输入日期的例子吗
-
示例 - 555;该字段非常重要,数据应该非常准确 - 这是保加利亚每个人最重要的个人号码
-
控件何时动态添加到您的页面?动态添加的控件如果没有在页面的 Init 上添加,则不会触发它们的事件。
-
如何获取'555'的Substring(4, 6)
-
是的 Samiey - 我写道我发现了我的错误。只是看看。现在我又遇到了一个问题
标签: asp.net validation