This often happens when you add a template column in The GridView and put some buttons in it. Then, when you click these buttons, you can obtain Content In two cases, you only need to take the value of one field and the value of multiple fields. Method Is to use the CommandArgument attribute 1. You only need to take the value of one field: Definition in the GridView <Asp: TemplateField ShowHeader = "False"> <ItemTemplate> <Asp: Button ID = "Button1" runat = "server" Text = "get number" CommandArgument = '<% # Eval ("id ") %> 'commandname = "getID"/> </ItemTemplate> <ItemStyle HorizontalAlign = "Center"/> </Asp: TemplateField>In red, this button is set to pass the parameter is the value of the id field of the current row, see the background code Protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e) { If (e. CommandName = "getID ") { Response. Write ("<br> id:" + e. CommandArgument ); Button mybt = new Button (); Mybt = (Button) e. CommandSource; Response. Write ("<br> text:" + mybt. Text ); Response. Write ("<br> type:" + sender. GetType (). tostring ()); }Does the Text attribute of the clicked button appear when I use e. CommandSource? Sometimes buttons may have different Text attributes because of different field values. Well, if a field such as "enable" is recorded in a table, you often need to add a template column in the displayed GridView to display the quick operation buttons such as "enable" and "Disable". It is difficult to see if two buttons are displayed for each row of records, therefore, you can use a button to perform two operations. The button can be defined as follows: <Asp: button id = "button1" runat = "server" text = '<% # convert. toint32 (eval ("isuse") = 0: "enable "? "Disable" %> 'commandargument = '<% # eval ("ID") %> 'commandname = "GETID"/>Check the Text attribute. If the isUse field value is 0, the current record is disabled. Therefore, the shortcut button should display "enable". Otherwise, "disabled" is displayed" Then, you can use e. CommandSource in the RowCommand event to obtain the Text attribute of the button and determine whether the current operation is "enabled" or "disabled ". 2. Obtain the values of Multiple Fields Because the CommandArgument attribute can only pass one value, if you want to pass the value of multiple fields, it is best to convert it to the row number that passes the current row. If we have a row number in the GridView, we can get all the data in this row. Definition in the GridView <Asp: templatefield showheader = "false"> <Itemtemplate> <Asp: button id = "button2" runat = "server" text = "retrieve row number" commandargument = '<% # container. dataitemindex + 1%> 'commandname = "getrow"/> </ItemTemplate> <ItemStyle HorizontalAlign = "Center"/> </Asp: TemplateField>Look at the red line and use the binding <% # Container. DataItemIndex + 1%> to pass the current row number. DataItemIndex starts from 0, so 1 must be added for each row. Background Protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e) { If (e. CommandName = "getRow ") { Response. Write ("current row number:" + e. CommandArgument ); } } You can use GridView1.Rows [e. commandArgument]. cells [I]. text gets the value of column I. If one column is a template column, you can use the FindControl method to obtain the value. |