The confirmation dialog box is displayed when the row is deleted from the gridview.

Source: Internet
Author: User

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 ;);
}
}
}

 

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.