The items for deleting columns in the gridview are very easy to design on the interface. In previous versions, we had to add another column, template column, or link button, in short, you need to find the control property and The onclick event. This mechanism is also used in ASP. NET 2.0. First, we set commandfield and set the showeditbutton and showdeletebutton attributes to true. Of course, this is a flexible application. You can also set only the showdeletebutton attribute so that you can change the order of the following code. The page code is as follows:
<Asp: commandfield showeditbutton = "true" headertext = "edit area" showdeletebutton = "true" buttontype = "image"
/>
I am using the image button and put it in the 8th column. Therefore, the following code only uses the settings I have set. In other cases, it is similar to flexible settings. The background code is as follows:
1 protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
2 {
3 if (E. Row. rowtype = datacontrolrowtype. datarow)
4 {
5 imagebutton imgflag = new imagebutton ();
6 imgflag = (imagebutton) E. Row. cells [7]. controls [2]);
7 if (imgflag. alternatetext = "delete ")
8 {
9 imgflag. Attributes. Add ("onclick", "javascript: Return confirm ('Are you sure you want to delete it !? ')");
// Button BTN = (button) E. Row. cells [5]. controls [0];
// BTN. attributes. add ("onclick", "javascript: Return confirm ('Are you sure you want to delete: \" "+ E. row. cells [1]. text +? ')");
// Button1.attributes. Add ("onclick", "Return confirm ('OK? ')");
// Button. Attributes. Add ("onclick", "If (confirm ('Are you sure ...? ') {Return true;} else {return false ;}")
10}
11
12}
13
14}
Tip: if the image button is not set, change imagebutton to linkbutton.
The original cause is explained. After the showeditbutton and showdeletebutton attributes are set in the commandfield column ,. net will add spaces (& nbsp;) between the showeditbutton and showdeletebutton generated results. Therefore, a cell contains three controls: Image and & nbsp; (Space), image (delete), (imagebutton) E. row. cells [7]. controls [2]) specifies the position of the column to be deleted. If you find this control, you can add the attribute. However, after clicking the edit button, there are no Delete columns in the "Update" and "cancel" columns. Therefore, you need
The Code is as follows:
If (imgflag. alternatetext = "delete ")
{
Imgflag. Attributes. Add ("onclick", "javascript: Return confirm ('Are you sure you want to delete it !? ')");
}
Tip: if it is a linkbutton, you can judge it based on the text of the linkbutton.