Confirmation dialog box for adding and deleting a gridview

Source: Internet
Author: User

Method 1:

Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)

{

If (e. Row. RowType = DataControlRowType. DataRow)
{
E. Row. Cells [5]. Attributes. Add ("onclick", "return confirm ('' are you sure you want to edit? '')");

}

}

Method 2

In the GridView provided by VS2005, we can directly add a CommandField to delete the column: <asp: CommandField ShowDeleteButton = "True"/>, and then delete it in the OnRowDeleting event of the GridView. However, in general, you need to confirm the deletion before deleting the record to avoid accidental deletion.

Then we can add a confirmation dialog box before deleting the GridView through the following method.

First, click "Columns" in the "properties" box of the GridView to enter its "field" designer; or click the small arrow in the upper-right corner of the GridView control in the design window, click "Edit column" to enter the "field" designer.

In the "field" designer's lower left "selected field" box, select the previously added CommandField "delete" column, at this time, under its attribute list on the right, you will see an item "convert this segment to TemplateFied", and click to convert it to the TemplateFied column.

Then exit the "field" designer and switch to the source code view. You will find that the column has been changed from the original one:
<Asp: CommandField ShowDeleteButton = "True"/>
Changed:
<Asp: TemplateField ShowHeader = "False">
<ItemTemplate>
<Asp: LinkButton ID = "LinkButton1" runat = "server" CausesValidation = "False"

CommandName = "Delete" Text = "Delete"> </asp: LinkButton>
</ItemTemplate>

Add OnClientClick = "return confirm ('Are you sure you want to delete this record? ');"

In this way, "Are you sure you want to delete this record?" is displayed on the client first when you click Delete ?" In the dialog box, click "OK" to delete the instance. Click "cancel" to delete the instance.
The Code originally written in the onRowDeleting event does not need to be changed.

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.