views:

44

answers:

1

I know that calling Page.IsValid in code-behind of an ASP.NET page throws an exception if I didn't explicitly call Page.Validate before or if validation didn't happen automatically in an event handler of a control with enabled "CausesValidation".

Sometimes I am in a method or an event of a page where I need to know if all input was valid but I don't know at this particular place where the postback came from. So I don't know if the page was validated before or not, that means, if I can ask Page.IsValid without calling Page.Validate before or not.

Is there a way to check if a page was already validated (something like a boolean property "Page.HasBeenValidated" or whatever)?

+3  A: 

Try to assign a private variable inside a try catch

 private bool isPageValid;
 public bool IsPageValid
 {
    get
    {
      try
      {
        isPageValid= Page.IsValid
      }
      catch
      {  
        Page.Validate();
        isPageValid = Page.IsValid
      }
      return isPageValid;
    }
 }
madatanic
Thanks, that's fine! I was hoping for something built-in in the page class, but well, it doesn't seem to exist.
Slauma
You don't need the private variable, you can simply do `try { return Page.IsValid; } catch { Page.Validate(); return Page.IsValid; }`.
Steven