2010-05-28 109 views
0

我有一个ASP.NET Web应用程序(使用C#)。其中我有一个有几个字段和验证器的表单。为了验证之一,我只需要在验证:有条件验证

  • 一定的文本框为空
  • 和一定的记录不存在于数据库中(这已经处理)。

我知道我无法在page_load上启用/禁用验证器,因为可能会在该文本框中输入内容。我也试过在提交按钮的onclick事件以下,但它似乎没有工作:

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

我也试过Page.Validate()但没有任何工作...

谁能请帮帮我?

谢谢

回答

1

使用customvalidator。在验证事件中,您具有类似以下伪代码的代码:

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

这件事情应该由客户端上的JavaScript来完成。然后提交你应该在服务器端进行验证。