title: How does Page.IsValid work?
body: <p>I have following code with a <code>RequiredFieldValidator</code>. The <code>EnableClientScript</code> property is set as "false" in the validation control. Also I have disabled script in browser.</p>
  
  <p>I am NOT using <code>Page.IsValid</code> in the code behind. Still, when I submit without any value in textbox I will get <code>error message</code>. </p>
  
  <p>From comments of @Dai, I came to know that this can be an issue, if there is any code in <code>Page_Load</code> that is executed in a <code>postback</code>. There will be no validation errors thrown. </p>
  
  <p>(However, for button click handler, there is no need to check <code>Page.IsValid</code>)</p>
  
  <pre><code>if (Page.IsPostBack)
  {
      string value = txtEmpName.Text;
      txtEmpName.Text = value + "Appended";
  }
  </code></pre>
  
  <p><strong>QUESTION</strong></p>
  
  <ol>
  <li>Why does not the server side validation happen before <code>Page_Load</code>?</li>
  <li>Why does it work fine when I use <code>Page.IsValid</code>?</li>
  <li>Can you provide any reference to an article that explains this? (Not something that says - always use <code>Page.IsValid</code>; but something that says what are the mandatory scenarios to use <code>Page.IsValid</code></li>
  </ol>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Refer <a href="http://blogs.microsoft.co.il/blogs/linqed/archive/2009/09/01/asp-net-validators-common-misconception.aspx" rel="noreferrer">ASP.NET Validators Common Misconception</a></p>
  
  <blockquote>
    <p><code>Page.IsValid</code> is accessible only after running <code>Page.Validate()</code> method which is invoked implicitly somewhere after <code>Page_Load</code>. In case you keep all of your logic in a Page_Load event handler (which is highly discouraged!), call the <code>Page.Validate()</code> before checking the <code>Page.IsValid</code>.</p>
  </blockquote>
  
  <p><em>Note</em>: It is advised <em>not to keep</em> all the logic in <code>Page_Load</code>. If something is to happen on button click event, move it to button click event handler. If something is to happen on drop-down event, move it to drop-down selected item change event handler.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>It seems like, we need to add <code>If(Page.IsValid)</code> in <code>button click</code> also if we are using a <code>Custom Validator</code> with server side validation. Refer <a href="https://stackoverflow.com/questions/11064613/customvalidator-not-working-well">CustomValidator not working well</a>.</p>
  
  <p><strong>Note</strong>: Client side validation question is present here: <a href="https://stackoverflow.com/questions/13832818/whether-to-use-page-isvalid-or-page-clientvalidate-for-client-side-events">Whether to use Page_IsValid or Page_ClientValidate() (for Client Side Events)</a></p>
  
  <p><strong>MARKUP</strong></p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
      alert('haiii');
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      &lt;asp:ValidationSummary runat="server" ID="vsumAll" DisplayMode="BulletList" CssClass="validationsummary" ValidationGroup="ButtonClick" /&gt;
      &lt;asp:TextBox ID="txtEmpName" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;asp:RequiredFieldValidator ID="valEmpName" runat="server" ControlToValidate="txtEmpName"
          EnableClientScript="false" ErrorMessage="RequiredFieldValidator" Text="*" Display="Dynamic"
          ValidationGroup="ButtonClick"&gt;&lt;/asp:RequiredFieldValidator&gt;
      &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="ButtonClick" /&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>CODE BEHIND</strong></p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
      string value = txtEmpName.Text;
      SubmitEmployee(value);
  }
  </code></pre>
  
  <p><strong>References</strong>:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/1039465/should-i-always-call-page-isvalid">Should I always call Page.IsValid?</a></li>
  <li><a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=121" rel="noreferrer">ASP.NET Validation Controls – Important Points, Tips and Tricks</a></li>
  <li><a href="https://stackoverflow.com/questions/11064613/customvalidator-not-working-well">CustomValidator not working well</a></li>
  </ol>