自定义控件之重写GridView
来源:互联网 发布:java通过ftp上传文件 编辑:程序博客网 时间:2024/05/16 15:51
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);
string mesg = "数据源的记录条数为:" + this.Rows.Count;
writer.Write(mesg);
writer.Write("<script>alert('对不起,没有检索到数据!')</script>");
}
else
{
base.Render(writer);
}
}
}
{
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);
string mesg = "数据源的记录条数为:" + this.Rows.Count;
writer.Write(mesg);
writer.Write("<script>alert('对不起,没有检索到数据!')</script>");
}
else
{
base.Render(writer);
}
}
}
0 0
- 自定义控件之重写GridView
- 自定义控件之重写GridView
- Android控件之GridView自定义
- winform自定义控件之ComboBox简单重写
- Android-自定义控件之重写控件(自定义TextView)
- Gridview全解之自定义删除控件
- 重写GRIDVIEW 控件 asp.net
- Android-自定义View之重写控件(自定义Dialog)
- Android-自定义View之重写控件(自定义Dialog)
- 自定义控件重写TagName
- 自定义控件重写TagName
- 自定义控件之GridView(一)创建自定义的GridView控件
- GridView控件自定义分页
- [GridView控件]自定义分页
- GridView控件自定义分页
- 自定义GridView控件
- 自定义GridView控件开发
- 自定义GridView 分页控件
- Android 动画分析之属性动画
- PHP学习笔记——运算符的优先级和表达式;
- Oracle+SQL优化第二弹
- 页面优化的方法
- 类class
- 自定义控件之重写GridView
- 项目引入bottom-navigation-bar values-v24 找不到资源问题
- 闭包讲解
- SLua踩坑笔记:import "UnityEngine"
- 什么时候定义静态成员呢?
- php安全编程&python测试实例编写
- 抽象工厂模式(对象创建)
- Tomcat集群配置学习篇-----分布式应用
- 函数适配器2——成员函数适配器