title: Gridview: Validate that SelectedIndex > -1
body: <p>I currently have a <code>Gridview</code>, And I want to use client-side validation to ensure that a row has been selected (ie: <code>SelectedIndex &gt; -1</code>).  </p>
  
  <p>At the moment I'm using <code>&lt;asp:CustomValidator&gt;</code> but want to move away from server-side validation. Here is what I'm currently doing:  </p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField HeaderText="MSN" DataField="MSN" /&gt;
          &lt;asp:CommandField ShowSelectButton="True" /&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  &lt;asp:CustomValidator ID="cvSelected" runat="server" ErrorMessage="Please select!" /&gt;
  </code></pre>
  
  <p>And then in code behind(<code>.cs</code>):  </p>
  
  <pre><code>Private Sub cvSelected_ServerValidate(ByVal source As Object, ByVal args As _
  System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvSelected.ServerValidate
      args.IsValid = (GridView1.SelectedIndex &gt; -1)
  End Sub
  </code></pre>