Obtain the field value in the OnRowDataBound or OnItemDataBound event.

Source: Internet
Author: User

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:


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.