2016-03-23 21 views
0

我已經創建了服務器創建的表單,使用Telerik AJAX控件和提交按鈕來完成發佈(無點擊事件)。我的表單使用的驗證器工作正常,除了發生字段驗證錯誤後的下一個帖子。ASP.NET驗證器後面的代碼生成形式

如果我使電子郵件字段爲空,代碼邏輯捕獲並返回錯誤。再次提交,需要電子郵件字段驗證程序才能在代碼之前捕獲錯誤。現在把有效的數據,並提交和最後一個空字段驗證錯誤再次出現。第二次提交好的數據並更新。

此外,如果我產生我自己的驗證錯誤,那麼驗證工作正常。我有必要的複選框的驗證碼。如果缺少檢查發生了,我叫

ValidatorError.Display(Page, message); 

這將導致後續

Page.Validate(FormEntryBase.VALIDATION_GROUP); 

正確驗證當前提交必填字段。

 protected void Page_Load(object sender, EventArgs e) { 


      localValidationError = theForm.CustomValidateForm(Request.Form); 
      if (localValidationError) { 
       validateErrorList = form.validateErrorList; 

       // get local valiation error put into validation summary error list 
       if (validateErrorList != null) { 
        foreach (string message in validateErrorList) { 
         ValidatorError.Display(Page, message); 
        } 
       } 
      } 
      Page.Validate(FormEntryBase.VALIDATION_GROUP); 

      if (!Page.IsValid) { 
       return; 
      } 

驗證錯誤代碼不趕不掛在初始提交可以讓壞數據的採集。

我有一個使用的點擊方法的另一個Web表單和僅使用Page.IsValid工作正常。

很奇怪的行爲,可能是某種生命週期問題。

+0

這是一篇文章,而不是Ajax調用。 – George

回答

0

有一些運氣,我想通過在代碼中添加一個點擊事件來解決問題。要添加點擊事件,首先我將RadButton添加到我的表單創建代碼中,並在aspx.cs中進行訪問。

RadButton button = new RadButton(); 
button.ValidateRequestMode = ValidateRequestMode.Enabled; 
button.ValidationGroup = VALIDATION_GROUP; 
button.UseSubmitBehavior = true; 
theForm.updateButton = button; 

在aspx.cs Page_Load中,我將click方法添加到按鈕。

protected void Page_Load(object sender, EventArgs e) { 
    ... 
    theForm = group.GetJoinForm(); 
    theForm.updateButton.Click += new EventHandler(UpdateButton_Click); 
    ... 

void UpdateButton_Click(object sender, EventArgs e) { 
    Page.Validate(FormEntryBase.VALIDATION_GROUP); 
    if (!Page.IsValid) { 
    return; 
    } 
    ... most of page load code moved here 

這三個步驟解決了我的驗證延遲問題,現在我所有的表單都很好地驗證了。