如何用gridview全选并删除所有选中项
如何用gridview全选并删除所有选中项
使用c#
不用js可以实现吗?
------解决方案--------------------模板列绑定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的id是自己的嵌套命名的,送楼主一套自己使用的js方法
/**************************************************************
* 全部选中GridView中的CheckBox列(指定checkBox的ID)
*
* @param gridViewName GridView控件ID:
* @param checkBoxName 模版列中的CheckBox控件ID:
*
**************************************************************/
function CheckAllWithcheckBoxName( gridViewName, checkBoxName )
{
//查找gridview的模式匹配字符串
var RegString = new RegExp( "(.*?) " + gridViewName + "(.*?) ");
for(i = 0; i < document.all.length; i = i + 1)
{
if (RegString.test(document.all[i].id))
{
//获取查找到的gridView对象
var grid = document.getElementById(document.all[i].id);
//循环执行选中操作
for(i = 0; i < grid.all.length; i = i + 1)
{
var RegString = new RegExp( "(.*?) " + gridViewName + "(.*?) " + checkBoxName);