Original post http://www.cnblogs.com/FreeHorse/articles/565333.html
The delete button of the gridview has three types: linkbutton, button, and imagebutton. Here I only talk about linkbutton, because the three are class-connected, and I believe that one of the other two are called. Drag the gridview control from the toolbox. in intelligent sensing, that is, the triangle in the upper right corner, select the edit column, select Delete in cammandfield, and click Add OK. The column is added after deletion. Add a rowdatabound event to the gridview event,CodeAs follows:
<Asp: gridview id ="Gridview1"Runat ="Server"Onrowdatabound ="Gridview1_rowdatabound"> <Columns> <asp: commandfield showdeletebutton ="True"/> </Columns> </ASP: gridview>
Then we bind the data in the background. The Code is as follows:
// Bind the gridviewPrivate VoidShowdatainfon (){// Test DataString[] Datainfon =New String[] {"Test data 1","Test data 2","Test data 3"}; Gridview1.datasource = datainfon; gridview1.databind ();}
Next, add the deletion prompt in the rowdatabound event. Find the control at the current row location and add the deletion prompt to the control. The Code is as follows:
// Prompt for adding the delete button when binding dataProtected VoidGridview1_rowdatabound (ObjectSender, gridviewroweventargs e ){// Only data rows are bound to Data.If(E. Row. rowtype = datacontrolrowtype. datarow ){// Because it is a link button, a link button is declared and changed according to the actual situation.Linkbutton lnkbtfalg = E. Row. cells [0]. controls [0]AsLinkbutton; lnkbtfalg. Attributes. Add ("Onclick","Define Crip: Return confirm ('Do you really want to delete it! ')");}}
In addition, cell [0] indicates the first column, controls [0] indicates the first control, and so on.