All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP函数学习之PHP函数点评_php资料_编程技术
详解网页制作中使用的HTML常用标记_[Html教程]
visual c++中标签控件(Tab Control)
ASP.NET,2.0,中的,Windows,身份验证_[Asp.Net教程]
C#,3.0新特性初步研究,Part1:使用隐含类型的本地变量_[Asp.Net教程]
left join的总结_[SQL Server教程]
.net,2.0中新增的nullable类型_[Asp.Net教程]
ASP.NET中用哪种方式表格化数据_[Asp.Net教程]
不使用VS进行ASP.NET,Membership管理_[Asp.Net教程]
Asp.net,弹出对话框基类,(输出alet警告框)_[Asp.Net教程]
Reading number is top 10 pictures
中国处女图鉴2
玩手机对身体不好
Go to the national museum3
原子弹轰炸长崎的珍贵照片
初五接财神啦!五路财神齐来到
上传几张色图
A man's favorite things2
Play for Free show breast in a world of ice and snow
Sanya, hainan Haitian party feast promiscuity
The money of more than 100 countries and regions19
Download software ranking
Rio big adventure
Unix video tutorial20
dreamweaver8中文版
Unix video tutorial11
致我们终将逝去的青春
天龙八部十二宫服务端
C#程序员参考手册
Wild things 2
Unix video tutorial3
功夫熊猫2(下集)
delv published in(发表于) 2014/1/23 3:11:30 Edit(编辑)
ASP.NET实例:手把手教你如何扩展GridView之自带CheckBox_[Asp.Net教程]

ASP.NET实例:手把手教你如何扩展GridView之自带CheckBox_[Asp.Net教程]

ASP.NET实例:手把手教你如何扩展GridView之自带CheckBox_[Asp.Net教程]























我们在使用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;
}
}
}
}
用于控制选择列是添加到表的左端还是右端的属性




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;
}
}
}
}
在页面加载的时候,注册全选脚本




StringBuilder sb = new StringBuilder();
sb.Append(" ");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CheckFun", sb.ToString(), false);
在GridView的RowCreate事件中,添加如下代码,用于创建CheckBox列





if (ShowCheckAll)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.Header)
{
TableCell cell = new TableCell();
cell.Wrap = Wrap;
cell.Width = Unit.Pixel(50);
cell.Text = " onclick='CheckAll(this)'/>";
if (CheckColumnAlign == CheckColumnAlign.Left)
{
row.Cells.AddAt(0, cell);
}
else
{
int index = row.Cells.Count;
row.Cells.AddAt(index, cell);
}
}
else if (row.RowType == DataControlRowType.DataRow)
{
TableCell cell = new TableCell();
cell.Wrap = Wrap;
CheckBox cb = new CheckBox();
cell.Width = Unit.Pixel(50);
cb.ID = "ItemCheckBox";
cell.Controls.Add(cb);
if (CheckColumnAlign == CheckColumnAlign.Left)
{
row.Cells.AddAt(0, cell);
}
else
{
int index = row.Cells.Count;
row.Cells.AddAt(index, cell);
}
}
}
用于记录CheckBox的ID的属性





public string CheckBoxID
{
get
{
return "ItemCheckBox";
}
}
使用的时候,只需要设置扩展GridView的ShowCheckAll=True,设置CheckColumnAlign为Left,CheckBox列在最左边,Right在最右面,注意因为没有添加Columns,所以Columns并没有因为因为添加了CheckBox列而变化,在Column的索引中,CheckBox列不在计算范围。
在扩展的GridView的OnRowDataBound事件中,就可以通过
CheckBox cb = e.Row.FindControl(gridView.CheckBoxID) as CheckBox;
if(cb!=null)
{
if(cb.Checked)
{
//...
}
}
来获取是否已经选中此行。





来源:jillzhang的blogs













































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.