【问题标题】:servervalidate function of my custom validator doesn't fire .net我的自定义验证器的 servervalidate 函数不会触发 .net
【发布时间】: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


【解决方案1】:

在向数据库插入记录期间检查 IsValid:

if (page.IsValid) 
{
    //insert record
}
else
{
    Response.Write("Input string is incorrect!");
}

【讨论】:

    【解决方案2】:

    当您尝试验证时,文本框的内容是否为空?要验证何时为空,您需要将ValidateEmptyText 属性设置为true

    【讨论】:

    • 不!文本框内容不为空。
    【解决方案3】:

    伙计们,您无法相信导致问题的原因 当我在文本框中写 - 5555

    当程序到达这一行时

     int day = Int32.Parse(egn.Substring(4, 6));
    

    我当然会得到一个例外,即我要减去的字符超出了我的字符串范围。

    问题是另一个问题。即使在我的 button_click 函数中,我也得到了这一行

    if (page.IsValid) 
    

    在插入数据库之前检查一切是否正常;我不知道为什么,但是通过 args.IsValid=false 将记录插入到数据库事件中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多