您的位置:首页 > 其它

扩展GridView控件(4) - 联动复选框(复选框的全选和取消全选)

2007-01-08 08:54 555 查看
GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。

[索引页]

[源码下载]

扩展GridView控件(4) - 联动复选框(复选框的全选和取消全选)

作者:webabcd

/*正式版的实现 开始*/

介绍

扩展GridView控件:

联动复选框(复选框的全选和取消全选)。选中指定的父复选框,则设置指定的所有子复选框为选中状态;取消选中指定的父复选框,则设置指定的所有子复选框为取消选中状态。如果指定的所有子复选框为均选中状态,则设置指定的父复选框为选中状态;如果指定的所有子复选框至少有一个为取消选中状态,则设置指定的父复选框为取消选中状态

使用方法(设置CascadeCheckboxes集合属性):

ParentCheckboxID - 模板列中 父复选框ID

ChildCheckboxID - 模板列中 子复选框ID

YYControls.Helper.SmartGridView中的静态方法

List<DataKey> GetCheckedDataKey(GridView gv, int columnIndex)

List<DataKey> GetCheckedDataKey(GridView gv, string checkboxId)

关键代码

js

var yy_sgv_ccGridView_pre = new Array(); // cs中动态向其灌数据(GridView内控件ID的前缀数组)

var yy_sgv_ccAll_post = new Array(); // cs中动态向其灌数据(全选复选框ID的后缀数组)

var yy_sgv_ccItem_post = new Array(); // cs中动态向其灌数据(项复选框ID的后缀数组)

function yy_sgv_ccCheck(e)

function yy_sgv_ccCheckAll(ccIndex, isCheckAll)

function yy_sgv_ccCheckItem(ccIndex)

function yy_sgv_ccCheckCheckbox(pre, post, isCheckAll)

function yy_sgv_ccListener()

if (document.all)

else

c#

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI.WebControls;

using System.Web.UI;

namespace YYControls.SmartGridViewFunction

/*正式版的实现 结束*/

/*测试版的实现 开始*/

介绍

平时使用GridView的时候经常要给每行加一个复选框,然后还需要放置一个单独的全选复选框,通过单击它来让这些复选框全选或取消全选,每次实现这样的功能都要写一段javascript,麻烦,所以扩展它。

控件开发

1、新建一个继承自GridView的类。

[ToolboxData(@"<{0}:SmartGridView runat='server'></{0}:SmartGridView>")]

public class SmartGridView : GridView

using System;

using System.Collections.Generic;

using System.Text;

namespace YYControls.SmartGridView

3、新建一个CheckboxAll类,有两个属性

using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using System.Web.UI;

namespace YYControls.SmartGridView

4、新建一个继承自CollectionBase的类CheckboxAlls

using System.Collections;

using System.ComponentModel;

using System.Web.UI;

namespace YYControls.SmartGridView

5、在继承自GridView的类中加一个复杂对象属性,该复杂对象就是第4步创建的那个CheckboxAlls

private CheckboxAlls _checkboxAlls;

[

PersistenceMode(PersistenceMode.InnerProperty),

DesignerSerializationVisibility(DesignerSerializationVisibility.Content),

Description("复选框组集合 一个组由一个 全选复选框 和多个 项复选框组成"),

Category("扩展")

]

public virtual CheckboxAlls CheckboxAlls

6、声明一些内部属性

protected string HiddenCheckboxAllID

protected string HiddenCheckboxItemID

protected char GroupSeparator

protected char ItemSeparator

private string _checkAllIDString;

private string _checkItemIDString;

private Dictionary<int, string> _checkItemIDDictionary = new Dictionary<int, string>();

8、重写OnRowDataBound以给我们声明的那些私有变量赋值。

protected override void OnRowDataBound(GridViewRowEventArgs e)

9、重写GridView的OnPreRender方法,以实现每行复选框的全选与取消全选的功能。

protected override void OnPreRender(EventArgs e)

控件使用

添加这个控件到工具箱里,然后拖拽到webform上,在模板列的头模板处添加一个复选框,在模板列的项模板处添加一个复选框,设置控件的CheckboxAlls属性即可。CheckboxAllID是模板列全选复选框ID;CheckboxItemID是模板列项复选框ID。

ObjData.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.ComponentModel;

public class OjbData

Default.aspx

<!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>SmartGridView测试</title>

</head>

<body>

<form id="form1" runat="server">

<div>

 

<yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False"

DataSourceID="ObjectDataSource1" Width="100%">

<Columns>

<asp:TemplateField>

<headertemplate>

<asp:checkbox id="checkall" runat="server" />

</headertemplate>

<itemtemplate>

<asp:checkbox id="checkitem" runat="server" />

</itemtemplate>

</asp:TemplateField>

<asp:TemplateField>

<itemtemplate>

abc

</itemtemplate>

</asp:TemplateField>

<asp:TemplateField>

<headertemplate>

<asp:checkbox id="checkall2" runat="server" />

</headertemplate>

<itemtemplate>

<asp:checkbox id="checkitem2" runat="server" />

</itemtemplate>

</asp:TemplateField>

</Columns>

<CheckboxAlls>

<yyc:CheckboxAll CheckboxAllID="checkall" CheckboxItemID="checkitem" />

<yyc:CheckboxAll CheckboxAllID="checkall2" CheckboxItemID="checkitem2" />

</CheckboxAlls>

<SortTip SortAscImage="~/Images/asc.gif" SortDescImage="~/Images/desc.gif" />

</yyc:SmartGridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select"

TypeName="OjbData"></asp:ObjectDataSource>

</div>

</form>

</body>

</html>

/*测试版的实现 结束*/

OK

[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐