This documentation is archived and is not being maintained.

GridView.OnSelectedIndexChanged Method

Raises the SelectedIndexChanged event.

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

protected virtual void OnSelectedIndexChanged(
	EventArgs e


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 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="C#" %>

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

<script runat="server">

  protected void ContactsGridView_SelectedIndexChanged(object sender, EventArgs e)
    if (ContactsGridView.SelectedIndex >= 0)
      ViewState["SelectedKey"] = ContactsGridView.SelectedValue;
      ViewState["SelectedKey"] = null;

  protected void ContactsGridView_DataBound(object sender, EventArgs e)
    for (int i = 0; i < ContactsGridView.Rows.Count; i++)
      // Ignore values that cannot be cast as integer.
        if ((int)ContactsGridView.DataKeys[i].Value == (int)ViewState["SelectedKey"])
          ContactsGridView.SelectedIndex = i;
      catch { }

  protected void ContactsGridView_Sorting(object sender, GridViewSortEventArgs e)
    ContactsGridView.SelectedIndex = -1;

  protected void ContactsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    ContactsGridView.SelectedIndex = -1;

<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

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.