Front-end code:
Copy codeThe Code is as follows:
<Asp: GridView ID = "GridLog" runat = "server"
AutoGenerateColumns = "False" CellPadding = "4" DataKeyNames = "ID"
BorderColor = "#333" BorderStyle = "solid" BorderWidth = "1"
OnRowDeleting = "PublicGridRowDeleting"
GridLines = "None" Width = "98%" ForeColor = "#333333">
<FooterStyle BackColor = "#507CD1" ForeColor = "White" Font-Bold = "True"/>
<Columns>
<Asp: BoundField DataField = "ID" HeaderText = "ID" InsertVisible = "false"
ReadOnly = "True"
SortExpression = "ID">
<ItemStyle HorizontalAlign = "Center" Width = "20px"/>
</Asp: BoundField>
<Asp: TemplateField HeaderText = "delete" ShowHeader = "False">
<ItemStyle HorizontalAlign = "Center" Width = "40px"/>
<ItemTemplate>
<Asp: LinkButton ID = "LinkButton1" runat = "server"
CausesValidation = "False" CommandName = "Delete" OnClientClick = "return confirm ('Are you sure you want to Delete
Division? '); "Text =" delete "> </asp: LinkButton>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
<RowStyle BackColor = "# EFF3FB"/>
<SelectedRowStyle BackColor = "# D1DDF1" Font-Bold = "True" ForeColor = "#333333"
/>
<PagerStyle BackColor = "# 2461BF" ForeColor = "White" HorizontalAlign = "Center"
/>
<HeaderStyle BackColor = "# 5A799C" ForeColor = "White" Height = "22px"/>
<AlternatingRowStyle BackColor = "White"/>
<EditRowStyle BackColor = "# 2461BF"/>
<EmptyDataTemplate>
The logstore is empty!
</EmptyDataTemplate>
</Asp: GridView>
CS code
Copy codeThe Code is as follows:
Protected void PublicGridRowDeleting (object sender, GridViewDeleteEventArgs e)
{
String strID = GridLog. DataKeys [e. RowIndex]. Value. ToString (); // strID indicates the ID of the row.
String strSQL = "Delete from table" +
"WHERE id =" + strID;
// Execute Delete
ClientScript. RegisterStartupScript (GetType (), "Message", "<SCRIPT
LANGUAGE = 'javascript '> alert ('deleted successfully! '); </Script> ");
GridBind ();
}
The key is that after DataKeyNames is set, you can rely on string strID = GridLog. DataKeys
[E. RowIndex]. Value. ToString (); obtain the ID Value of the selected column and delete it with this ID.