Repeater,DataGrid或其他选项

时间:2011-12-02 21:06:02

标签: c# asp.net datagrid repeater

我需要一个控件,它将一次加载500-1000行,没有分页,轻量级,可排序,每行基于“状态”的颜色代码,并且能够每隔第N行重复一次标题。

我想到使用转发器,并想知道这种控制是否支持所有选项。

3 个答案:

答案 0 :(得分:0)

这实际上取决于数据。你想要一个像网格布局,或者你想要一个模板来布局。但无论是或将会奏效。 Here is a link for sorting in a repeater

答案 1 :(得分:0)

Repeater不支持自行排序,但您可以通过对绑定数据的原始对象进行排序来向控件添加排序。如果将此对象存储在Session中,则用户可以多次排序,而无需等待数据库中的新结果(例如)。

至于每N行重复一次标题,我认为没有办法做到这一点。复制标题有很多其他选项(例如使用jQuery或JavaScript),但如果你问我,那就太臭了。您可能最好使用一些CSS或JavaScript来浮动标题行(一旦用户滚过标题)并将其保持在屏幕顶部。

答案 2 :(得分:0)

您可以使用GridView。这是一个带有行状态,标题重复和排序的小演示。

e.g。

Default.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>

Default.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";    
            } 
        }
    }
}

(一些小错误,但你明白了......)