文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASPxGridView利用CheckBox实现全选

ASPxGridView利用CheckBox实现全选

时间:2011-04-05  来源:江宇旋

本篇介绍:ASPxGridView 结合CheckBox实现多选 ,实质跟gridview是一样的:遍历ASPxGridView 找到每行的CheckBox,让后做处理。不过ASPxGridView 在遍历的时候有很多地方和gridview是不同的,对于和我一样对ASPxGridView 不属性的人来说,要花很多的事件去找和适当属性。
    其实ASPxGridView 官方也提供了实现全选的例子不过那个似乎是不能绑定数据库的。

    遍历ASPxGridView 时findcontrol事要注意要用 (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1")来取得某行的CheckBox
还有记得要把head的CheckBox的设置AutoPostBack="True" ,负责当点击CheckBox时相当与head的click。

下面是详细代码:

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;

using DevExpress.Web.ASPxGridView;


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

     }
    protected void selectAll_CheckedChanged(object sender, EventArgs e)
     {
        int startid = ASPxGridView1.PageIndex * ASPxGridView1.SettingsPager.PageSize;
        int endid;

        if (ASPxGridView1.PageIndex == ASPxGridView1.PageCount - 1)
         {
             endid = ASPxGridView1.VisibleRowCount;
         }
        else
         {
             endid = (ASPxGridView1.PageIndex + 1) * ASPxGridView1.SettingsPager.PageSize;
         }

         CheckBox cbHead = (CheckBox)sender;
        if (cbHead.Checked == true)
         {
             CheckBox cb;
            for (int i = startid; i < endid; i++)
             {
                 cb = (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1");
                 cb.Checked = true;
             }
         }
        else
         {
             CheckBox cb;
            for (int i = startid; i < endid; i++)
             {
                 cb = (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["SelectAll"], "CheckBox1");
                 cb.Checked = false;
             }
         }
     }
}

 

aspx文件

 

代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="结合checkbox_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxGridView.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" 
             AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
             KeyFieldName="ProductID">
            <Columns>
                <dxwgv:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" 
                     VisibleIndex="0">
                    <EditFormSettings Visible="False" />
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataCheckColumn FieldName="Discontinued" VisibleIndex="2">
                </dxwgv:GridViewDataCheckColumn>
                <dxwgv:GridViewDataTextColumn FieldName="SelectAll" VisibleIndex="3">
                    <DataItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" 
                             Checked='<%# Eval("Discontinued") %>' />
                    </DataItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="selectAll" runat="server" AutoPostBack="True" 
                             oncheckedchanged="selectAll_CheckedChanged" />
                         SelectAll
                    </HeaderTemplate>
                </dxwgv:GridViewDataTextColumn>
            </Columns>
        </dxwgv:ASPxGridView>
    
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
         SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products] ORDER BY [ProductID]">
    </asp:SqlDataSource>
    </form>
</body>
</html>

 

 

数据库有的是Nothwind表是Products

排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载