Triggered by the OnRowCommand event of the control dynamically bound to the GridView

Source: Internet
Author: User

Topic: The dynamically generated control of the GridView cannot trigger the OnRowCommand event. When you click the control button, the value of the control disappears.

Case,

The order of Data columns to be bound is dynamically generated, and some data columns need to be bound with the Button control. Therefore, you need to dynamically bind the TemplateField.

If the OnRowCommand event cannot be triggered or the control button is clicked, the column value of the control disappears.

The OnRowCommand Code cannot be triggered as follows:

Page_Load ((! === GridViewTemplate (DataControlRowType. header, = GridViewTemplate (DataControlRowType. dataRow, ==] =] = EventHandler (GridViewTemplate (DataControlRowType, === GridViewTemplate (DataControlRowType, controlID, =. controlID ======= (e. row. rowType = e. row. findControl () = (e. commandName = index = GridViewRow row = ListItem item = Server. htmlDecode (row. cells []. text) ++View Code

Later, the data binding is removed from Page. IsPostBack, which triggers the OnRowCommand event. However, when you click it, you need to re-query the GridView, which is less efficient.

Page_Load ((! === GridViewTemplate (DataControlRowType. Header, = GridViewTemplate (DataControlRowType. DataRow, ==] =] =View Code

Final Solution,

1. Use BoundField to replace the TemplateField method. The Code is as follows.

 ButtonField adup = == = = 

 

2. Rewrite the OnInit method. Binding TemplateField on OnInit triggers the OnRowCommand event.

          = = =  GridViewTemplate(DataControlRowType.Header, =  GridViewTemplate(DataControlRowType.DataRow, , =        }

2,

It is estimated that the dynamic binding control cannot be triggered because:

1) The controls dynamically bound with TemplateField are not server-side controls and are not recorded in ViewState. Therefore, after the page is refreshed, the values of these columns will disappear and the OnRowCommand event will naturally not be triggered.

2) When BoundField is used, ViewState is recorded. Therefore, after the page is refreshed, the event OnRowCommand is triggered.

3) bind the control to OnInit. First, bind the control and event (OnRowCommand is rebound ). In this way, the OnRowCommand event will also be triggered.

 

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.