This demand came from a forum friend raised the question, he would like to be able to customize the edit, cancel the button, rather than the GridView with the editing and cancellation. This is only a point to propose some solutions.
First set up a GridView in the foreground of the page.
<Div> <Asp:gridviewID= "GridView1"runat= "Server"> <Columns> <Asp:templatefieldHeaderText= "Action"> <ItemTemplate> <Table> <TDAlign= "Center"> <Asp:buttonID= "Edit"runat= "Server"Text= "Edit"Visible= "true"OnClick= "Edit_click"CommandArgument= "<%# container.dataitemindex%>" /> <Asp:buttonID= "Cancel"runat= "Server"Text= "Cancel"Visible= "false"OnClick= "Cancel_click" /> </TD> </Table> </ItemTemplate> </Asp:templatefield> </Columns> </Asp:gridview> </Div>
Notice here that I set a Dataitemindex value for the CommandArgument property of the button edit, which is the index value of the default row. This parameter allows you to get the line number of the GridView.
Then I bind the data source when the homepage is loaded.
protected voidPage_Load (Objectsender, EventArgs e) { if(!page.ispostback) {DataTable dt=NewDataTable (); Dt. Columns.Add ("ID",typeof(int)); Dt. Columns.Add ("name",typeof(string)); Dt. Rows.Add (10001,"guwei40371"); Dt. Rows.Add (10002,"guwei40372"); This. Gridview1.datasource =dt. DefaultView; This. Gridview1.databind (); } }
Here is a simple, binding two columns to the GridView binding.
Next two button events:
protected voidEdit_click (Objectsender, EventArgs e) { intindex = Convert.ToInt32 (sender asButton). CommandArgument);//get to line numberButton Button = This. Gridview1.rows[index]. FindControl ("Cancel") asButton;//Find the Cancel button for the current linebutton. Visible =true;//Set the button's visible to True} protected voidCancel_click (Objectsender, EventArgs e) { intRow = ((GridViewRow) ((Button) sender). NamingContainer). RowIndex;//the rowindex of the naming container (GridViewRow) is found directly via the buttonResponse.Write ("<script>alert ('"+ This. Gridview1.rows[row]. cells[1]. Text +"') </script>");//directly pops up the contents of the current row cell index to 1}
The meaning of the specific code, which is already annotated, is not repeated here.
Finally, look at the effect of execution.
When you click the Edit button, the Cancel button is displayed.
When you click the Cancel button, it pops up 10001.
GridView Edit, Cancel button custom control