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"下拉菜单选项中。
精彩图集
精彩文章
热门标签
整个单词
修改时间时区
Video
多文件
array_column
去除空格
字符串函数
实现原理
管道 PHP多线程
htaccess
网站程序池
Th
索引器
数组变字符串
最长递增
c语言网络
限制行数
Argument
键值
动态创建dom
Java编程
msxml3.dll
十个
select标签
相互
证书加密
获取sess
hadoop伪分布
linux基础与应用
cp
顶尖算法
memory
极速快跑
访问日志分析
chm手册
mysql删除
刷新父页面
显
判断上传文件
List添加元素
弹出登陆框
产品密钥
Lesson02_03
popupwindow
mechanize库
浏览历史记录
域控制器
常用操作
算法原理
随机查询
二进制
闹钟程序
国内如何google
元素拖动
6
多线程返回值
游戏
简单工厂模式
date()
页面返回值
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

