程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.net 自定義服務器控件之 GridViewControl

ASP.net 自定義服務器控件之 GridViewControl

編輯:.NET實例教程


根據項目需要,提供自定義服務器控件之 GridVIEwControl,提供數據為空時的自定義顯示。

[DefaultProperty("EnableEmptyContentRender")]
[ToolboxData("<{0}:GridViewControl runat=server></{0}:GridVIEwControl>")]
public class GridViewControl : System.Web.UI.WebControls.GridVIEw
{
    /// <summary>
    /// 是否數據為空時顯示標題行
    /// </summary>
    private bool _enableEmptyContentRender = true;
    /// <summary>
    /// 是否數據為空時顯示標題行
    /// </summary>
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public bool EnableEmptyContentRender
    {
        set { _enableEmptyContentRender = value; }
        get { return _enableEmptyContentRender; }
    }
    /// <summary>
    /// 是否數據為空時顯示標題行
    /// </summary>
    private string _EmptyDataCellCSSClass;
    /// <summary>
    /// 為空時信息單元格樣式類
    /// </summary>
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public string EmptyDataCellCSSClass
    {
        set { _EmptyDataCellCSSClass = value; }
        get { return _EmptyDataCellCSSClass; }
    }
    /// <summary>
    /// 為空時輸出內容
    /// </summary>
    /// <param name="writer"></param>
    protected virtual void RenderEmptyContent(HtmlTextWriter writer)
    {
        //Create Header Table
        Table tblHeader = new Table();
        //Copy All Property
        tblHeader.CssClass = this.CSSClass;
        tblHeader.GridLines = this.GridLines;
        tblHeader.BorderStyle = this.BorderStyle;
        tblHeader.BorderWidth = this.BorderWidth;
        tblHeader.CellPadding = this.CellPadding;
        tblHeader.CellSpacing = this.CellSpacing;
        tblHeader.HorizontalAlign = this.HorizontalAlign;
        tblHeader.Width = this.Width;
        tblHeader.CopyBaseAttributes(this);
        TableRow rowHeader = new TableRow();
        tblHeader.Rows.Add(rowHeader);
        rowHeader.CSSClass = "TableHeader";
        //Generate Table Header
        foreach (DataControlFIEld f in this.Columns)
        {
            TableCell cell = new TableCell();
            cell.Text = f.HeaderText;
            rowHeader.Cells.Add(cell);
        }
        TableRow rowBody = new TableRow();
        tblHeader.Rows.Add(rowBody);
        TableCell cellBody = new TableCell();
        cellBody.CssClass = this._EmptyDataCellCSSClass;
        if (this.EmptyDataTemplate != null)
            //the second row, use the template
            this.EmptyDataTemplate.InstantiateIn(cellBody);
        else
            //the second row, use the EmptyDataText
            cellBody.Text = this.EmptyDataText;
        cellBody.HorizontalAlign = HorizontalAlign.Left;
        cellBody.ColumnSpan = this.Columns.Count;
   rowBody.Cells.Add(cellBody);
        tblHeader.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);
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved