Select rows in asp.net GridView template columns

  • 2020-05-10 18:02:28
  • OfStack

Most of the functions through the template column, in order to facilitate the selection and removal of the use of its own functions and methods, it is easy to achieve, there is no big problem found; However, when deploying to the server, it was found that instead of text, "select" was selected to be displayed. No reason was found for this, so it had to be implemented via template columns instead.
When I changed to the template column implementation, I found that the index value of the row could not be obtained through e.CommandArgument in the gv_sjy_RowCommand event, so I had to think of another way. I found that there are two ways to do this:
1 species:

 
  <asp:LinkButton ID="btnSelect" runat="server" CausesValidation="False" CommandName="Select" Text=" choose " CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"></asp:LinkButton> 

Add the red part, the background can be int rowIndex = Convert.ToInt32 (e.CommandArgument); Gets the index value of the row

The second:
 
  protected void gv_sjy_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
if (e.CommandName == "Select") 
{ 
    GridViewRow gvRow = (GridViewRow)(((LinkButton)e.CommandSource).Parent.Parent); 
int rowIndex = gvRow.RowIndex;// Gets the selected row index  
txtSjyxh.Text = dt2.Rows[rowIndex]["sjyxh"].ToString(); 
} 
} 


Related articles: