title: How to validate gridview when Clicking on Save button?
body: <p>I have a gridview in a form that contains a Save ImageButton. I would like to create a Client-side CustomValidator that checks whether the grid is empty or not. If it is empty then I would like to throw an error message to the user.</p>
  
  <p>This is my code. In the "Save_btn_Click" event, I check if the page is Valid:</p>
  
  <pre><code> &lt;asp:GridView ID="MyGridView" runat="server" 
                        AutoGenerateColumns="False" 
                        OnRowCancelingEdit="gridView_RowCancelingEdit"
                        OnRowCommand="gridView_RowCommand" 
                        OnRowDataBound="gridView_RowDataBound" 
                        OnRowEditing="gridView_RowEditing"
                        OnRowUpdating="gridView_RowUpdating" 
   &gt;....&lt;/GridView&gt;
  
  &lt;asp:CustomValidator id="cvFabricCollection" runat="server"                                                 
  ErrorMessage="Please enter at least one row"
  ControlToValidate="gridView"
  ValidationGroup="MyGroup"
  ClientValidationFunction ="ValidateGrid"&gt;
  &lt;/asp:CustomValidator&gt;
  
  &lt;asp:ImageButton ID="Save_btn" 
  ImageUrl="images/save.gif"
  runat="server"
  CausesValidation="True" 
  ValidationGroup="MyGroup"
  OnClick="Save_btn_Click"/&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>function ValidateGrid(sender, args)
  {
      var rowscount = document.getElementByID(&lt;%=MyGridView.ClientID%&gt;).rows.length;
      alert(rowscount);
      if(rowscount &lt;= 1)
      {
          args.IsValid = false;
          return;
      }
      args.IsValid = true;
  }                 
  </code></pre>
  
  <p>Any ideas on what I'm doing wrong?</p>
  
  <p>Thanks!</p>