asp.net without the GridView delete function, delete a row of data _ practical Tips

Source: Internet
Author: User
Foreground code:
Copy Code code 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 (' You confirm delete
Except? '); 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"
/>
<alternatingrowstyle backcolor= "White"/>
<editrowstyle backcolor= "#2461BF"/>
<EmptyDataTemplate>
The log library is temporarily empty!
</EmptyDataTemplate>
</asp:GridView>

CS Code
Copy Code code as follows:

protected void Publicgridrowdeleting (object sender, Gridviewdeleteeventargs e)
{
String Strid = Gridlog.datakeys[e.rowindex]. Value.tostring ()//strid is the ID of the line
String strSQL = "Delete from table" +
"WHERE id =" + strid;
Perform deletes
Clientscript.registerstartupscript (GetType (), "message", "<script
language= ' JavaScript ' >alert (' delete successful! '); </script> ");
Gridbind ();
}

The key is to set a good datakeynames, you can rely on string Strid = Gridlog.datakeys
[E.rowindex]. Value.tostring (); Get the ID value of the selected column and then use this ID to perform the deletion.
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.