zoukankan      html  css  js  c++  java
  • GridView中实现CheckBox的全选

    转载于:http://www.cnblogs.com/oec2003/archive/2007/11/09/954798.html

    用服务器端的方法:
    在页面上放一个gridview控件,配置好数据源,编辑列,添加一个模版列,再编辑模版,放入一个checkbox控件。代码如下:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="AreaID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="AreaID" HeaderText="AreaID" ReadOnly="True" SortExpression="AreaID" />
                <asp:BoundField DataField="CityID" HeaderText="CityID" SortExpression="CityID" />
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkItem" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    后台cs代码:

    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            ((CheckBox)GridView1.Rows[i].FindControl("chkItem")).Checked = 
                ((CheckBox)this.GridView1.HeaderRow.FindControl("chkAll")).Checked;
        }
    }

    用脚本实现:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="AreaID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="AreaID" HeaderText="AreaID" ReadOnly="True" SortExpression="AreaID" />
                <asp:BoundField DataField="CityID" HeaderText="CityID" SortExpression="CityID" />
                <asp:TemplateField>
                    <HeaderTemplate>
                        <input id="chkAll"  onclick="SelectAll(this)"; type=checkbox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <input id="chkItem" type=checkbox>                
                     </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    在客户端写javascript

    function SelectAll(chkbox)
    {
       var box=chkbox;
       state=theBox.checked;    
       elem=box.form.elements;
       for(i=0;i<elem.length;i++)
       if(elem[i].type=="checkbox" && elem[i].id!=box.id)
        {
             if(elem[i].checked!=state)
             {
                elem[i].click();
             }
        }
     }
  • 相关阅读:
    常见存储过程分页PK赛——简单测试分析常见存储过程分页速度
    简单的ASP.NET无刷新分页
    程序员45个好习惯
    手机应用兼职开发平台,欢迎有识之士参加...
    DotNetRemoting分布式安全部署(整理+原创)
    刚刚整理的截获SQL错误代码弹出提示信息类.
    485modbus通讯协议
    485通信
    ucoss在stm32上的移植
    使用OFFICE组件出问题环境配置
  • 原文地址:https://www.cnblogs.com/Tirisfal/p/4777590.html
Copyright © 2011-2022 走看看