asp.net实例操作显示及隐藏GridView列头的方法(2)
C# Code [http://www.xueit.com] private void SetupShowHideColumns(GridView gridView, Literal showHideColumnsLiteral){ StringBuilder sb = new StringBuilder(); sb.Append( " div class=\"showHideColumnsCon
C# Code [http://www.xueit.com]
private void SetupShowHideColumns(GridView gridView, Literal showHideColumnsLiteral) { StringBuilder sb = new StringBuilder(); sb.Append("<div class=\"showHideColumnsContainer\">"); sb.Append("<select id=\""); sb.Append(gridView.ClientID); sb.Append("_showCols\" onchange=\"javascript:ShowCol('"); sb.Append(gridView.ClientID); sb.Append("', this.value);\" style=\"display:none;\">"); sb.Append("<option>- Show Column -</option></select></div>"); showHideColumnsLiteral.Text = sb.ToString(); }
在数据绑定到GridView之后,其余的工作由ShowHideColumns.js中的javascript来完成.当列头的hyperlink被点击的时候后,它将会传递GridView的名字,列的索引和列名给HideCol方法,这个方法能找到这一列的每个单元格,每个单元格的将添加display:none样式,用来隐藏这一列.
当选择"Show Column"中的选项后,Javascript方法ShowCol将会被调用,它将移除每个单元格的display:none样式,这一列将会被再次显示.
在服务端显示/隐藏GridView的列
服务端的例子将通过RowCreated事件给每个列头添加一个负号,这次是使用LinkButton控件.设置CommandName和CommandArgument属性,这样当通过LinkButton引发RowCommand事件时,相关的列都可以隐藏。以前隐藏的列索引存储在一个List<int>中,这些列在建立时,将会被隐藏的。
C# Code [http://www.xueit.com]
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { // For the header row add a link button to each header // cell which can execute a row command if (e.Row.RowType == DataControlRowType.Header) { // Loop through each cell of the header row for (int columnIndex = 0; columnIndex < e.Row.Cells.Count; columnIndex ) { LinkButton hideLink = new LinkButton(); hideLink.CommandName = "hideCol"; hideLink.CommandArgument = columnIndex.ToString(); hideLink.Text = "-"; hideLink.CssClass = "gvHideColLink"; hideLink.Attributes.Add("title", "Hide Column"); // Add the "Hide Column" LinkButton to the header cell e.Row.Cells[columnIndex].Controls.AddAt(0, hideLink); // If there is column header text then // add it back to the header cell as a label if (e.Row.Cells[columnIndex].Text.Length > 0) { Label columnTextLabel = new Label(); columnTextLabel.Text = e.Row.Cells[columnIndex].Text; e.Row.Cells[columnIndex].Controls.Add(columnTextLabel); } } } // Hide the column indexes which have been stored in hiddenColumnIndexes foreach(int columnIndex in hiddenColumnIndexes) if (columnIndex < e.Row.Cells.Count) e.Row.Cells[columnIndex].Visible = false; }
在SetupShowHideColumns 方法中创建"Show Columns"下拉菜单,以前被隐藏的列名被添加到"Show Columns"下拉菜单选项中。
精彩图集
精彩文章
热门标签
Enterprise
入门教程
greenlet
Buffered
服务器角色
Linux换行符
常见漏洞
访问无效
sql数据库连接
类的大小
妙用
lastIndexOf
ZeroClipboar
tsql
阵列
组策略
头尾
ip数据库
Xdebug扩展
Parsing
用户名问
显示不正常
cloud
创建
n天、周
PHP实例教程--
基金会
C程序
映射
连接失败
无法解析
向QQ群成员
托管管道模式
木马攻击
codeigniter使
MYSQL删除数据
授权
Iisweb.vbs
编译运行
批量化
有证书
发邮
swapon
foun
核心参数
out.print
show命令
mysql分区表
Profiling
大小限制
复选框
架构
下载
密匙
pathinfo()
省市区县
java分布式
最新的几条
ReplaceAll
目录文件
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

