ListView.SelectedDataKey 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 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
属性值
ListView 控件中的选定项的数据键值。 默认值为 null
,表示当前未选择项。
- 属性
例外
DataKeyNames 属性中未指定数据键。
示例
以下示例演示如何使用第二个键字段作为主/详细信息方案中的参数。 控件 ListView 用于显示 AdventureWorks 数据库的 Product Inventory 表中的记录。 在控件中选择 ListView 某个项时,产品的详细信息会显示在另一个 ListView 控件中。 ProductID 是第一 ListView 个控件中的第二个键名称。 若要访问第二个键,代码使用Visual Basic) 中的 (ProductInventoryListView.SelectedDataKey(1)
值ProductInventoryListView.SelectedDataKey[1]
作为PropertyNameControlParameter对象的值。 此参数对象又由第二个ListView控件绑定到的SqlDataSource控件使用。
<%@ 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"> </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"> </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>
注解
DataKeyNames设置属性时,该ListView控件使用指定字段或字段的值为控件中的每个项创建一个DataKey对象。 然后,对象 DataKey 将添加到控件的 DataKeys 集合中。 通常,该 DataKeys 属性用于检索 DataKey 控件中 ListView 特定数据项的对象。 但是,如果只想检索 DataKey 当前所选项的对象,则可以使用 SelectedDataKey 该属性作为快捷方式。
SelectedDataKey使用该属性与从由属性指定的SelectedIndex索引处DataKeys的集合中检索DataKey对象相同。 还可以使用该 SelectedValue 属性直接检索当前所选项的数据键值。
如果要创建对象并想要访问第一个ControlParameter字段以外的键字段,请使用对象属性中的PropertyName索引SelectedDataKey属性ControlParameter。