【发布时间】:2011-12-02 21:06:02
【问题描述】:
我需要一个控件,它可以一次加载 500-1000 行,没有分页,轻量级,可排序,每行基于“状态”的颜色代码,并且能够每第 N 行重复一次标题。
我想使用中继器,想知道这个控件是否支持所有选项。
【问题讨论】:
标签: c# asp.net datagrid repeater
我需要一个控件,它可以一次加载 500-1000 行,没有分页,轻量级,可排序,每行基于“状态”的颜色代码,并且能够每第 N 行重复一次标题。
我想使用中继器,想知道这个控件是否支持所有选项。
【问题讨论】:
标签: c# asp.net datagrid repeater
这真的取决于数据。你想要一个类似网格的布局还是想要一个模板来布局它。但要么或将工作。 Here is a link for sorting in a repeater
【讨论】:
Repeater 本身不支持排序,但您可以通过对绑定数据的原始对象进行排序来为控件添加排序。如果您将此对象存储在Session 中,那么您的用户可以进行多次排序,而无需等待来自数据库的新结果(例如)。
至于每 N 行重复一次标题,我认为没有办法做到这一点。复制标头还有很多其他选项(例如,使用 jQuery 或 JavaScript),但如果你问我,那就太难了。您最好使用一些 CSS 或 JavaScript 来浮动标题行(一旦用户滚动经过标题)并将其保持在屏幕顶部。
【讨论】:
您可以使用 GridView。这是一个关于行状态、标题重复和排序的小演示。
例如
默认.aspx
<%@ Page Language="C#" Inherits="GridViewDemo.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head runat="server">
<title>Default</title>
<style>
tr.Good { background-color: green; }
tr.Bad { background-color: red; }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="GV" runat="server"
OnRowDataBound="GV_RowDataBound"
AutoGenerateColumns="false"
AllowSorting="true"
OnSorting="GV_Sort"
>
<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" SortExpression="Id"/>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name"/>
</Columns>
</asp:GridView>
</form>
</body>
</html>
默认.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace GridViewDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GV.DataSource = GetDataSource();
GV.DataBind();
}
protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
SetCssClass(e.Row);
if (e.Row.RowIndex % 10 == 0)
{
AddHeader (e);
}
}
}
private void SetCssClass(GridViewRow row)
{
row.CssClass += ((Entity)row.DataItem).Status;
}
private void AddHeader (GridViewRowEventArgs e)
{
GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.Controls.Add(new Label { Text = "Id" });
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(new Label { Text = "Name" });
row.Cells.Add(cell);
Table tbl = (e.Row.Parent as Table);
tbl.Controls.AddAt(e.Row.RowIndex + 1, row);
}
protected void GV_Sort(object sender, GridViewSortEventArgs e)
{
List<Entity> sortedList = GetDataSource();
if (e.SortExpression == "Id")
{
if (e.SortDirection == SortDirection.Ascending)
sortedList.Sort((x, y) => x.Id.CompareTo(y.Id));
else
sortedList.Sort((x, y) => y.Id.CompareTo(x.Id));
}
else if (e.SortExpression == "Name")
{
if (e.SortDirection == SortDirection.Ascending)
sortedList.Sort((x, y) => x.Name.CompareTo(y.Name));
else
sortedList.Sort((x, y) => y.Name.CompareTo(x.Name));
}
GV.DataSource = sortedList;
GV.DataBind();
}
protected List<Entity> GetDataSource ()
{
List<Entity> result = new List<Entity>();
for (int i = 0; i < 500; i++)
{
result.Add(new Entity() { Id=i, Name="foo" });
}
return result;
}
}
}
Entity.cs
using System;
namespace GridViewDemo
{
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public string Status
{
get
{
if (Id % 42 == 0) { return "Good"; };
if (Id % 111 == 0) { return "Bad"; };
return "Normal";
}
}
}
}
(一些小错误,但你明白了......)
【讨论】: