Confirmation prompt for deletion added to DataGrid, gridview, and detailsview

Source: Internet
Author: User
This article describes how to add the deletion confirmation prompt in the DataGrid (1.0), gridview (2.0), and detailsview (2.0) of DOTNET.

First, add the itemdatabound event of the DataGrid in "1.0" and add the following Code < ASP: templatecolumn headertext = " Delete " >
< Itemtemplate >
< ASP: imagebutton ID = " Delete " Runat = " Server " Commandname = " Delinfo " Imagealign = " Middle " Imageurl = " Images/btn_del.gif " > </ ASP: imagebutton >
</ Itemtemplate >
</ ASP: templatecolumn >

Datagrid_itemdatabound
Private   Void Datagrid_itemdatabound ( Object Sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype = Listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem)
{
Datarowview DRV = (Datarowview) E. Item. dataitem;

Imagebutton L = (Imagebutton) E. Item. cells [ 4 ]. Findcontrol ( " Delete " );
L. Attributes. Add ( " Onclick " , " Javascript: Return confirm ('Are you sure you want to delete " + DRV. Row [ " Sname " ]. Tostring () + " ? ') " );
}
}

Note:"Sname"To add the column name of the validation data in confirmation, this example uses imagebutton and other buttons, similar to this.

In the 2.0 gridview, add the rowdatabound event of the gridview and add the following similar code:<ASP: commandfield showdeletebutton="True" />

  Protected   Void Gridview1_rowdatabound ( Object Sender, gridviewroweventargs E)
{
If (E. Row. rowtype = Datacontrolrowtype. datarow)
E. Row. cells [ 10 ]. Attributes. Add ( " Onclick " , " Javascript: Return confirm ('Are you sure you want to delete \ "" + E. Row. cells [1]. Text + " \ " ? ') " );

}

Note: cells [10] is the column where the delete button is located. The delete button must be in a separate column and can be converted to a template column.

Finally, detailsview directly uses the onclientclick of the. Net Control as follows: < ASP: templatefield showheader = " False " >
< Itemtemplate >
< ASP: linkbutton ID = " Linkbutton1 " Runat = " Server " Onclientclick = " Javascript: Return confirm ('Are you sure you want to delete it? ') " Causesvalidation = " False " Commandname = " Delete "
Text = " Delete " > </ ASP: linkbutton >
</ Itemtemplate >
</ ASP: templatefield >

Well, this is the end.

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.