ListView.SelectedDataKey Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

abstract SelectedDataKey : DataKey with get
override SelectedDataKey : DataKey with get

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.

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

.NET Framework
Available since 3.5
Return to top