我添加了一个正则表达式验证器,但它没有在页面上显示任何内容,基本上验证是在我需要启动的其他地方完成的。这是验证器
<div>
<asp:RequiredFieldValidator
ID="RegularNoCardAccepted" runat="server"
ControlToValidate="txtCreditCardNumber"
CssClass="Error" Display="Dynamic">
</asp:RequiredFieldValidator>
</div>
以下是我试图启动的方式,实际上我不需要它来检查正则表达式,我只是不确定如何在遇到这种情况时弹出它
if (CardNotAccepted())
{
//Find the validator located somewhere in the master page.
RequiredFieldValidator reqVal =
FindControlRecursive(this.Page.Master, "RegularNoCardAccepted")
as RequiredFieldValidator;
if (reqVal != null)
{
//The code goes through here but it never shows.
reqVal.Enabled = true;
reqVal.Text = "Credit Card Type is not accepted";
reqVal.Visible = true;
reqVal.Validate();
}
return;
}
答案 0 :(得分:1)
ASP.NET FieldValidators在POST事件上自动工作(假设Enabled属性设置为true)。以下是使用示例:http://www.w3schools.com/aspnet/showasp.asp?filename=demo_reqfieldvalidator