GridView中实现CheckBox的全选

转:http://www.cnblogs.com/oec2003/archive/2007/11/09/954798.html  不过他有错误。。。

GridView中实现CheckBox的全选
用服务器端的方法: 
在页面上放一个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=box.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();
         }
    }
 }
GridView中实现CheckBox的全选

我自己的。。

GridView中实现CheckBox的全选
 <Columns>
                                <asp:TemplateField HeaderText="选择">
                                <HeaderTemplate>
                    <input id="chkAll"  onclick="SelectAll(this)"; type="checkbox">
                </HeaderTemplate>
              
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    <ItemTemplate>
                                        <asp:CheckBox ID="CheckBox1" runat="server" />
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" Width="5%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                </asp:TemplateField>
                                  <asp:BoundField DataField="ICinfoguid" HeaderText="ic卡主键">
                                    <HeaderStyle CssClass="disbtn" />
                                    <ItemStyle CssClass="disbtn" />
                                </asp:BoundField>
                                  <asp:BoundField DataField="iccardkey" HeaderText="IC卡卡号">
                                    <HeaderStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                <asp:BoundField DataField="icownnumb" HeaderText="IC卡自编号">
                                    <HeaderStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                <asp:BoundField DataField="keeper" HeaderText="保管人">
                                    <HeaderStyle HorizontalAlign="Center" Width="12%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="12%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                  <asp:BoundField DataField="belongorgname" HeaderText="所属单位">
                                    <HeaderStyle HorizontalAlign="Center" Width="15%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="15%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                  <asp:BoundField DataField="platenum" HeaderText="所属车辆">
                                    <HeaderStyle HorizontalAlign="Center" Width="15%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="15%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                <asp:BoundField DataField="oilname" HeaderText="油品名称">
                                    <HeaderStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                  <asp:TemplateField HeaderText="发卡状态">
                                    <ItemTemplate >
                                    <%# getChinaStatues(Eval("iccardkey").ToString())%>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="8%" VerticalAlign="Middle" />
                                </asp:TemplateField>
                                <asp:BoundField DataField="createtime" HeaderText="创建日期">
                                    <HeaderStyle HorizontalAlign="Center" Width="11%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="11%" VerticalAlign="Middle" />
                                </asp:BoundField>
                                <asp:TemplateField HeaderText="下发机器">
                                    <ItemTemplate>
                                       <asp:LinkButton runat="server" ID="lkbtnloadmachin" CommandName="comn_search" Text="查看"></asp:LinkButton>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" Width="11%" VerticalAlign="Middle" />
                                    <ItemStyle HorizontalAlign="Center" Width="11%" VerticalAlign="Middle" />
                                </asp:TemplateField>
GridView中实现CheckBox的全选

js:

GridView中实现CheckBox的全选
 function SelectAll(chkbox) {
             var box = chkbox;
             state = box.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();
                     }
                 }
         }
GridView中实现CheckBox的全选

GridView中实现CheckBox的全选

上一篇:Fragstats景观分析研究


下一篇:VTL说明文档