添加行到GridView吞下分页事件

本文关键字:分页 事件 GridView 添加行 | 更新日期: 2023-09-27 18:02:47

我一直在为网格视图编写扩展,通过观察RowDataBoundEvent中所需的项何时更改,将分组行添加到网格视图。到目前为止,它看起来有点像这样:

protected override void OnRowDataBound(GridViewRowEventArgs args)
{
    if (args.Row.RowType == DataControlRowType.DataRow)
    {
        object dataitem = args.Row.DataItem;
        string currentValue = dataitem.GetType().GetProperty(GroupingColumn).GetValue(dataitem, null).ToString();
        if (string.IsNullOrEmpty(previousValue) || currentValue != previousValue)
        {
            int currentRowIndex = args.Row.RowIndex + rowsAdded;
            int colspan = this.Columns.Count - 1;
            Label label = new Label { Text = currentValue, CssClass = "rowLabel" };
            TableCell cell = new TableCell { ColumnSpan = colspan, CssClass = "groupHeader" };
            GridViewRow newRow = new GridViewRow(currentRowIndex, currentRowIndex, DataControlRowType.DataRow, DataControlRowState.Normal);
            cell.Controls.Add(label);                                   
            newRow.Cells.Add(cell);
            ((Table)args.Row.Parent).Controls.AddAt(currentRowIndex, newRow);
            previousValue = currentValue;
            rowsAdded++;
         }
     }
    base.OnRowDataBound(args);
}

问题是,当我单击分页按钮时,分页事件不会触发,并且添加的额外行变成空行。页面回发,并通过页面加载等运行,但从不通过分页事件运行。我有自定义分页内置于此扩展以及,并为我的分页按钮的单击事件不火。直到第二次单击分页按钮,分页才发生。

我认为这是发生的,因为分页行在行绑定事件之前构造,在此期间创建分页按钮并分配其事件处理程序。但是,向基础表添加行会删除这些处理程序。有人知道如何确保自定义分页按钮启动吗?

编辑1:为了澄清,添加到数据源然后重新绑定将不起作用,因为没有额外的数据要添加。我试图实现的效果是这样的:

|组1 |

|项目1 |项目2 |项目3 |

|项目4 |项目5 |项目6 |

|组2 |

|项目7 |项目8 |项目9 |

添加行到GridView吞下分页事件

根据类似问题的答案找到解决方案。诀窍是在PrepareControlHierarchy事件中添加行。另外,请确保您的行类型为"Header",否则自定义分页可能会产生一些讨厌的副作用。

参见添加Gridview Row AFTER Header