2014-02-06 53 views
0

我想驗證一個使用RequiredFieldValidator的文本框,並通過後面的代碼在ValidationSummary中顯示錯誤消息,但我無法做到這一點。 我的代碼放在這裏通過後面的代碼使用asp.net驗證控件

 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;     
     } 

請幫我這個

+0

您需要重新創建pre-init上的元素。 –

回答

2

可以使用Page.Validate() OR Page.Validate("YOUR_VALIDATION_GROUP")然後檢查Page.IsValid,檢查所有驗證是有效的,並以特殊的驗證也可以使用YOUR_RequiredFieldValidator.IsValid'YOUR_RequiredFieldValidator' is one attached to your textbox control

相關問題