Front-end
Code :
Copy code The 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 codeCopy 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.