2017年5月25日 星期四

【ASP.NET Webform】CustomValidator in DataBoundControl ServerValidate fire twice?

客製化驗證放在像 GridView, FormView 這類控制項跑 insert 或 update 時會引發兩次後端驗證的問題。

還真的是 bug 耶! 好久沒碰到原廠 bug 了!
參考 ASP.NET Forums

懶得進去看的人可以抄這份,我從裡面類似的解改過來的
  1. private bool? _lastValid = null;
  2. protected void CustomValidator_TypeA_ServerValidate(object source, ServerValidateEventArgs args)
  3. {
  4. if (_lastValid != null) // prevent second validate
  5. {
  6. args.IsValid = _lastValid.Value;
  7. return;
  8. }
  9.  
  10. if(something) // your validation logic
  11. {
  12. _lastValid = true;
  13. }
  14. else
  15. {
  16. _lastValid = false;
  17. }
  18.  
  19. args.IsValid = _lastValid.Value;
  20. }

沒有留言:

張貼留言