Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

asp.net - Whether to use Page_IsValid or Page_ClientValidate() (for Client Side Events)

I have following code inside a button click handler. Both approaches work fine. The Page_ClientValidate() causes an extra validation check and do processing whereas Page_IsValid makes use of existing property.

QUESTIONS

  1. In case of button click, is it always guaranteed that Page_IsValid would have been calculated by that time? If is not guaranteed, we need to call Page_ClientValidate() explicitly.
  2. What are the events that happen before Page_IsValid is set? For such events we should not rely on Page_IsValid

UPDATE

Is it assured that the button click event handler (in JavaScript) will be called only after the validation part is completed (i.e., after Page_ClientValidate() was invoked as part of validation) ? If this is assured, can I rely on Page_IsValid?

SCRIPT

  $('#btnSave').click(function (e) {

  //Aproach 1
  var isValid = Page_ClientValidate('');
  if (isValid) 
  {
       //Do reamining work
  }

  //Aproach 2
  if (Page_IsValid)
  {
      //Do reamining work
  }

  });

REFERENCES:

  1. Hide redundant error message in ASP.Net ValidationSummary
  2. Validator causes improper behavior for double click check
  3. Page_ClientValidate is not defined
  4. Page_ClientValidate is validating multiple times.
  5. MSDN - ASP.NET Validation in Depth
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. In case of button click, Page_ClientValidate() is called when (and only when) the button's CausesValidation is set to true.

  2. Page_ClientValidate() is part of process of doing postback, so it is called within button's click.
    I rely on Page_IsValid only in a scope of a function after calling Page_ClientValidate(). Otherwise I always call Page_ClientValidate().

Comment: calling Page_ClientValidate() repeatedly may cause the page to be too obtrusive (multiple alerts etc.). That's why it's good to have a custom validate function that takes care of all validation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...