Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether to use Page_IsValid or Page_ClientValidate() (for Client Side Events)

Tags:

asp.net

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
like image 371
LCJ Avatar asked Dec 12 '12 04:12

LCJ


People also ask

Why is it so important that you check the value of the IsValid property of the page when processing data?

Note: If we don't check for Page. IsVaid property then hackers can disable JavaScript from the browser and bypass our validation and submit the form which we don't want to. So it is recommended to check Page. IsValid before submitting to server.

How do you check if a website is valid in JavaScript?

Page_ClientValidate() will work. It returns true if the page was valid and it works fine. If you are using ASP.NET 2.0, pass the validation group name as a parameter.

What is use of page IsValid method?

The Validate method validates the specified validation group. After calling the Validate method on a validation group, the IsValid method will return true only if both the specified validation group and the validation group of the control that caused the page to be posted to the server are valid.

What is Page_ClientValidate?

Page_ClientValidate is an ASP.Net JavaScript function in-built which is used to perform ASP.Net validation explicitly i.e. invoking the ASP.Net Validators using JavaScript.


1 Answers

  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.

like image 70
Peter Ivan Avatar answered Oct 06 '22 10:10

Peter Ivan