Saturday, 6 February 2016

Page.IsValid and Page.Validate in Asp.net

After validating we may thing that we have built a secure application but a hacker could disable JavaScript and bypass all our validators ! This is where the Page.Validate() method and more importantly, the Page.IsValid property come in.
Page.IsValid is a property to check whether page validation succeeded or not

The Page.Validate() method is fired automatically by controls that have the CausesValidation property set to true. Note that the Button control’s CausesValidation property is true by default.

We should check this property only after calling the Page. Validate () method, or set the CausesValidation property to true which is by default true for button control.

Example :

protected void btnLogin_Click(object sender, EventArgs e)
{
    Page.Validate(); //optional here because it is required only if buttons's  CausesValidation property is set to false but it is true by default

    if (!Page.IsValid)
    {
        return;
    }     
    //write your login code here
}

No comments:

Post a Comment