GridView.SelectedValue Właściwość

Definicja

Pobiera wartość klucza danych wybranego wiersza w kontrolce GridView .

public:
 property System::Object ^ SelectedValue { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public object SelectedValue { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : obj
Public ReadOnly Property SelectedValue As Object

Wartość właściwości

Wartość klucza danych wybranego wiersza w kontrolce GridView .

Atrybuty

Przykłady

W poniższym przykładzie pokazano, jak za pomocą SelectedValue właściwości określić wartość klucza danych wybranego wiersza w kontrolce GridView .


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

  void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)  
  {
        
    // Display the primary key value of the selected row.
    Message.Text = "The primary key value of the selected row is " +
      CustomersGridView.SelectedValue.ToString() + ".";
    
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView SelectedValue Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView SelectedValue Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
                
      <br/><br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        allowpaging="true"
        autogeneratecolumns="true"
        autogenerateselectbutton="true"    
        datakeynames="CustomerID"
        onselectedindexchanged="CustomersGridView_SelectedIndexChanged"   
        runat="server">
                
        <selectedrowstyle backcolor="LightBlue"
          forecolor="DarkBlue"/> 
               
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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">

  Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        
    ' Display the primary key value of the selected row.
    Message.Text = "The primary key value of the selected row is " & _
      CustomersGridView.SelectedValue.ToString() & "."
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView SelectedValue Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView SelectedValue Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
                
      <br/><br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        allowpaging="true"
        autogeneratecolumns="true"
        autogenerateselectbutton="true"    
        datakeynames="CustomerID"
        onselectedindexchanged="CustomersGridView_SelectedIndexChanged"   
        runat="server">
                
        <selectedrowstyle backcolor="LightBlue"
          forecolor="DarkBlue"/> 
               
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

Uwagi

DataKeyNames Gdy właściwość jest ustawiana z rozdzielaną przecinkami listą nazw pól reprezentujących klucz podstawowy źródła danych, GridView kontrolka automatycznie tworzy DataKey obiekt dla każdego wiersza w kontrolce przy użyciu wartości lub wartości określonego pola lub pola. Obiekty DataKey są następnie dodawane do kolekcji kontrolki DataKeys . DataKeys Zwykle właściwość jest używana do pobierania DataKey obiektu dla określonego wiersza danych w kontrolceGridView. Jeśli jednak wystarczy pobrać DataKey obiekt aktualnie wybranego wiersza, możesz po prostu użyć SelectedDataKey właściwości jako skrótu. Jako kolejny skrót możesz bezpośrednio określić wartość klucza danych pierwszego pola klucza wybranego wiersza za pomocą SelectedValue właściwości .

Jeśli tworzysz ControlParameter obiekt i chcesz uzyskać dostęp do pola klucza innego niż pierwsze pole, użyj SelectedDataKey właściwości . Aby zapoznać się z przykładem, zobacz SelectedDataKey.

Dotyczy

Zobacz też