Export (0) Print
Expand All
Expand Minimize

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)

virtual property DataKey^ SelectedDataKey {
	DataKey^ get ();

Property Value

Type: System.Web.UI.WebControls::DataKey
The data key for the selected item in a ListView control. The default is nullptr, which indicates that no item is currently selected.


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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft