ListView.SelectedDataKey Właściwość

Definicja

Pobiera wartość klucza danych dla wybranego elementu w kontrolce ListView .

public:
 virtual property System::Web::UI::WebControls::DataKey ^ SelectedDataKey { System::Web::UI::WebControls::DataKey ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DataKey SelectedDataKey { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedDataKey : System.Web.UI.WebControls.DataKey
Public Overridable ReadOnly Property SelectedDataKey As DataKey

Wartość właściwości

DataKey

Klucz danych dla wybranego elementu w kontrolce ListView . Wartość domyślna to null, która wskazuje, że żaden element nie jest obecnie zaznaczony.

Atrybuty

Wyjątki

W właściwości nie określono DataKeyNames żadnych kluczy danych.

Przykłady

W poniższym przykładzie pokazano, jak użyć drugiego pola klucza jako parametru w scenariuszu wzorca/szczegółów. Kontrolka ListView służy do wyświetlania rekordów z tabeli Product Inventory bazy danych AdventureWorks. Po wybraniu elementu w kontrolce ListView szczegóły produktu są wyświetlane w innej ListView kontrolce. ProductID to druga nazwa klucza w pierwszej ListView kontrolce. Aby uzyskać dostęp do drugiego klucza, kod używa wartości ProductInventoryListView.SelectedDataKey[1] (ProductInventoryListView.SelectedDataKey(1) w Visual Basic) jako PropertyName obiektu ControlParameter . Ten obiekt parametru jest z kolei używany przez kontrolkę SqlDataSource , z którą jest powiązana druga ListView kontrolka.

<%@ 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 ProductInventoryListView_PagePropertiesChanging(object sender, 
    PagePropertiesChangingEventArgs e)
  {
    ProductInventoryListView.SelectedIndex = -1;
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView SelectedDataKey Example</title>
    <style type="text/css">
      .header
      {
        border: 1px solid #008080;
        background-color: #008080;
        color: White;
      }
      .item td { border: 1px solid #008080; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
    
      <h3>ListView SelectedDataKey Example</h3>
      
      <asp:ListView runat="server" 
        ID="ProductInventoryListView"
        DataSourceID="ProductInventoryDataSource" 
        DataKeyNames="LocationID,ProductID" 
          OnPagePropertiesChanging="ProductInventoryListView_PagePropertiesChanging">
        <LayoutTemplate>
          <b>Product Inventory</b>
          <br />
          <table width="400px" runat="server" id="tblProducts">
            <tr class="header" runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">Product ID</th>
              <th runat="server">Location ID</th>
              <th runat="server">Shelf</th>
              <th runat="server">Bin</th>
              <th runat="server">Quantity</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ProductInventoryPager">
            <Fields>
              <asp:NextPreviousPagerField ShowFirstPageButton="true"
                ShowLastPageButton="true" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr class="item" runat="server">
            <td>
              <asp:ImageButton runat="server" 
                ID="SelectButton" 
                Width="15"
                Height="15"
                ImageUrl="~/images/select.jpg" 
                CommandName="Select" />
            </td>
            <td>
              <asp:Label runat="server" ID="ProductIDLabel" Text='<%#Eval("ProductID") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="LocationIDLabel" Text='<%#Eval("LocationID") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="ShelfLabel" Text='<%#Eval("Shelf") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="BinLabel" Text='<%#Eval("Bin") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="QuantityLabel" Text='<%#Eval("Quantity") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
      <br /><br />
          
      <asp:ListView runat="server" ID="ProductListView"
        DataSourceID="ProductDataSource">
        <LayoutTemplate>
          <b>Product Details</b>          
          <table runat="server" id="tblDetails">
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td class="header">Product ID:</td>
            <td>
              <asp:Label runat="server" ID="ProductLabel" Text='<%#Eval("ProductID") %>' />
            </td>
          </tr>
          <tr runat="server">
            <td class="header">Name:</td>
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
          </tr>
          <tr runat="server">
            <td class="header">Color:</td>
            <td>
              <asp:Label runat="server" ID="ColorLabel" Text='<%#Eval("Color") %>' />
            </td>
          </tr>
          <tr runat="server">
            <td class="header">Price:</td>
            <td>
              <asp:Label runat="server" ID="ListPriceLabel" Text='<%#Eval("ListPrice", "{0:c}") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
       
      <!-- 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="ProductInventoryDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [LocationID], [Shelf], [Bin], [Quantity]
                       FROM Production.ProductInventory">
      </asp:SqlDataSource>
      
      <asp:SqlDataSource ID="ProductDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [Name], [Color], [ListPrice], [ProductNumber]
                         FROM Production.Product
                         WHERE ProductID = @ProductID
                         ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductID" 
              DefaultValue="0"
              ControlID="ProductInventoryListView" 
              PropertyName="SelectedDataKey[1]"  />
          </SelectParameters>
      </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 ProductInventoryListView_PagePropertiesChanging(ByVal sender As Object, _
    ByVal e As PagePropertiesChangingEventArgs)
    
    ProductInventoryListView.SelectedIndex = -1
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView SelectedDataKey Example</title>
    <style type="text/css">
      .header
      {
        border: 1px solid #008080;
        background-color: #008080;
        color: White;
      }
      .item td { border: 1px solid #008080; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
    
      <h3>ListView SelectedDataKey Example</h3>
      
      <asp:ListView runat="server" 
        ID="ProductInventoryListView"
        DataSourceID="ProductInventoryDataSource" 
        DataKeyNames="LocationID,ProductID" 
          OnPagePropertiesChanging="ProductInventoryListView_PagePropertiesChanging">
        <LayoutTemplate>
          <b>Product Inventory</b>
          <br />
          <table width="400px" runat="server" id="tblProducts">
            <tr class="header" runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">Product ID</th>
              <th runat="server">Location ID</th>
              <th runat="server">Shelf</th>
              <th runat="server">Bin</th>
              <th runat="server">Quantity</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ProductInventoryPager">
            <Fields>
              <asp:NextPreviousPagerField ShowFirstPageButton="true"
                ShowLastPageButton="true" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr class="item" runat="server">
            <td>
              <asp:ImageButton runat="server" 
                ID="SelectButton" 
                Width="15"
                Height="15"
                ImageUrl="~/images/select.jpg" 
                CommandName="Select" />
            </td>
            <td>
              <asp:Label runat="server" ID="ProductIDLabel" Text='<%#Eval("ProductID") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="LocationIDLabel" Text='<%#Eval("LocationID") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="ShelfLabel" Text='<%#Eval("Shelf") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="BinLabel" Text='<%#Eval("Bin") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="QuantityLabel" Text='<%#Eval("Quantity") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
      <br /><br />
          
      <asp:ListView runat="server" ID="ProductListView"
        DataSourceID="ProductDataSource">
        <LayoutTemplate>
          <b>Product Details</b>          
          <table runat="server" id="tblDetails">
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td class="header">Product ID:</td>
            <td>
              <asp:Label runat="server" ID="ProductLabel" Text='<%#Eval("ProductID") %>' />
            </td>
          </tr>
          <tr runat="server">
            <td class="header">Name:</td>
            <td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
            </td>
          </tr>
          <tr runat="server">
            <td class="header">Color:</td>
            <td>
              <asp:Label runat="server" ID="ColorLabel" Text='<%#Eval("Color") %>' />
            </td>
          </tr>
          <tr runat="server">
            <td class="header">Price:</td>
            <td>
              <asp:Label runat="server" ID="ListPriceLabel" Text='<%#Eval("ListPrice", "{0:c}") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
       
      <!-- 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="ProductInventoryDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [LocationID], [Shelf], [Bin], [Quantity]
                       FROM Production.ProductInventory">
      </asp:SqlDataSource>
      
      <asp:SqlDataSource ID="ProductDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [Name], [Color], [ListPrice], [ProductNumber]
                         FROM Production.Product
                         WHERE ProductID = @ProductID
                         ORDER BY [Name]">
          <SelectParameters>
            <asp:ControlParameter Name="ProductID" 
              DefaultValue="0"
              ControlID="ProductInventoryListView" 
              PropertyName="SelectedDataKey[1]"  />
          </SelectParameters>
      </asp:SqlDataSource>
       
    </form>
  </body>
</html>

Uwagi

Po ustawieniu DataKeyNames właściwości kontrolka ListView tworzy DataKey obiekt dla każdego elementu 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 elementu danych w kontrolceListView. Jeśli jednak chcesz pobrać tylko DataKey obiekt aktualnie wybranego elementu, możesz użyć SelectedDataKey właściwości jako skrótu.

SelectedDataKey Użycie właściwości jest takie samo, jak pobieranie DataKey obiektu z DataKeys kolekcji w indeksie SelectedIndex określonym przez właściwość. Możesz również użyć SelectedValue właściwości , aby pobrać wartość klucza danych dla aktualnie wybranego elementu bezpośrednio.

Jeśli tworzysz ControlParameter obiekt i chcesz uzyskać dostęp do pola klucza innego niż pierwsze pole, użyj właściwości indeksowanej SelectedDataKey we PropertyName właściwości ControlParameter obiektu.

Dotyczy

Zobacz też