
打开APP
Web中的多行删除和分页 原创
2013-10-22 19:02:40
猿猿-web网页设计
码龄9年
关注
在Web中要实现多行删除的思路:
1、需要在控件前面加上CheckBox控件,选中哪些后点击一个按钮即可实现多行删除的功能。
2、 在按钮的点击事件中写入方法:
a、 我们既要判断我们选择了哪个CheckBox控件,又要保存住选择行的用户名。
3、我们要先遍历整个GridView控件,然后判断哪行的CheckBox控件被选中。
string uersname = ""
for (int i = 0i <this.GridView1.Rows.Counti++)
{
CheckBox ck= this.GridView1.Rows[i].FindControl("CheckBox1") as CheckBox//FindControl方法可以找到CheckBox控件,as是将其转化为CheckBox类
if (ck.Checked==true)//判断是否选择
{
uersname="'"+this.GridView1.Rows[i].Cells[1].Text+"'," //保存被选择行的用户名
}
}
uersname = uersname.Trim(',') //将最后一个逗号删除
4、连接数据库,经行SQL语句,删除语句。用户名的确定用关键字In
通常在WEB页面中不用这个事件!例如要删除某一行时,通常先对数据库 *** 作删除先,再绑定一下GRIDVIEW!这样就达到了删除效果!
或是
形如:
protected void gvMessage_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string userid = gvMessage.DataKeys[e.RowIndex].Value.ToString()
using (SqlConnection conn = DBAccess.getSqlConnection())
{
SqlCommand comm = new SqlCommand("delete SiteMailing where ID='" + userid + "'", conn)
SqlCommand comm2 = new SqlCommand("delete Message where ID='" + userid + "'", conn)
try
{
comm.ExecuteNonQuery()
comm2.ExecuteNonQuery()
Response.Write("<script>alert(' *** 作成功,已删除指定记录!')</script>")
}
catch (Exception ex)
{
Response.Write("<script>alert(' *** 作失败,请重试!')</script>")
ex.ToString()
}
}
gvBind()
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)