0

我正在尝试使用 RequiredFieldValidator 验证文本框,并通过后面的代码在 ValidationSummary 中显示错误消息,但我无法做到这一点。我的代码在这里

        if (txtUsrName.Text.Length == 0 || txtUsrAge.Text.Length == 0)
        {
            RequiredFieldValidator req = new RequiredFieldValidator();
            req.ID = "Required";
            req.ControlToValidate = txtUsrName.ID;                                
            req.IsValid = false;
            req.Visible = true;
            req.Enabled = true;                               
            req.ValidationGroup = "ValidationGroup";
            req.ErrorMessage = "Thease are required fields";
            req.InitialValue = "";
            req.Text = "*";

            ValidationSummary valsum = new ValidationSummary();
            ValidationSummaryDisplayMode mode = new ValidationSummaryDisplayMode();
            valsum.ID = "validatesummury";                
            valsum.HeaderText = "please correct the following errors";                
            valsum.DisplayMode = mode;
            valsum.ShowSummary = true;
            valsum.ValidationGroup = "ValidationGroup";
            valsum.Visible = true;                
        }

请在这件事上给予我帮助

4

2 回答 2

2

您可以使用Page.Validate() OR Page.Validate("YOUR_VALIDATION_GROUP")然后检查Page.IsValid是否所有验证器都有效,并且您也可以使用特殊验证器YOUR_RequiredFieldValidator.IsValid 'YOUR_RequiredFieldValidator' is one attached to your textbox control

于 2014-02-06T08:31:49.667 回答
0

这是一个类似的问题,可能会为您提供见解:使用数据注释在 Webforms 中进行不显眼的验证。数据注释提供了检查类属性的长度、最小值/最大值等的方法。

于 2014-02-06T08:32:50.447 回答