自定义控件之重写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);
            }
        }    

    }
0 0
原创粉丝点击