ListView.DataKeyNames Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets an array that contains the names of the primary key fields for the items displayed in a ListView control.
public:
virtual property cli::array <System::String ^> ^ DataKeyNames { cli::array <System::String ^> ^ get(); void set(cli::array <System::String ^> ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))]
public virtual string[] DataKeyNames { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.StringArrayConverter))>]
member this.DataKeyNames : string[] with get, set
Public Overridable Property DataKeyNames As String()
Property Value
An array that contains the names of the primary key fields for the items displayed in a ListView control.
- Attributes
Examples
The following example shows how to use the DataKeyNames property to specify the key field of the data source.
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") + ", " + Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- 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="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
<asp:ListView runat="server"
ID="EmployeesListView"
OnItemCommand="EmployeesListView_OnItemCommand"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table runat="server" id="tblEmployees"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="EmployeesDataPager" runat="server" PageSize="10">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel"
Text='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Add To List"
CommandName="AddToList"
CommandArgument='<%#Eval("LastName") & ", " & Eval("FirstName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<br /><br />
<b>Selected Employees:</b><br />
<asp:ListBox runat="server" ID="SelectedEmployeesListBox" Rows="10" Width="300px" />
<!-- 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="EmployeesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName]
FROM HumanResources.vEmployee
ORDER BY [LastName], [FirstName], [EmployeeID]">
</asp:SqlDataSource>
Remarks
Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. To set this property declaratively, use a comma-separated list of field names.
When the DataKeyNames property is set, the ListView control automatically populates its DataKeys collection with the values from the specified field or fields. This provides a convenient way to access the primary keys of each item.
Important
The ListView control stores key field values in the control-state field. If these values contain sensitive information, we recommend that you enable view-state encryption by setting the ViewStateEncryptionMode property to ViewStateEncryptionMode.Always
.
You must set the DataKeyNames property for the automatic updating and deleting features of the ListView control to work. The values of these key fields are passed to the data source control in order to match the item to update or delete.
Fields that are specified as data keys by using the DataKeyNames property are passed to the data source control during update and delete operations by using the Keys
collection. Data key fields are passed to the data source control even if they are not bound to controls in a template.