在 ASP.NET 中自定义 DataGrid 分页器






4.52/5 (16投票s)
通过添加有关正在显示的数据的额外信息,使 DataGrid 中的分页器更有用。
引言
ASP.NET 中内置的 DataGrid
控制用于显示表格数据。本文展示了如何扩展此控件并修改分页器以显示有关数据的其他信息,例如记录数和页数。
背景
DataGrid
中的分页器有助于用户浏览大量数据。但是,页码与右侧对齐,左侧有一个很大的空白区域,可以更有效地利用 (参见图 1)。本文展示了如何将一些关于数据的额外信息插入到此空间中 - 例如,文本如下:正在显示 82 条记录。当前在第 2 页,共 10 页。 还可以应用一些可选的格式设置,例如,如果只有一页,则删除页码。
图 1. 默认 DataGrid
控制。
创建扩展的 DataGrid 控制
要开始,我们创建一个从 DataGrid
继承的新类。我们将这个新类命名为 DataGridWithImprovedPager
。
class DataGridWithImprovedPager: DataGrid
{
private bool m_DisplayPageNumbers = true;
public DataGridWithImprovedPager()
{
}
}
我们重写 OnItemCreated
方法,并在分页器中插入一个空白标签(和单元格)。我们将在控制渲染之前检索此标签,并用有关正在显示的数据的信息填充它。
protected override void OnItemCreated(DataGridItemEventArgs e)
{
switch (e.Item.ItemType)
{
case (ListItemType.Pager):
// Only display paging links if we have more than one page
m_DisplayPageNumbers = (this.PageCount > 1);
// Get the table cell holding the page numbers
TableCell cell = e.Item.Controls[0] as TableCell;
// Hide the cell if we're not displaying page numbers
cell.Visible = m_DisplayPageNumbers;
// Check that we haven't added our numrecords label already
// For some reason, the Pager item gets hit twice.
if (cell.FindControl("lblNumRecords") == null)
{
// Work out how many columns we needs to substract from our colspan
int count = (m_DisplayPageNumbers && this.Columns.Count > 1) ?
this.Columns.Count - 1 : this.Columns.Count;
// Extract the Pager
TableCell pager = (TableCell) e.Item.Controls[0];
// Add Cell to Row to Hold Row Count Label
TableCell newcell = new TableCell();
newcell.ColumnSpan = count;
newcell.HorizontalAlign = HorizontalAlign.Left;
newcell.Style["border-color"] = pager.Style["border-color"];
// Add Label Indicating Row Count
Label lblNumRecords = new Label();
lblNumRecords.ID = "lblNumRecords";
// Create a new column and append it to
// the list if we have more the one column.
// Otherwise, just add our label into the existing column
if (this.Columns.Count > 1)
{
newcell.Controls.Add(lblNumRecords);
// Add Table Cell to Pager
e.Item.Controls.AddAt(0, newcell);
// Subtract from Colspan of Original Pager to Account for New Row
pager.ColumnSpan = pager.ColumnSpan - count;
}
else
{
cell.Controls.AddAt(0, lblNumRecords);
}
}
break;
}
base.OnItemCreated (e);
}
最后一步是重写 OnPreRender
方法,并检索先前添加的标签。然后,我们用列表中项目的数量填充它,如果有多于一页,则还包括有关当前页和总页数的信息。
protected override void OnPreRender(System.EventArgs e)
{
Control c1 = this.Controls[0];
Control c2 = c1.Controls[c1.Controls.Count - 1];
Label lblNumRecords = c2.FindControl("lblNumRecords") as Label;
// Populate the message label with the number of records
lblNumRecords.Text = string.Format("{0} records found.", this.VirtualItemCount);
// If we have pages of data, then update the message to show paging information
if (m_DisplayPageNumbers)
{
lblNumRecords.Text += string.Format(" Currently on page {0} of {1}.",
CurrentPageIndex+1,
PageCount);
}
base.OnPreRender (e);
}
图 2. 我们改进后的 DataGrid
控制。
关注点
我在 DataGrid
内部检索列表中项目的总数时遇到了一些问题,因此必须在绑定数据时手动设置 VirtualItemCount
属性。
历史
这是我的第一篇文章,任何建议或评论都将不胜感激!:-)