GridView.OnSelectedIndexChanged Method (EventArgs)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Raises the SelectedIndexChanged event.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Protected Overridable Sub OnSelectedIndexChanged (
	e As EventArgs


Type: System.EventArgs

An System.EventArgs that contains event data.

The SelectedIndexChanged event is raised when a row's Select button is clicked, but after the GridView control handles the select operation. This enables you to provide an event-handling method that performs a custom routine, such as updating a status label with the currently selected row, whenever this event occurs.

Raising an event invokes the event handler through a delegate. For more information, see NIB: Raising an Event.

The OnSelectedIndexChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:

When overriding OnSelectedIndexChanged in a derived class, be sure to call the base class's OnSelectedIndexChanged method so that registered delegates receive the event.

The following example shows how create an event handler for the SelectedIndexChanged event. In this example, the selected row is persisted in the view state. So even after a sorting or a paging operation, only that row will be selected.

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<script runat="server">

  Protected Sub ContactsGridView_SelectedIndexChanged()
    If ContactsGridView.SelectedIndex >= 0 Then
      ViewState("SelectedKey") = ContactsGridView.SelectedValue
      ViewState("SelectedKey") = Nothing
    End If
  End Sub

  Protected Sub ContactsGridView_DataBound()
    For i As Integer = 0 To ContactsGridView.Rows.Count - 1
      ' Ignore values that cannot be cast as integer.
        If Convert.ToInt32(ContactsGridView.DataKeys(i).Value) = Convert.ToInt32(ViewState("SelectedKey")) Then _
          ContactsGridView.SelectedIndex = i
      End Try
  End Sub

  Protected Sub ContactsGridView_Sorting()
    ContactsGridView.SelectedIndex = -1
  End Sub

  Protected Sub ContactsGridView_PageIndexChanging()
    ContactsGridView.SelectedIndex = -1
  End Sub

<html xmlns="" >
  <head id="Head1" runat="server">
    <title>GridView OnSelectedIndexChanged Example</title>
    <form id="form1" runat="server">

      <h3>GridView OnSelectedIndexChanged Example</h3>

      <asp:GridView ID="ContactsGridView"
        AllowPaging="True" AllowSorting="True" 
        <SelectedRowStyle BackColor="Aqua" />

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName] FROM Person.Contact">


.NET Framework
Available since 2.0
Return to top