首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>.NET2.0>gridview空数据源时仍显示表头和提示
gridview空数据源时仍显示表头和提示
来源: 发布时间:2007-12-16 发布人: 浏览: 人次   字体: [ ]  
问题:asp.net 2.0 中引入的GridView控件当其数据源为空时(GridView.DataSource=null)不能显示出表头.
  解决:
  方法一:采用其EmptyTemplate来实现,模版中写一个静态的table;
  如果你的表头只是html的文本,没有任何控件。你可以在表头显示出来的时候,拷贝表头部分的html,然后放到EmptyDataTemplate里面。
  缺点: 麻烦,每个GridVIew都需要设置一下.
  方法二: 若数据源为DataTable,则当无数据时,始终返回一个空行的DataTable;
  若数据源是集合类(ArrayList,List<T>等),无数据时,生成一个空的实体,加入到集合类中.
  缺点: 还是麻烦.
  方法三:
  也是要给大家介绍的方法: 扩展GridView来实现.继承GridVie,重写Render方法,当其数据源为空时做一下处理,直接看代码吧:
  
   /// <summary>
   /// GridView 扩展控件  
   /// </summary>
   public class GridView : System.Web.UI.WebControls.GridView
   {
   private bool _enableEmptyContentRender = true ;
   /// <summary>
   /// 是否数据为空时显示标题行
   /// </summary>
   public bool EnableEmptyContentRender
   {
   set { _enableEmptyContentRender = value; }
   get { return _enableEmptyContentRender; }
   }
  
   private string _EmptyDataCellCssClass ;
   /// <summary>
   /// 为空时信息单元格样式类
   /// </summary>
   public string EmptyDataCellCssClass
   {
   set { _EmptyDataCellCssClass = value ; }
   get { return _EmptyDataCellCssClass ; }
   }
  
   /// <summary>
   /// 为空时输出内容
   /// </summary>
   /// <param name="writer"></param>
   protected virtual void RenderEmptyContent(HtmlTextWriter writer)
   {
   Table t = new Table(); //create a table
   t.CssClass = this.CssClass; //copy all property
   t.GridLines = this.GridLines;
   t.BorderStyle = this.BorderStyle;
   t.BorderWidth = this.BorderWidth;
   t.CellPadding = this.CellPadding;
   t.CellSpacing = this.CellSpacing;
  
   t.HorizontalAlign = this.HorizontalAlign;
  
   t.Width = this.Width;
  
   t.CopyBaseAttributes(this);
  
   TableRow row = new TableRow();
   t.Rows.Add(row);
  
   foreach (DataControlField f in this.Columns) //generate table header
   {
   TableCell cell = new TableCell();
  
   cell.Text = f.HeaderText;
  
   cell.CssClass = "TdHeaderStyle1"; //这里把表头样式写死了
  
   row.Cells.Add(cell);
   }
  
   TableRow row2 = new TableRow();
   t.Rows.Add(row2);
  
   TableCell msgCell = new TableCell();
   msgCell.CssClass = this._EmptyDataCellCssClass;
  
   if (this.EmptyDataTemplate != null) //the second row, use the template
   {
   this.EmptyDataTemplate.InstantiateIn(msgCell);
   }
   else //the second row, use the EmptyDataText
   {
   msgCell.Text = this.EmptyDataText;
   }
  
   msgCell.HorizontalAlign = HorizontalAlign.Center;
   msgCell.ColumnSpan = this.Columns.Count;
  
   row2.Cells.Add(msgCell);
  
   t.RenderControl(writer);
   }
  
   protected override void Render(HtmlTextWriter writer)
   {
   if ( _enableEmptyContentRender && ( this.Rows.Count == 0 || this.Rows[0].RowType == DataControlRowType.EmptyDataRow) )
   {
   RenderEmptyContent(writer);
   }
   else
   {
   base.Render(writer);
   }
   }      
   }
  }

相 关 文 章   发布商链接
·asp.net2.0回发或回调参数无效问题的...
·asp.net url 映射技术改变网址
·使用放在App_Code中类的静态成员应该...
·Asp.net页面传递参数的几种方法对比...
·如何通过代码实现 Windows Mobile 窗...
·asp.net中GridView每个单元格鼠标移...
·asp.net下的MD5加密应用实例
·asp.net文件下载防止乱码出现的代码...
·.NET使用ajax最基本代码实例
·ASP.NET 2.0无刷新页面的实现
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·实例讲解在.net2.0中对config
·包含32种常见的用户输入验证...
·asp.net不使用服务器控件的情
·如何更改单个aspx页面的编码...
·实例讲解如何设置gridview的...
·asp.NET 2.0中引发CSS失效问...
·GridView通过模板列实现第一...
·VS2005利用TreeView控件和递...
·DataView对象获取数据行相应...
·GridView中固定列的两种方法
·为GridView控件添加分页导航...
·Net2.0中使用母版页及其母版...
·实例解决GridView中弹出对话...
·Gridview控件字段内容过长的...
·图解ASP.NET下AJAX安装(VS20...
热 门 文 章
·asp.net(C#)弹出窗口返回值并...
·VS2005利用Excel组件操作Exce...
·AJAX实现无刷新适时显示gridV...
·gridview表头固定并实现打印...
·Js获取radiobuttonlist选中值...
·gridview绑定数据显示的各种...
·ASP.NET中“无法加载 DLL 找...
·asp.net2.0回发或回调参数无...
·asp.net文件下载防止乱码出现...
·实现gridview内容打印功能的...
·图解ASP.NET下AJAX安装(VS20...
·ASP.NET多个用户控件回车提交...
·实例解决GridView中弹出对话...
·asp.net中GridView每个单元格...
·Asp.net页面传递参数的几种方...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .