欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 不用GridView自带删除功能,删除一行数据

程序员文章站 2024-03-08 16:16:16
前台代码: 复制代码 代码如下: 前台代码:
复制代码 代码如下:

<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="删除" showheader="false">
<itemstyle horizontalalign="center" width="40px" />
<itemtemplate>
<asp:linkbutton id="linkbutton1" runat="server"
causesvalidation="false" commandname="delete" onclientclick="return confirm('您确认删
除?');" text="删除"></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>
日志库暂时为空!
</emptydatatemplate>
</asp:gridview>

cs代码
复制代码 代码如下:

protected void publicgridrowdeleting(object sender, gridviewdeleteeventargs e)
{
string strid = gridlog.datakeys[e.rowindex].value.tostring();//strid就是该行的id
string strsql = "delete from table " +
" where id = " + strid;
//执行删除
clientscript.registerstartupscript(gettype(), "message", "<script
language='javascript'>alert('删除成功!');</script>");
gridbind();
}

关键是设定好datakeynames后,可以靠 string strid = gridlog.datakeys
[e.rowindex].value.tostring();获得选择列的id值 然后用这个id执行删除就可以了 。