Using DetailsView control Datakeynames Property
To identify a record uniquely in the DetailsView control, the DataKeyNames property is used. To learn more about how the DataKeyNames property is used, read the article - Using GridView DataKeyNames property
The following example demonstrates how to set the DataKeyNames property of the DetailsView control.
As in the GridView control, inserting, updating or deleting data in the DetailsView control is done using a DataSource control by setting the DataKeyNames property.
We can retrieve the DataKey value in the detailsView control using the following syntax:
Response.Write("Data key value: " & DetailsView1.DataKey.Value)
We can also assign multiple data keys to the DataKeyNames property. In the below code, we have assigned the multiple fields - Code and Name to the DataKeyNames property.
We can retrieve the multiple datakey values in the DataBound event of the DetailsView control using the below code.
The following example demonstrates how to set the DataKeyNames property of the DetailsView control.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="true" AllowPaging="true" DataKeyNames="Code" DataSourceID="MyDataSource" AutoGenerateInsertButton="true" AutoGenerateEditButton="true"> </asp:DetailsView> <asp:SqlDataSource ID="MyDataSource" ConnectionString="<%$Connectionstrings:ERPConnectionString%>" SelectCommand="SELECT * FROM Sample" UpdateCommand="Update SAMPLE SET Name=@Name,description=@description Where Code=@Code" DeleteCommand="Delete SAMPLE Where Code=@Code" InsertCommand="Insert SAMPLE(Code,Name,description) VALUES(@Code,@Name,@description)" runat="server"/>
As in the GridView control, inserting, updating or deleting data in the DetailsView control is done using a DataSource control by setting the DataKeyNames property.
We can retrieve the DataKey value in the detailsView control using the following syntax:
Response.Write("Data key value: " & DetailsView1.DataKey.Value)
We can also assign multiple data keys to the DataKeyNames property. In the below code, we have assigned the multiple fields - Code and Name to the DataKeyNames property.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="true" AllowPaging="true" DataKeyNames="Code,Name" DataSourceID="MyDataSource" OnPageIndexChanged="DetailsView1_PageIndexChanged" AutoGenerateInsertButton="true" AutoGenerateEditButton="true"> </asp:DetailsView>

We can retrieve the multiple datakey values in the DataBound event of the DetailsView control using the below code.
protected void ItemsDetailsView_DataBound(object sender, EventArgs e) { string ID = ItemsDetailsView.DataKey[0].ToString(); string Name = ItemsDetailsView.DataKey[1].ToString(); }
DetailsView Articles
- Difference between DetailsView and FormView control
- DataBinding a DetailsView control
- GridView and DetailsView Master/Detail page using SqlDataSource control
- GridView and DetailsView master detail page using ObjectDataSource control
- DropDownList and GridView Master/Detail page using ObjectDataSource control
- DetailsView databound event
- DetailsView integer type conversion error?
- How to get the datakey value in DetailsView control
- DetailsView autogeneraterows property
- DetailsView fields
- Formatting DetailsView control with style properties
- Using Boundfields in DetailsView control
- Display message using EmptyDataTemplate in the DetailsView Control
- How to access DetailsView's fields programmatically
- Bind a DetailsView control with a DropDownList control
- Using Command buttons in DetailsView control
- Using Commandfield element in a DetailsView control
- Using buttonfield in a DetailsView control
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
ERP
ERP Software Development
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls