ASPxGridView 结合CheckBox实现多选(在后台事件代码控制)
本篇介绍: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事件。
下面是详细代码:
后台代码:
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;
publicpartialclass 结合checkbox_Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid 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;
}
}
}
}
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;
publicpartialclass 结合checkbox_Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid 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;
}
}
}
}
前台代码:
<%@ 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>
<%@ 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