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

ASP.Net使用GridView模板删除一行的用法

程序员文章站 2023-02-28 15:27:48
添加gridview(为其绑定数据源后)->(在设计界面)进行编辑- > 添加模板 ->设置该列模板的属性->返回 ->编辑模板->添加一个linkbutton-...

添加gridview(为其绑定数据源后)->(在设计界面)进行编辑-

> 添加模板 ->设置该列模板的属性->返回

->编辑模板->添加一个linkbutton->设置text为删除

>设置commandname属性(如 commandname="delete" )

->设置commandargument的值{如 commandargument='<%# bind("stuno")}

>接着添加gridview的rowcommand事件

具体代码:

主要代码:

<:templatefield showheader="false">
<itemtemplate>
<asp:linkbutton id="linkbutton1" runat="server" causesvalidation="false"
commandname="delete" text="删除" onclick= "return confirm('是否要删除数据?')" commandargument='<%# bind("stuno") %>' ></asp:linkbutton>
</itemtemplate>
</asp:templatefield>

后台主要代码:

protected void gridview1_rowcommand(object sender, gridviewcommandeventargs e)
{
if (e.commandname == "delete")//如果是删除按钮
{
string stuno = e.commandargument.tostring();//获取学生编号

int result = stubll.delstudent(stuno);//使用三层架构实现,调用业务逻辑层的方法
if (result > 0)

{

response.write("删除成功!");

}
else
{
response.write("删除失败!");
}

gvbind();// 删除之后,要重新绑定数据
}
}

 


作者 瑞英