Asp.net does not use the built-in delete function of the GridView to delete a row of data.

Source: Internet
Author: User

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.

Related Article

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.