0

I'm a beginner in ASP.NET. just want to ask when we check if any validation fails, we do:

if (Page.IsValid)
{
   ...
}

can we do:

if (IsValid)
{
...
}

are they the same?

  • Have you checked out these SO posts [1](https://stackoverflow.com/a/9107227/718373) and [2](https://stackoverflow.com/a/13763706/718373) on the same? – fujiFX Feb 25 '19 at 22:36
  • 2
    Possible duplicate of [What is relationship between Page.IsValid and args.IsValid](https://stackoverflow.com/questions/9106836/what-is-relationship-between-page-isvalid-and-args-isvalid) – fujiFX Feb 25 '19 at 22:38

1 Answers1

1

Depends on the context, i.e. where the code is.

Whenever you call a property without specifying the object it belongs to, this is implied. So for example, this:

if (IsValid)

...is the same as this:

if (this.IsValid)

If the code exists within a page, then you this.IsValid is calling the IsValid property of the current page object. Which would make it the same.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • I do like `public partial class myForm : System.Web.UI.Page`, so they are the same, isn't it? –  Feb 25 '19 at 22:46
  • Yes. It inherits from `Page`, so `myForm` is a subclass of or is a type of `Page`, having all of its methods and properties. – John Wu Feb 25 '19 at 22:49
  • so when we have to use `Page.IsValid`, could you give me a code example? thanks –  Feb 25 '19 at 22:57
  • 1
    A code example wouldn't tell you much because it's all about the context. You'd have to use it whenever `this` and `Page` are different, e.g. any code that is not running within the page. – John Wu Feb 25 '19 at 22:59
  • Thanks for your answer, could you also have a look at this question https://stackoverflow.com/questions/54860715/how-servervalidateeventargs-works-in-asp-net –  Feb 25 '19 at 23:23