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.