首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 自訂伺服器控件:擴充 GridView 支援空資料顯示標題列

     編輯:關於ASP.NET

控件設計目標

繼承 GridView,新增 EnableEmptyContentRender 與 EmptyDataCellCssClass 屬性。當資料來源為空且啟用此功能時,仍渲染表頭與自訂空資料訊息,而非預設的 EmptyDataRow。

核心實作

protected virtual void RenderEmptyContent(HtmlTextWriter writer) {
    Table tblHeader = new Table();
    // 複製 GridView 樣式屬性...
    TableRow rowHeader = new TableRow();
    foreach (DataControlField f in this.Columns) {
        TableCell cell = new TableCell { Text = f.HeaderText };
        rowHeader.Cells.Add(cell);
    }
    tblHeader.Rows.Add(rowHeader);
    TableRow rowBody = new TableRow();
    TableCell cellBody = new TableCell { ColumnSpan = this.Columns.Count };
    if (this.EmptyDataTemplate != null) this.EmptyDataTemplate.InstantiateIn(cellBody);
    else cellBody.Text = this.EmptyDataText;
    rowBody.Cells.Add(cellBody);
    tblHeader.Rows.Add(rowBody);
    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);
}

此控件可直接拖放至工具箱使用,完整保留 GridView 原有功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved