Confirmation box prompted when the GridView deletes columns using CommandField

Source: Internet
Author: User

A prompt box is displayed when you use CommandField to delete the content in. in the GridView provided by net2005, 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:
Copy codeThe Code is as follows:
<Asp: TemplateField ShowHeader = "False">
<ItemTemplate>
<Asp: LinkButton ID = "LinkButton1" runat = "server" CausesValidation = "False" CommandName = "Delete" Text = "Delete"> </asp: LinkButton>
</ItemTemplate>

Finally, add OnClientClick = "return confirm ('Are you sure you want to delete it? ');"

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.

Method 2:

Implementation Method:

Double-click the OnRowDataBound event of the GridView;

Add code in the gridviewdomainrowdatabound () method in the background. The final code is as follows:
Copy codeThe Code is as follows:

Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
// Bind a 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 ('Are you sure you want to delete: \" "+ e. row. cells [1]. text +? ')");
}
}

}

The preceding two methods are the most common methods for deleting controls in the GridView. These two methods have not yet been bound to 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.