Always show FooterTemplate, even no data

后端 未结 3 1510
太阳男子
太阳男子 2020-12-16 22:24

Is there a short way to make a FooterTemplate (in a GridView) always visible, even when DataSource is empty?

3条回答
  •  情书的邮戳
    2020-12-16 22:41

    As one of the previous commenters mentioned, the RowDataBound event doesn't fire for the footer. I found another code snippet that addresses this issue, but in addition to displaying the footer, it explicitly creates the row (firing the RowCreated event) and binds it (firing the RowDataBound event).

    I've converted the above referenced code to c# using a code converter and made a few minor tweaks. I also included the comments I made as I stepped through the code to break it down. The RowCreated and RowDataBound events are firing now and I'm able to populate dropdowns in footers.

        using System.Linq;
        using System.Web.UI.WebControls;
        using System.ComponentModel;
    
        namespace WebUI.Controls
        {
            //modified from https://stackoverflow.com/questions/3437581/show-gridview-footer-on-empty-grid
            public class GridViewExtended : GridView
            {
    
                private GridViewRow _footerRow;
                [DefaultValue(false), Category("Appearance"), Description("Include the footer when the table is empty")]
                public bool ShowFooterWhenEmpty { get; set; }
    
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
                public override GridViewRow FooterRow {
                    get {
                        if ((this._footerRow == null)) {
                            this.EnsureChildControls();
                        }
                        return this._footerRow;
                    }
                }
    
                protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
                {
                    //creates all the rows that would normally be created when instantiating the grid
                    int returnVal = base.CreateChildControls(dataSource, dataBinding);
                    //if no rows were created (i.e. returnVal == 0), and we need to show the footer row, then we need to create and bind the footer row.
                    if (returnVal == 0 && this.ShowFooterWhenEmpty) {
                        Table table = this.Controls.OfType().First
    (); DataControlField[] dcf = new DataControlField[this.Columns.Count]; this.Columns.CopyTo(dcf, 0); //creates the footer row this._footerRow = this.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal, dataBinding, null, dcf, table.Rows, null); if (!this.ShowFooter) { _footerRow.Visible = false; } } return returnVal; } private GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, bool dataBind, object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) { GridViewRow row = this.CreateRow(rowIndex, dataSourceIndex, rowType, rowState); GridViewRowEventArgs e = new GridViewRowEventArgs(row); if ((rowType != DataControlRowType.Pager)) { this.InitializeRow(row, fields); } else { this.InitializePager(row, fields.Length, pagedDataSource); } //if the row has data, sets the data item if (dataBind) { row.DataItem = dataItem; } //Raises the RowCreated event this.OnRowCreated(e); //adds the row to the gridview's row collection rows.Add(row); //explicitly binds the data item to the row, including the footer row and raises the RowDataBound event. if (dataBind) { row.DataBind(); this.OnRowDataBound(e); row.DataItem = null; } return row; } } }

    提交回复
    热议问题