linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > asp.net >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·NetBPM工作流的一个示例:请假
·Office Web Components(OWC)绘
·asp.net正则表达式语法
·asp.net 2.0 ajax中使用PopupC
·Ado.Net读取Excel常见问题总结
·数据源为空时如何让GridView显
·如何让UpdatePanel支持文件上传
·C#.Net的常见面试试题和参考答
·asp.net ajax客户端编程+jquer
·Brettle.Web.NeatUpload.dll支
·ASP.NET使用Cookie
·ASP.NET DEMO 15: 同时支持行单
·如何使IE的后退按钮无效
·如何在ASP.NET中用OWC绘制图表
·asp.net:正确判断当前用户角色
最新文章
·Ajax Control Toolkit Animati
·讨论一下类似BlogEngine内一次
·使用CSS+SiteMap+UserControl+
·Asp.net中多彩下拉框的实现
·浅谈ASP.NET的Postback
·分清ASP.NET AJAX中的Extender
·Tip:在使用AjaxControlTookit
·有关注册DataItem的一些可能被
·IIRF(Ionic's Isapi Rewrite
·asp.net 客户端回调功能的实现
·关于控件部分的看法--读Progra
·为什么在vista上做开发
·如何封装JS和CSS文件为服务器端
·岂今我看过的最强的排序算法
·设计模式学习笔记之单件模式
Google
 
数据源为空时如何让GridView显示表头和提示
[ 作者:  加入时间:2007-10-22 12:46:52  来自:Linux联盟收集整理 ]
问题:asp.net 2.0 中引入的GridView控件当其数据源为空时(GridView.DataSource=null)不能显示出表头. JVfLinux联盟
  解决: JVfLinux联盟
  方法一:采用其EmptyTemplate来实现,模版中写一个静态的table; JVfLinux联盟
  如果你的表头只是html的文本,没有任何控件。你可以在表头显示出来的时候,拷贝表头部分的html,然后放到EmptyDataTemplate里面。 JVfLinux联盟
  缺点: 麻烦,每个GridVIew都需要设置一下. JVfLinux联盟
  方法二: 若数据源为DataTable,则当无数据时,始终返回一个空行的DataTable; JVfLinux联盟
  若数据源是集合类(ArrayList,List<T>等),无数据时,生成一个空的实体,加入到集合类中. JVfLinux联盟
  缺点: 还是麻烦. JVfLinux联盟
  方法三: JVfLinux联盟
  也是要给大家介绍的方法: 扩展GridView来实现.继承GridVie,重写Render方法,当其数据源为空时做一下处理,直接看代码吧: JVfLinux联盟
   JVfLinux联盟
   /// <summary> JVfLinux联盟
   /// GridView 扩展控件 JVfLinux联盟
   /// @author:jianyi0115@163.com JVfLinux联盟
   /// </summary> JVfLinux联盟
   public class GridView : System.Web.UI.WebControls.GridView JVfLinux联盟
   { JVfLinux联盟
   private bool _enableEmptyContentRender = true ; JVfLinux联盟
   /// <summary> JVfLinux联盟
   /// 是否数据为空时显示标题行 JVfLinux联盟
   /// </summary> JVfLinux联盟
   public bool EnableEmptyContentRender JVfLinux联盟
   { JVfLinux联盟
   set { _enableEmptyContentRender = value; } JVfLinux联盟
   get { return _enableEmptyContentRender; } JVfLinux联盟
   } JVfLinux联盟
   JVfLinux联盟
   private string _EmptyDataCellCssClass ; JVfLinux联盟
   /// <summary> JVfLinux联盟
   /// 为空时信息单元格样式类 JVfLinux联盟
   /// </summary> JVfLinux联盟
   public string EmptyDataCellCssClass JVfLinux联盟
   { JVfLinux联盟
   set { _EmptyDataCellCssClass = value ; } JVfLinux联盟
   get { return _EmptyDataCellCssClass ; } JVfLinux联盟
   } JVfLinux联盟
   JVfLinux联盟
   /// <summary> JVfLinux联盟
   /// 为空时输出内容 JVfLinux联盟
   /// </summary> JVfLinux联盟
   /// <param name="writer"></param> JVfLinux联盟
   protected virtual void RenderEmptyContent(HtmlTextWriter writer) JVfLinux联盟
   { JVfLinux联盟
   Table t = new Table(); //create a table JVfLinux联盟
   t.CssClass = this.CssClass; //copy all property JVfLinux联盟
   t.GridLines = this.GridLines; JVfLinux联盟
   t.BorderStyle = this.BorderStyle; JVfLinux联盟
   t.BorderWidth = this.BorderWidth; JVfLinux联盟
   t.CellPadding = this.CellPadding; JVfLinux联盟
   t.CellSpacing = this.CellSpacing; JVfLinux联盟
   JVfLinux联盟
   t.HorizontalAlign = this.HorizontalAlign; JVfLinux联盟
   JVfLinux联盟
   t.Width = this.Width; JVfLinux联盟
   JVfLinux联盟
   t.CopyBaseAttributes(this); JVfLinux联盟
   JVfLinux联盟
   TableRow row = new TableRow(); JVfLinux联盟
   t.Rows.Add(row); JVfLinux联盟
   JVfLinux联盟
   foreach (DataControlField f in this.Columns) //generate table header JVfLinux联盟
   { JVfLinux联盟
   TableCell cell = new TableCell(); JVfLinux联盟
   JVfLinux联盟
   cell.Text = f.HeaderText; JVfLinux联盟
   JVfLinux联盟
   cell.CssClass = "TdHeaderStyle1"; //这里把表头样式写死了 JVfLinux联盟
   JVfLinux联盟
   row.Cells.Add(cell); JVfLinux联盟
   } JVfLinux联盟
   JVfLinux联盟
   TableRow row2 = new TableRow(); JVfLinux联盟
   t.Rows.Add(row2); JVfLinux联盟
   JVfLinux联盟
   TableCell msgCell = new TableCell(); JVfLinux联盟
   msgCell.CssClass = this._EmptyDataCellCssClass; JVfLinux联盟
   JVfLinux联盟
   if (this.EmptyDataTemplate != null) //the second row, use the template JVfLinux联盟
   { JVfLinux联盟
   this.EmptyDataTemplate.InstantiateIn(msgCell); JVfLinux联盟
   } JVfLinux联盟
   else //the second row, use the EmptyDataText JVfLinux联盟
   { JVfLinux联盟
   msgCell.Text = this.EmptyDataText; JVfLinux联盟
   } JVfLinux联盟
   JVfLinux联盟
   msgCell.HorizontalAlign = HorizontalAlign.Center; JVfLinux联盟
   msgCell.ColumnSpan = this.Columns.Count; JVfLinux联盟
   JVfLinux联盟
   row2.Cells.Add(msgCell); JVfLinux联盟
   JVfLinux联盟
   t.RenderControl(writer); JVfLinux联盟
   } JVfLinux联盟
   JVfLinux联盟
   protected override void Render(HtmlTextWriter writer) JVfLinux联盟
   { JVfLinux联盟
   if ( _enableEmptyContentRender && ( this.Rows.Count == 0 || this.Rows[0].RowType == DataControlRowType.EmptyDataRow) ) JVfLinux联盟
   { JVfLinux联盟
   RenderEmptyContent(writer); JVfLinux联盟
   } JVfLinux联盟
   else JVfLinux联盟
   { JVfLinux联盟
   base.Render(writer); JVfLinux联盟
   } JVfLinux联盟
   } JVfLinux联盟
   JVfLinux联盟
   } JVfLinux联盟
  } Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·从 DataGridView 控件 托放数据 到 TreeView控件  (2007-10-29 14:21:22)
 ·手把手教你如何扩展GridView之个性分页  (2007-10-26 13:08:40)
 ·将datagrid的数据源到出导excel  (2007-10-26 12:54:44)
 ·从GridView生成DataTable  (2007-10-22 14:34:52)
 ·ASP.NET DEMO 15: 同时支持行单击和双击事件的 GridView/DataGr  (2007-10-22 11:39:07)
 ·jsp连接sql server调用数据源方法  (2007-10-19 13:58:05)
 ·用Oracle的异构服务连接异种数据源  (2007-02-12 11:56:48)