DataList和Gridview实现全选

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="meno.aspx.cs" Inherits="gridviewselectAll.meno" %>

<!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 type="text/javascript">
        function chage(sender) {
       
            var table = document.getElementById("GridView1");
            for (var i = 1; i < table.rows.length; i++) {

               table.rows[i].cells[0].getElementsByTagName("input")[0].checked = sender.checked;
//获取当前数据行中的第几列是checked然后让它等于上面checked事件
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
            DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
            InsertCommand="INSERT INTO [student] ([sname], [sex], [age]) VALUES (@sname, @sex, @age)"
            SelectCommand="SELECT [sid], [sname], [sex], [age] FROM [student]"
            UpdateCommand="UPDATE [student] SET [sname] = @sname, [sex] = @sex, [age] = @age WHERE [sid] = @sid">
            <DeleteParameters>
                <asp:Parameter Name="sid" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="sname" Type="String" />
                <asp:Parameter Name="sex" Type="String" />
                <asp:Parameter Name="age" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="sname" Type="String" />
                <asp:Parameter Name="sex" Type="String" />
                <asp:Parameter Name="age" Type="Int32" />
                <asp:Parameter Name="sid" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="sid" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
                            oncheckedchanged="CheckBox2_CheckedChanged1" />
                        <br />
                        <input id="Checkbox3" type="checkbox" οnclick="chage(this)"/>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
                    ReadOnly="True" SortExpression="sid" />
                <asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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


        protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
        {
            CheckBox ckb = sender as CheckBox;
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    (row.Cells[4].FindControl("CheckBox1") as CheckBox).Checked = ckb.Checked;
                }

            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值