2011-07-27 55 views
0

我試圖確定爲什麼我的驗證功能沒有在我的Web窗體上被調用。我已閱讀各種文章MSDN,最顯着的發現這塊金塊。在UpdatePanel中使用ClientValidationFunction(JavaScript)

當您使用UpdatePanel控件內CustomValidator控件, 確保驗證控件,並將其與 相關的控制都在同一個小組。有關將UpdatePanel 控件用於部分頁面更新的更多信息,請參閱Partial-Page Rendering概述。

有了這些知識我修改了代碼如下:

<asp:UpdatePanel ID="UpdatePanel0" runat="server"> 
    <ContentTemplate> 
     <script type="text/javascript"> 
      function IsNotChecked(obj, args) { 
       args.IsValid = false; 
       if (document.getElementByID("cbRegion0").checked) 
       { 
        args.IsValid = true; return true; 
       } 
       return false; 
      } 
     </script> 
     <asp:CheckBox ID="cbRegion0" runat="server" ValidationGroup="0" AutoPostBack="true" OnCheckedChanged="CheckBox_CheckedChanged" /> 
     <asp:CustomValidator ID="CustomValidator0" runat="server" ValidationGroup="0" ClientValidationFunction="IsNotChecked" ErrorMessage="You did not check the box." ValidateEmptyText="True" /> 
    </ContentTemplate> 
</asp:UpdatePanel> 

我遇到的問題是,當點擊提交我的網頁上按鈕時,驗證程序沒有得到執行。 設計的一些獨特元素是,上面的代碼實際上是通過Control.Add()添加到頁面的.ascx的內部,但我不明白這會如何影響ClientValidationFunction。我相信這與<script>在表單中的位置有關,但是儘管在MSDN處指示了方向,但似乎沒有什麼區別。 感謝您的任何想法!

回答

1

我在Stack Overflow中找到了答案討論How do I get Client-side validation of ASP.Net page to run?。對不起,沒有看到這一點。正如@Brian指出的,通過在我的代碼中指定ValidationGroup="0",預計頁面上的提交按鈕具有相同的ValidationGroup分配。最後,我只是從指令中刪除了屬性,現在它調用JS。
我找到了答案,因爲我正在瀏覽頁面源代碼,並注意到提交按鈕正在調用javascript WebForm_OnSubmit()方法,最終檢查的方法是Page_ValidationActive
這使我想到了我已經鏈接的問題。 我希望這可以幫助別人。
謝謝!

+0

+1爲後續! – Brian

1

異步回發後,UpdatePanel內部的腳本將會丟失。嘗試將您的IsNotChecked方法放在任何更新面板之外...或者,實施IScriptControl並將您的驗證方法放在您創建的客戶端腳本文件中以伴隨腳本控制...

+0

感謝您的回答。這看起來與MSDN的文章相反,但我仍然嘗試過。行爲沒有變化。 – McArthey

+0

@McArthey - 爲了清楚起見,我不建議您將驗證器放在更新面板之外。只是被調用的腳本。您的更新面板中的

  • 11. 使用JavaScript或jQuery手動更新UpdatePanel
  • 12. UpdatePanel和Javascript
  • 13. javascript衝突UpdatePanel
  • 14. ASP.NET - UpdatePanel和JavaScript
  • 15. javascript on updatepanel
  • 16. 在UpdatePanel/UpdateProgress中使用JavaScript加載指示器?
  • 17. 如何使用javascript在updatepanel中獲取回發元素?
  • 18. .Net UpdatePanel和JavaScript禁用
  • 19. UpdatePanel中的JavaScript代碼
  • 20. ASP.Net - AJAX中的Javascript UpdatePanel
  • 21. 使用的UpdatePanel
  • 22. 在UpdatePanel的JavaScript中強制回發?
  • 23. 在updatepanel中丟失javascript click事件
  • 24. UpdatePanel和Javascript問題
  • 25. ASP.NET UpdatePanel Javascript回調
  • 26. 如何在UpdatePanel中使用FileUpload?
  • 27. ASP .NET在UpdatePanel中使用Repeater和UpdateProgress
  • 28. webforms:在updatepanel中使用infragistics webdropdown
  • 29. 在SharePoint中使用UpdatePanel 2013 web部件
  • 30. 在ASP.NET UpdatePanel中使用jQuery佈局