Gridview全选和删除选中行功能

aspx代码
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView.aspx.cs" Inherits="GridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>无标题页</title>
    
    
<%--全选和取消全选区的功能--%>
   
<script language="javascript">

function SelectAllCheckboxes(spanChk){

   
// Added as ASPX uses SPAN for checkbox
   var oItem = spanChk.children;
   
var theBox= (spanChk.type=="checkbox"? 
        spanChk : spanChk.children.item[
0];
   xState
=theBox.checked;
   elm
=theBox.form.elements;

   
for(i=0;i<elm.length;i++)
     
if(elm[i].type=="checkbox" && 
              elm[i].id
!=theBox.id)
     {
       
//elm[i].click();
       if(elm[i].checked!=xState)
         elm[i].click();
       
//elm[i].checked=xState;
     }
}
</script>


</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames
="id" DataSourceID="SqlDataSource1" AllowPaging="True" 
            PageSize
="3">
            
<Columns> 
            
            
<asp:TemplateField HeaderText="全选&lt;input type='checkbox' id='chk' onclick='javascript:SelectAllCheckboxes(chk)'&gt;">
                    
<ItemTemplate>
                        
<input ID="id" type="checkbox"  runat="server" value='<%# Eval("id") %>'/>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly
="True" SortExpression="id" />
                
<asp:BoundField DataField="className" HeaderText="className" 
                    SortExpression
="className" />
               
            
</Columns>
        
</asp:GridView>
        
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        
<br />
        
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString
="<%$ ConnectionStrings:liuqiangConnectionString %>" 
            DeleteCommand
="DELETE FROM [classTree] WHERE [id] = @id" 
            InsertCommand
="INSERT INTO [classTree] ([className], [addTime]) VALUES (@className, @addTime)" 
            SelectCommand
="SELECT [id], [className], [addTime] FROM [classTree]" 
            UpdateCommand
="UPDATE [classTree] SET [className] = @className, [addTime] = @addTime WHERE [id] = @id">
            
<DeleteParameters>
                
<asp:Parameter Name="id" Type="Int32" />
            
</DeleteParameters>
            
<UpdateParameters>
                
<asp:Parameter Name="className" Type="String" />
                
<asp:Parameter DbType="Date" Name="addTime" />
                
<asp:Parameter Name="id" Type="Int32" />
            
</UpdateParameters>
            
<InsertParameters>
                
<asp:Parameter Name="className" Type="String" />
                
<asp:Parameter DbType="Date" Name="addTime" />
            
</InsertParameters>
        
</asp:SqlDataSource>
    
</div>
    
</form>
</body>
</html>

 

.cs代码
代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class GridView : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {

    }
    
protected void Button1_Click(object sender, EventArgs e)
    {
        
        
for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            HtmlInputCheckBox checkBox 
=(HtmlInputCheckBox) GridView1.Rows[i].Cells[0].FindControl("id");
            
if (checkBox!=null&& checkBox.Checked)
            {
                SqlDataSource1.DeleteParameters[
"id"].DefaultValue = checkBox.Value;
                SqlDataSource1.Delete();
            }
        }
    }
}

 

posted @ 2010-08-28 10:01  刘强 cnblogs  阅读(693)  评论(0编辑  收藏  举报