GridView use CommandField Delete Column to implement Delete prompt confirmation box _ practical Tips

Source: Internet
Author: User
The GridView pop-up prompt when using CommandField deletes, in the GridView provided by the. Net2005 we can add a CommandField directly to delete columns: <asp:commandfield showdeletebutton= "True"/&gt, finished deleting in its RowDeleting event. But in most of us do this delete operation need to first let the operator reconfirm, and then delete, in order to avoid misoperation caused by the deletion of the mistake.

You can add a confirmation dialog box before you delete the GridView by following this method.

First, click "Columns" in the GridView Properties box to enter its field designer. Then in the field designer, select the previously added CommandField "Delete" column, where you will see a "convert this segment to Templatefied" item under its list of properties and click Convert it to templatefied column.

After you exit the field designer, switch to Source view you will find that the column has been from the original: <asp:commandfield showdeletebutton= "True"/>
Changed into:
Copy Code code as follows:

<asp:templatefield showheader= "False" >
<ItemTemplate>
<asp:linkbutton id= "LinkButton1" runat= "Server" causesvalidation= "False" commandname= "delete" text= "Remove" ></ Asp:linkbutton>
</ItemTemplate>

Finally in <asp:LinkButton> add: onclientclick= "return confirm (' Confirm to delete?") ');"

Click Delete in this way will be on the client before "confirm to delete?" dialog box, and the code originally written in the RowDeleting event is completely unchanged.

The second method:

Implementation method:

Double-click the Onrowdatabound event of the GridView;

In the background The GridView1_RowDataBound () method adds code, and the final code looks like this:
Copy Code code as follows:


protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If it is a bound data row
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
if (e.row.rowstate = = Datacontrolrowstate.normal | | e.row.rowstate = = datacontrolrowstate.alternate)
{
((LinkButton) e.row.cells[6]. Controls[0]). Attributes.Add ("onclick", "Javascript:return Confirm" (' You are sure to delete: \ "" + e.row.cells[1]. Text + "\"? ")");
}
}

}

These are two of the most common ways to refine the delete controls in the GridView, which have not yet implemented binding on specific data.

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.