【发布时间】:2011-10-09 07:02:18
【问题描述】:
我一直在编写网格视图的扩展,通过观察 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);
}
问题是,当我单击分页按钮时,分页事件不会触发,并且添加的额外行变为空行。页面回发,并通过页面加载等运行,但从不通过分页事件。我在此扩展程序中也内置了自定义分页,并且我的分页按钮的单击事件也不会触发。直到第二次点击分页按钮才会发生分页。
我认为这是因为分页行是在 rowdatabound 事件之前构建的,在此期间会创建分页按钮并分配它们的事件处理程序。但是,将行添加到基础表会删除这些处理程序。任何人都知道如何确保自定义分页按钮触发?
编辑1:澄清一下,添加到数据源然后重新绑定将不起作用,因为没有额外的数据要添加。我试图达到的效果是这样的:
|第 1 组 |
|项目 1 |项目 2 |项目 3 |
|第 4 项 |第 5 项 |第 6 项 |
|第 2 组 |
|第 7 项 |第 8 项 |第 9 项 |
【问题讨论】:
-
当您触发 page_load 时,您似乎将行添加到网格而不是数据源,您将网格重新绑定到“旧”数据源并丢失您添加的值.
-
我不想向数据源添加行,因为没有额外的数据要添加。相反,额外的行充当组标题。
标签: c# asp.net event-handling