title: advancing to button code-behind when validation fails... how to solve it without Page.IsValid?
body: <p>I have an ASP .NET page with both asp .net validators and some javascript checking too.
  I am advancing into the button code behind:</p>
  
  <pre><code>protected void Button2_Click(object sender, EventArgs e)
      {
          if (Page.IsValid)
          {
              /// ...
  </code></pre>
  
  <p>Even when the validation fails!
  The Page.IsValid check solves it, but it also resets all the JavaScript variables… May there be a way not to advance into the button code?
  I have an ajax update panel and some image buttons on the page too… Anything I may look out for?
  Thanks in advance!</p>
  
  <p>Here is my aspx:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" 
  CodeBehind="WebForm2.aspx.cs" 
  Inherits="WebForm2" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div style="width: 500px;"&gt;
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;
          &lt;/asp:ScriptManager&gt;
  
          &lt;script type="text/javascript"&gt;
              var nrOptions = 0;
              alert(nrOptions + " - variable initialized");
              function IncrementQuantity() {
                  nrOptions++;
                  alert(nrOptions);
              }
              function DecrementQuantity() {
                  nrOptions--;
                  alert(nrOptions);
              }
              function MoreThanTwo() {
                  alert(nrOptions);
                  if (nrOptions &gt; 1) {
                      alert('okay - you have: ' + nrOptions);
                      return true;
                  }
                  else {
                      alert('error - must have at least two options, you only have: ' + nrOptions);
                      return false;
                  }
              }            
          &lt;/script&gt;
  
          &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
              &lt;ContentTemplate&gt;
                  &lt;asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"&gt;
                      &lt;HeaderTemplate&gt;
                          Fill in with two or more options:&lt;br /&gt;
                          &lt;table border="1" width="100%"&gt;
                      &lt;/HeaderTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;tr&gt;
                              &lt;td valign="middle"&gt;
                              &lt;/td&gt;
                              &lt;td valign="middle"&gt;
                                  Description:
                                  &lt;asp:TextBox ID="TBox1" runat="server" Width="120px" Text='&lt;%# Eval("Desc")%&gt;'&gt;&lt;/asp:TextBox&gt;
                                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TBox1"
                                      ValidationGroup="InsertVal" ErrorMessage="*"&gt;&lt;/asp:RequiredFieldValidator&gt;
                                  Points:
                                  &lt;asp:TextBox ID="TBox2" runat="server" Width="20px" Text='&lt;%# Eval("Pont")%&gt;'&gt;&lt;/asp:TextBox&gt;
                                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TBox2"
                                      ValidationGroup="InsertVal" ErrorMessage="*"&gt;&lt;/asp:RequiredFieldValidator&gt;
                                  &lt;asp:Button ID="ImageButton1" runat="server" Text="x" CommandName="DeleteRow" OnClientClick="DecrementQuantity();" /&gt;
                                  &lt;asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TBox2"
                                      ValidationExpression="\d+" ValidationGroup="InsertVal" runat="server"
                                      ErrorMessage="Number &gt;= 0."&gt;&lt;/asp:RegularExpressionValidator&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;tr&gt;
                              &lt;td colspan="2" align="right"&gt;
                                  &lt;asp:Button ID="lnkAddRow" runat="server" Text="Add option" OnClientClick="IncrementQuantity();"
                                      CommandName="AddRow" OnClick="lnkAddRow_Click" /&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;/table&gt;
                      &lt;/FooterTemplate&gt;
                  &lt;/asp:Repeater&gt;
                  &lt;br /&gt;
                  &lt;p style="text-align: right;"&gt;
                      &lt;asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" OnClientClick="return MoreThanTwo();"
                          ValidationGroup="InsertVal" /&gt;
                  &lt;/p&gt;
              &lt;/ContentTemplate&gt;
          &lt;/asp:UpdatePanel&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my code-behind:</p>
  
  <p>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;</p>
  
  <pre><code>public partial class WebForm2 : System.Web.UI.Page
  {        
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!this.IsPostBack)
          {
              DTable = EmptyDTOptions();
              Repeater1.DataSource = DTable;
              Repeater1.DataBind();
          }
      }
  
      protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
      {
  
          if (e.CommandName == "AddRow")
          {
              DTable.Rows.Add(0, "", "");
              Repeater1.DataSource = DTable;
              Repeater1.DataBind();
              return;
          }
  
          if (e.CommandName == "DeleteRow")
          {
  
              int idx = e.Item.ItemIndex;
              DTable.Rows.RemoveAt(idx);
              Repeater1.DataSource = DTable;
              Repeater1.DataBind();
              return;
          }
  
      }
  
      protected void lnkAddRow_Click(object sender, EventArgs e)
      {
          foreach (RepeaterItem item in Repeater1.Items)
          {
              int idx = item.ItemIndex;
  
              TextBox tb1 = (TextBox)item.FindControl("TBox1");
              TextBox tb2 = (TextBox)item.FindControl("TBox2");
  
              DTable.Rows[idx]["Desc"] = tb1.Text;
              DTable.Rows[idx]["Pont"] = tb2.Text;
          }
      }
  
      protected void Button2_Click(object sender, EventArgs e)
      {
          if (Page.IsValid)
          {                
              // save!             
          }
      }
  
      private DataTable DTable
      {
          get
          {
              DataTable _dt = (DataTable)Session["DTable"];
              if (Object.Equals(_dt, null))
              {
                  _dt = EmptyDTOptions();
                  Session.Add("DTable", _dt);
              }
              return _dt;
          }
          set
          {
              Session["DTable"] = value;
          }
      }
  
      DataTable EmptyDTOptions()
      {
          DataTable dt = new DataTable();
          DataColumn dc;
  
          dc = new DataColumn("OptionNr", System.Type.GetType("System.Int32"));
          dt.Columns.Add(dc);
          dc = new DataColumn("Desc");
          dt.Columns.Add(dc);
          dc = new DataColumn("Pont");
          dt.Columns.Add(dc);
  
          return dt;
      }
  }
  </code></pre>
  
  <p>I think it shows what I'm trying to avoid... Advancing to the button2_click with the failed validation (and resetting the javascript variable)... In order to get a list of two or more pairs of items, one of them being a number.</p>