1. How to delete a commandfield in the gridview? A confirmation box is displayed?
In the gridview provided by vs2005, we can directly add a commandfield deletion column: <asp: commandfield showdeletebutton = "true"/>, and then delete it in its rowdeleting event. However, in most cases, the operator must confirm the deletion before deleting the deletion to avoid accidental deletion due to misoperations.
You can add a confirmation dialog box before deleting the gridview using the following method.
First, click "columns" in the "properties" box of the gridview to enter its "field" designer. In the field designer, select the previously added commandfield "delete" column, in this case, a "convert this segment to templatefied" item is displayed in its attribute list. Click to convert it to the templatefied column.
After exiting the field designer, switch to the source code view and you will find that the column has been changed from the original: <asp: commandfield showdeletebutton = "true"/>
Changed:
In this way, "Are you sure you want to delete it?" is displayed on the client first when you click Delete ?" Dialog box, but the code originally written in the rowdeleting event does not need to be changed.
2.
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
If (E. Row. rowindex>-1)
{
Int id = convert. toint32 (gridview1.datakeys [E. Row. rowindex]. value );
Linkbutton lbtndelete = (linkbutton) E. Row. findcontrol (& amp; quot; lbtndelete & amp; quot ;);
If (lbtndelete! = NULL)
{
Lbtndelete. commandargument = ID. tostring ();
Lbtndelete. Attributes. Add (& amp; quot; onclick & amp; quot;, & amp; quot; <SCRIPT> return confirm ('Are you sure you want to delete it! ') </SCRIPT> & amp; quot ;);
}
}
}
}
3. First reference system. windwos. forms, and then process
4. Add a delete column
Protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
{
Tablecell Tc = (tablecell) E. Row. cells [E. Row. cells. Count-1];
For (INT I = 0; I <TC. Controls. Count; I + = 2)
{
// Cerco il controllo imagebutton (Ho utilizzato quello)
Object o = tc. controls [I];
If (O is imagebutton)
{
// Controllo Trovato!
// Ora aggiungo l 'evento JS onclick per chiedere conferma all' utente
Imagebutton lB = (imagebutton) O;
(Imagebutton) LB ). attributes. add (& amp; quot; onclick & amp; quot;, @ & amp; quot; javascript: Return confirm ('attenzione: sicuro di Voler cancellare? '); & Amp; quot ;);
}
}
}