VKinfotek Inc.
  • Us
    About Us
    Contact Us
  • FAQs
  • Ready Software
    Ready ERP ASP Core Software for Azure Cloud
    Ready Web ERP Software
    Ready ASP.Net Azure Software
    Ready C# SQL Server Accounting Software
    Ready ASP.Net MVC and EF Software
First slide
Earn $100/hour in USA! Click Here

How to get the datakey value in DetailsView control

To get the datakey value of the selected record of the DetailsView control, we need to set the DataKeyNames property of the DetailsView control. This property should be assigned with the Primary Key of the database table. After doing this, we can retrieve the DataKey value in the following ways.

string ID=ItemsDetailsView.DataKey.Value.ToString();
or
string ID = ItemsDetailsView.DataKey[0].ToString();

We can use the above code in the DataBound event of the DetilaView control to retrieve the DataKeys. The code to retrieve DataKey value in the DataBound event of detailsView control is given below.

<asp:DetailsView ID="ItemsDetailsView" runat="server"  
AutoGenerateRows="true" 
   AllowPaging="true" DataKeyNames="ItemID" 
   DataSourceID="MyDataSource"  AutoGenerateInsertButton="true" 
    OnDataBound="ItemsDetailsView_DataBound"
   AutoGenerateEditButton="true">
   </asp:DetailsView>
<br/>
<asp:label id="Message"    forecolor="Red"    runat="server"/>
<asp:SqlDataSource ID="MyDataSource"  
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT ItemID,ItemName,ItemType,ClStk FROM ItemTable" 
runat="server"      />


protected void ItemsDetailsView_DataBound(object sender, EventArgs e)
 {
	string temp=ItemsDetailsView.DataKey.Value.ToString();
	//or
	string ID = ItemsDetailsView.DataKey[0].ToString();
 }


The code for retrieving the DataKey value in the PageIndexChanged event of detailsView control is given below.

<asp:DetailsView ID="DetailsView1" runat="server"  AutoGenerateRows="true" 
   AllowPaging="true" DataKeyNames="Code" 
   DataSourceID="MyDataSource"  OnPageIndexChanged="DetailsView1_PageIndexChanged"  
   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"/>


When the page index of the DetailsView control is changed, the PageIndexChanged event is fired. We cannot retrieve the datakey values as the data is not yet bound to the detailsView control. So, we call Databind() method in the PageIndexChanged event handler and then retrieve the datakey value as shown in the code given below.

protected void DetailsView1_PageIndexChanged(object sender, EventArgs e)
{
        //string ID = DetailsView1.DataKey.Value.ToString();
        DetailsView1.DataBind();
        string ID = DetailsView1.DataKey[0].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?
  • Using detailsview control datakeynames property
  • 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


  • SITEMAP
  • Terms of use
  • Testimonials

Back to top

Copyright © 2016 - All Rights Reserved - VKInfotek.com