日期:2014-05-19  浏览次数:20426 次

GridView里如何一次删除多行?
像以前用asp的时候,每一行后面可以加一个复选框,页面下面再加一个删除按钮,可以把选中的记录都删除。GridView里怎么做呢?

------解决方案--------------------
模板列绑定checkbox:

<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type= "checkbox " id= "selectall " onclick= "select_all() ">
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID= "chkSelect " runat= "server " />
</ItemTemplate>
</asp:TemplateField>
........
</Columns>


删除按钮:
<asp:Button ID= "Button2 " CssClass= "button1 " runat= "server " Text= "删 除 " />


全选JS:
function select_all(){
var a=document.getElementsByTagName( "input ");
for(var j=0;j <a.length;j++)
{
if(a[j].type== "checkbox ")
a[j].checked=document.all.selectall.checked;
}
}


后台删除操作VB:
Sub button2_click(ByVal s As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rowindex As Int16
Dim suc As Boolean = False
Dim conn As New OleDbConnection
conn.ConnectionString = StrCon
conn.Open()
Dim strcomm As New OleDbCommand
strcomm.Connection = conn
For rowindex = 0 To GridView1.Rows.Count - 1
If CType(GridView1.Rows(rowindex).FindControl( "chkSelect "), CheckBox).Checked = True Then
strsql = "delete from shouzhi where id= " & GridView1.DataKeys.Item(rowindex).Value & " "
strcomm.CommandText = strsql
strcomm.ExecuteNonQuery()
suc = True
End If
Next
conn.Close()
strsql = ViewState( "strsql ")
BindList()
If suc = True Then
Label3.Text = "删除成功 "
Label3.ForeColor = Drawing.Color.Red
Else
Label3.Text = "未选择任何项目 "
Label3.ForeColor = Drawing.Color.Red
End If
End Sub
------解决方案--------------------
板列绑定checkbox:

<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label id= "lab_DeleteItem " runat= "server "> </HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID= "chkSelect " runat= "server " />
</ItemTemplate>
</asp:TemplateField>
........
</Columns>


删除按钮:
<asp:Button ID= "Button2 " CssClass= "button1 " runat= "server " Text= "删 除 " />
//后台代码
在Bind GridView里写

lab_DeleteItem.Text= " <input type=checkbox name=Chk_DeleteItem id= Chk_DeleteItem value= ' "+数据库唯一的ID值+ " '> "
//点删除后写如下代码
string _DeleteIDArrayString=Request[ "Chk_DeleteItem "]
以下操作和原来asp是一样的了,因为_DeleteIDArrayString读出来的是以 ", "分割的所有的数据库唯一的ID值