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.