ListView.SelectedDataKey Property


Gets the data-key value for the selected item in a ListView control.

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

Public Overridable ReadOnly Property SelectedDataKey As DataKey

Property Value

Type: System.Web.UI.WebControls.DataKey

The data key for the selected item in a ListView control. The default is null, which indicates that no item is currently selected.

Exception Condition

No data keys are specified in the DataKeyNames property.

When the DataKeyNames property is set, the ListView control creates a DataKey object for each item in the control by using the value or values of the specified field or fields. The DataKey objects are then added to the control's DataKeys collection. Normally, the DataKeys property is used to retrieve the DataKey object for a specific data item in the ListView control. However, if you want to retrieve only the DataKey object of the currently selected item, you can use the SelectedDataKey property as a shortcut.

Using the SelectedDataKey property is the same as retrieving the DataKey object from the DataKeys collection at the index that is specified by the SelectedIndex property. You can also use the SelectedValue property to retrieve the data-key value for the currently selected item directly.

If you are creating a ControlParameter object and you want to access a key field other than the first field, use the indexed SelectedDataKey property in the PropertyName property of the ControlParameter object.

The following example shows how to use the second key field as a parameter in a master/detail scenario. A ListView control is used to display records from the Product Inventory table of the AdventureWorks database. When an item is selected in the ListView control, the details of the product are displayed in another ListView control. ProductID is the second key name in the first ListView control. To access the second key, the code uses the value of ProductInventoryListView.SelectedDataKey[1] (ProductInventoryListView.SelectedDataKey(1) in Visual Basic) as the PropertyName for the ControlParameter object. This parameter object is in turn used by the SqlDataSource control that the second ListView control is bound to.

<%@ Page language="VB" %>

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

<script runat="server">

  Protected Sub ProductInventoryListView_PagePropertiesChanging(ByVal sender As Object, _
    ByVal e As PagePropertiesChangingEventArgs)

    ProductInventoryListView.SelectedIndex = -1

  End Sub


<html xmlns="" >
  <head id="Head1" runat="server">
    <title>ListView SelectedDataKey Example</title>
    <style type="text/css">
      	border: 1px solid #008080;
      	background-color: #008080;
      	color: White;
      .item td { border: 1px solid #008080; }
    <form id="form1" runat="server">

      <h3>ListView SelectedDataKey Example</h3>

      <asp:ListView runat="server" 
          <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 runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="ProductInventoryPager">
              <asp:NextPreviousPagerField ShowFirstPageButton="true"
                ShowLastPageButton="true" />
          <tr class="item" runat="server">
              <asp:ImageButton runat="server" 
                CommandName="Select" />
              <asp:Label runat="server" ID="ProductIDLabel" Text='<%#Eval("ProductID") %>' />
              <asp:Label runat="server" ID="LocationIDLabel" Text='<%#Eval("LocationID") %>' />
              <asp:Label runat="server" ID="ShelfLabel" Text='<%#Eval("Shelf") %>' />
              <asp:Label runat="server" ID="BinLabel" Text='<%#Eval("Bin") %>' />
              <asp:Label runat="server" ID="QuantityLabel" Text='<%#Eval("Quantity") %>' />
      <br /><br />

      <asp:ListView runat="server" ID="ProductListView"
          <b>Product Details</b>          
          <table runat="server" id="tblDetails">
            <tr runat="server" id="itemPlaceholder" />
          <tr runat="server">
            <td class="header">Product ID:</td>
              <asp:Label runat="server" ID="ProductLabel" Text='<%#Eval("ProductID") %>' />
          <tr runat="server">
            <td class="header">Name:</td>
              <asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
          <tr runat="server">
            <td class="header">Color:</td>
              <asp:Label runat="server" ID="ColorLabel" Text='<%#Eval("Color") %>' />
          <tr runat="server">
            <td class="header">Price:</td>
              <asp:Label runat="server" ID="ListPriceLabel" Text='<%#Eval("ListPrice", "{0:c}") %>' />

      <!-- 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 ID="ProductDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ProductID], [Name], [Color], [ListPrice], [ProductNumber]
                         FROM Production.Product
                         WHERE ProductID = @ProductID
                         ORDER BY [Name]">
            <asp:ControlParameter Name="ProductID" 
              PropertyName="SelectedDataKey[1]"  />


.NET Framework
Available since 3.5
Return to top