It generally happens with many developers that they want some value from the datasource to be
carried in Asp.net Data controls
(ListView, GridView etc) but don’t want to show
that value in the UI.
One traditional way to do this
is to bind that value to a column or hidden field and set the control visibility as false.
To get rid of that we can use DataKeys and DataKeyNames property of ListView and GridView.
property is to specify the field names that we want to carry as hidden, from the data source. For more
than one field name we use a comma-separated list of field names.
DataKeyNames = ‘Field1, Field2, Field3’
DataKeyNames property is set, the ListView control automatically
creates a DataKey
object for each item (GridViewRow, ListViewDataItem) in the control. The DataKey object contains
the values of the field or fields that are specified in the DataKeyNames
property. Then it is added to the
control’s DataKeys collection. This provides a convenient way to access
different fields of each item.
So, in the code behind the DataKey object
give a access to these hidden values. You just need to pass
the key to the DataKeys collection to get the
value out of it.
The following example explain all these things.
Design Page: to specify the column names in the DataKeyNames property of
the ListView or GridView
< asp :style=”color: #a31515;”>ListView ID=”lvExample”runat =”server”OnItemDataBoundEmployeeId,WorkStatus<span