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.