Whether in the GridView, DataList, or Repeater control, the Repeater control does not have the DataKeyNames or DataKeyField attribute. To obtain the record's primary key value, you have to bind the primary key field with the Label or HiddenField, and then get it in the background OnItemDataBound.
Although the GridView and DataList controls have the DataKeyNames and DataKeyField attributes, or sometimes the program requires that some non-primary key fields need further function processing to obtain these fields.
Currently, Insus. NET teaches you how to obtain the primary key value or field value in the OnRowDataBound or OnItemDataBound event. For the GridView control or DataList control, you must set the DataKeyNames and DataKeyField attributes. For the Repeater control, you can only use the HiddenField control.
Let's take a lookGridView Control:
'>
Go to the. aspx. cs webpage and write the GridView1_RowDataBound event.
Of course, if you want to obtain the primary key in OnRowDataBound, you do not need to set the DataKeyNames attribute. You can also obtain it like getting other fields. The following is the second method:
Method 3:
Below isDataList ControlDemo,
'>
First:
Method 2:
Method 3:
Below isRepeater controlIn the OnItemDataBound event, obtain the field value.
'>
Method 1:
Method 2:
Method 3: