简体   繁体   English

条件验证

[英]Conditional validation

I have an ASP.NET web app (with C#). 我有一个ASP.NET Web应用程序(带有C#)。 In it I have a form with several fields and validators. 在其中,我有一个包含多个字段和验证器的表单。 For one of the validators I only need to validate when: 对于验证者之一,我只需要在以下情况下验证:

  • a certain textbox is empty 某个文本框为空
  • and a certain record does not exist in the database (this is already handled). 并且数据库中不存在某个记录(已处理)。

I know I can't enable/disable the validator on page_load because something might be entered into that textbox. 我知道我无法在page_load上启用/禁用验证器,因为可能在该文本框中输入了一些内容。 I also tried the following in the onclick event of the submit button but it didn't seem to work: 我还在提交按钮的onclick事件中尝试了以下操作,但似乎不起作用:

                    Validator1.Enabled = true;
                    Validator1.Validate();

I also tried Page.Validate() but that didn't work either... 我也尝试了Page.Validate()但是那也不起作用...

Can anyone please help? 谁能帮忙吗?

Thanks 谢谢

Use a customvalidator. 使用customvalidator。 Inside the Validation Event you have code like this pseudo code: 在Validation Event中,您具有类似于此伪代码的代码:

OnValidating(object sender, ServerValidateEventArgs e)
{
 if(CertainTextBox.Text.IsNullOrEmpty() && CertainRecordDoesNotExistInDB))
{
 // validate
// and set e.Valid to the desired validation output
}
else
{
 e.IsValid = false;
}
}

this things should be done by JavaScript on client. 这件事应该由客户端上的JavaScript完成。 Then on submit you should validate at server side. 然后在提交时,您应该在服务器端进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM