GridView.OnSelectedIndexChanged(EventArgs) 方法

定义

引发 SelectedIndexChanged 事件。

protected:
 virtual void OnSelectedIndexChanged(EventArgs ^ e);
protected virtual void OnSelectedIndexChanged (EventArgs e);
abstract member OnSelectedIndexChanged : EventArgs -> unit
override this.OnSelectedIndexChanged : EventArgs -> unit
Protected Overridable Sub OnSelectedIndexChanged (e As EventArgs)

参数

e
EventArgs

包含事件数据的 EventArgs

示例

以下示例演示如何为 SelectedIndexChanged 事件创建事件处理程序。 在此示例中,所选行将保留在视图状态中。 因此,即使在排序或分页操作之后,也只会选择该行。

<%@ Page language="C#" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void ContactsGridView_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (ContactsGridView.SelectedIndex >= 0)
      ViewState["SelectedKey"] = ContactsGridView.SelectedValue;
    else
      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.
      try
      {
        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;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>GridView OnSelectedIndexChanged Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView OnSelectedIndexChanged Example</h3>
      
      <asp:GridView ID="ContactsGridView"
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"        
        AutoGenerateSelectButton="True"
        OnSelectedIndexChanged="ContactsGridView_SelectedIndexChanged"        
        AllowPaging="True" AllowSorting="True" 
        OnDataBound="ContactsGridView_DataBound" 
        OnSorting="ContactsGridView_Sorting" 
        OnPageIndexChanging="ContactsGridView_PageIndexChanging"
        runat="server">
        <SelectedRowStyle BackColor="Aqua" />
      </asp:GridView>
            
      <!-- 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">
      </asp:SqlDataSource>


    </form>
  </body>
</html>
<%@ Page language="VB" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub ContactsGridView_SelectedIndexChanged()
    If ContactsGridView.SelectedIndex >= 0 Then
      ViewState("SelectedKey") = ContactsGridView.SelectedValue
    Else
      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.
      Try
        If Convert.ToInt32(ContactsGridView.DataKeys(i).Value) = Convert.ToInt32(ViewState("SelectedKey")) Then _
          ContactsGridView.SelectedIndex = i
      Catch
      End Try
    Next
  End Sub

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

  Protected Sub ContactsGridView_PageIndexChanging()
    ContactsGridView.SelectedIndex = -1
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>GridView OnSelectedIndexChanged Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView OnSelectedIndexChanged Example</h3>
      
      <asp:GridView ID="ContactsGridView"
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"        
        AutoGenerateSelectButton="True"
        OnSelectedIndexChanged="ContactsGridView_SelectedIndexChanged"        
        AllowPaging="True" AllowSorting="True" 
        OnDataBound="ContactsGridView_DataBound" 
        OnSorting="ContactsGridView_Sorting" 
        OnPageIndexChanging="ContactsGridView_PageIndexChanging"
        runat="server">
        <SelectedRowStyle BackColor="Aqua" />
      </asp:GridView>
            
      <!-- 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">
      </asp:SqlDataSource>


    </form>
  </body>
</html>

注解

单击 SelectedIndexChanged 行的“选择”按钮时,但在控件处理选择操作之后 GridView ,将引发 该事件。 这使你可以提供一个事件处理方法,该方法执行自定义例程,例如,每当发生此事件时,使用当前所选行更新状态标签。

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnSelectedIndexChanged 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnSelectedIndexChanged(EventArgs) 时,一定要调用基类的 OnSelectedIndexChanged(EventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅