Add a deletion prompt to the delete column in The gridview

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.