手把手教你如何扩展GridView之自带CheckBox
我们在使用GridView的时候,很多时候需要使用CheckBox列,比如批量删除,批量审批,但是每每都需要记住繁琐的实现方法。多麻烦呀!再次给GridView做个手术,让它自己就能产生CheckBox岂不爽死了。以后您就有权利忘记怎么实现CheckBox列了。哈哈,作咱们这行的,就要学的慢慢退化,什么事情都记着,累也累死了。
下面谈谈我这实现的思路:
因为GridView是基于模板的,Columns也不能在后台添加,所以排除通过添加Column来实现,而采用在GridView创建行的时候动态创建表单元格,在表头行上添加一个全选CheckBox,数据行上添加选择框,点击全选,通过向页面注册的脚本来实现全选。
下面就看看关键的代码:
为了增加灵活性,添加了一个属性,控制是否显示CheckBox列
//是否显示全选
[
Description("显示全选列"),
Category("扩展"),
DefaultValue(false)
]
public virtual bool ShowCheckAll
{
get
{
object obj2 = this.ViewState["ShowCheckAll"];
if (obj2 != null)
{
return (bool)obj2;
}
return false;
}
set
{
bool aShowCheckAll = this.ShowCheckAll;
if (value != aShowCheckAll)
{
this.ViewState["ShowCheckAll"] = value;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
}
}
[
Description("显示全选列"),
Category("扩展"),
DefaultValue(false)
]
public virtual bool ShowCheckAll
{
get
{
object obj2 = this.ViewState["ShowCheckAll"];
if (obj2 != null)
{
return (bool)obj2;
}
return false;
}
set
{
bool aShowCheckAll = this.ShowCheckAll;
if (value != aShowCheckAll)
{
this.ViewState["ShowCheckAll"] = value;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
}
}
用于控制选择列是添加到表的左端还是右端的属性
public enum CheckColumnAlign
{
Left, Right
}
//是否显示全选
[
Description("全选列的位置"),
Category("扩展"),
DefaultValue(CheckColumnAlign.Left)
]
public virtual CheckColumnAlign CheckColumnAlign
{
get
{
object obj2 = this.ViewState["CheckColumnAlign"];
if (obj2 != null)
{
return (CheckColumnAlign)obj2;
}
return CheckColumnAlign.Left;
}
set
{
CheckColumnAlign aCheckColumnAlign = this.CheckColumnAlign;
if (value != aCheckColumnAlign)
{
this.ViewState["CheckColumnAlign"] = value;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
}
}
{
Left, Right
}
//是否显示全选
[
Description("全选列的位置"),
Category("扩展"),
DefaultValue(CheckColumnAlign.Left)
]
public virtual CheckColumnAlign CheckColumnAlign
{
get
{
object obj2 = this.ViewState["CheckColumnAlign"];
if (obj2 != null)
{
return (CheckColumnAlign)obj2;
}
return CheckColumnAlign.Left;
}
set
{
CheckColumnAlign aCheckColumnAlign = this.CheckColumnAlign;
if (value != aCheckColumnAlign)
{
this.ViewState["CheckColumnAlign"] = value;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
}
}
[第1页][第2页]