Gridview控件改写
当数据源没有数据时候,前台的gridview怎么去展现尼
方式1 直接显示“没有符合条件的记录!”
方式2 利用gridview 模板列EmptyDataTemplate去构造
<EmptyDataTemplate>
<table class="main-table" style="background-color: #f5f5fd; border: 1px;" width="100%"
align="center">
<tr align="center" >
<td>
id</td>
<td>
name</td>
<td>
price</td>
</tr>
</table>
</EmptyDataTemplate>
方式3邦定一空行datatable
方式4 重写gridview控件,
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();
t.CssClass = this.CssClass;
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)
{
TableCell cell = new TableCell();
cell.Text = f.HeaderText;
cell.CssClass = "TdHeaderStyle1";
row.Cells.Add(cell);
}
/* 以下是显示已定义了EmptyDataTemplate
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);
}
}
}
浙公网安备 33010602011771号