Bagikan melalui


GridView.OnSelectedIndexChanged(EventArgs) Metode

Definisi

Memunculkan kejadian 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)

Parameter

e
EventArgs

Yang EventArgs berisi data peristiwa.

Contoh

Contoh berikut menunjukkan cara membuat penanganan aktivitas untuk peristiwa tersebut SelectedIndexChanged . Dalam contoh ini, baris yang dipilih dipertahankan dalam status tampilan. Jadi bahkan setelah operasi pengurutan atau penomoran halaman, hanya baris tersebut yang akan dipilih.

<%@ 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>

Keterangan

Peristiwa SelectedIndexChanged dimunculkan saat tombol Pilih baris diklik, tetapi setelah GridView kontrol menangani operasi pilih. Ini memungkinkan Anda untuk menyediakan metode penanganan peristiwa yang melakukan rutinitas kustom, seperti memperbarui label status dengan baris yang saat ini dipilih, setiap kali peristiwa ini terjadi.

Menaikkan peristiwa memanggil penanganan aktivitas melalui delegasi. Untuk informasi selengkapnya, lihat Menangani dan Menaikkan Peristiwa.

Metode ini OnSelectedIndexChanged juga memungkinkan kelas turunan untuk menangani peristiwa tanpa melampirkan delegasi. Ini adalah teknik yang disukai untuk menangani peristiwa di kelas turunan.

Catatan Bagi Inheritor

Saat mengambil alih di kelas turunan OnSelectedIndexChanged(EventArgs) , pastikan untuk memanggil metode kelas OnSelectedIndexChanged(EventArgs) dasar sehingga delegasi terdaftar menerima peristiwa tersebut.

Berlaku untuk

Lihat juga