0

我有一个中继器控件绑定到一个通用的List<>.

中继器的物品都在自己的<div>标签中,因此它提供了良好的块视觉效果,以及所有物品的标准占位符。

通过自定义排序,我已经能够按照自己的标准排列结果,但现在我需要一些不标准的东西。

我需要能够在一个<div>块中获取选定的项目,并将其移动到中继器项目列表的底部或顶部。

我有什么办法可以做到这一点?通过绑定还是使用中继器的repeater_ItemDataBound()方法?

代码很长。所以我会发布我认为“需要知道”的内容。

填充通用列表

                        while (rdr.Read())
                        {
                            challenge.Add(new ChallengeList
                                {
                                    GameName = rdr["gameName"].ToString(),
                                    CreatorName = rdr["creatorName"].ToString(),
                                    MediatorName = rdr["mediatorName"].ToString(),
                                    ChallengeID = rdr["challengeId"].ToString(),
                                    ChallengeAccepted = rdr["accepted"].ToString(),
                                    MatchDate = DateTime.Parse(rdr["matchDate"].ToString()).ToString("dd MMMM yyyy hh:mm")
                                });
                        }

-相同的方法-对数据进行排序,然后绑定

    switch (sort)
    {
        case "name":
            Comparison<ChallengeList> name = ChallengeList.CompareGameName;
            challenge.Sort(name);
            break;

        case "date":
            Comparison<ChallengeList> date = ChallengeList.CompareDate;
            challenge.Sort(date);
            break;

        case "status":
            Comparison<ChallengeList> status = ChallengeList.CompareStatus;
            challenge.Sort(status);
            break;
    }

    rptChallenges.DataSource = challenge;
    rptChallenges.DataBind();

通用列表类(带排序)

/// <summary>
/// Class ChallengeList
/// With sorting
/// </summary>
public class ChallengeList
{
    /// <summary>
    /// Compares the name of the game.
    /// </summary>
    /// <param name="game1">The game1.</param>
    /// <param name="game2">The game2.</param>
    /// <returns>System.Int32.</returns>
    public static int CompareGameName(ChallengeList game1, ChallengeList game2)
    {
        return String.Compare(game1.GameName, game2.GameName, StringComparison.Ordinal);
    }

    /// <summary>
    /// Compares the status.
    /// </summary>
    /// <param name="status1">The status1.</param>
    /// <param name="status2">The status2.</param>
    /// <returns>System.Int32.</returns>
    public static int CompareStatus(ChallengeList status1, ChallengeList status2)
    {
        return string.Compare(status1.ChallengeAccepted, status2.ChallengeAccepted, StringComparison.Ordinal);
    }

    /// <summary>
    /// Compares the date.
    /// </summary>
    /// <param name="date1">The date1.</param>
    /// <param name="date2">The date2.</param>
    /// <returns>System.Int32.</returns>
    public static int CompareDate(ChallengeList date1, ChallengeList date2)
    {
        return string.Compare(date1.MatchDate, date2.MatchDate, StringComparison.Ordinal);
    }

    /// <summary>
    /// Compares the date reverse.
    /// </summary>
    /// <param name="date2">The date2.</param>
    /// <param name="date1">The date1.</param>
    /// <returns>System.Int32.</returns>
    public static int CompareDateReverse(ChallengeList date2, ChallengeList date1)
    {
        return string.Compare(date1.MatchDate, date2.MatchDate, StringComparison.Ordinal);
    }

    /// <summary>
    /// Gets or sets the name of the game.
    /// </summary>
    /// <value>The name of the game.</value>
    public string GameName { get; set; }
    /// <summary>
    /// Gets or sets the name of the creator.
    /// </summary>
    /// <value>The name of the creator.</value>
    public string CreatorName { get; set; }
    /// <summary>
    /// Gets or sets the name of the mediator.
    /// </summary>
    /// <value>The name of the mediator.</value>
    public string MediatorName { get; set; }
    /// <summary>
    /// Gets or sets the challenge ID.
    /// </summary>
    /// <value>The challenge ID.</value>
    public string ChallengeID { get; set; }
    /// <summary>
    /// Gets or sets the challenge accepted.
    /// </summary>
    /// <value>The challenge accepted.</value>
    public string ChallengeAccepted { get; set; }
    /// <summary>
    /// Gets or sets the match date.
    /// </summary>
    /// <value>The match date.</value>
    public string MatchDate { get; set; }
}

我的问题在现实世界中的应用 如果存在“粘性”匹配 - 无论日期、状态或名称如何,它都必须出现在匹配的顶部

4

1 回答 1

0

我建议您在看到同上中继器之前对列表中的数据进行排序。如果由于您的数据结构和/或转发器的项目模板无法做到这一点,您能否提供数据和转发器的样本,以便我们进一步提供帮助?

提问者发布代码后编辑

而不是将挑战设置为转发器的数据源,您应该将需要位于列表顶部的所有项目分隔到一个新列表中,然后将其他其他项目附加到顶部项目的末尾。

像这样的东西:

var topItmes = new List<ChallengeList>();
var otherItmes = new List<ChallengeList>();

foreach (var item in challenge)
{
    if(/*item needs to be on top*/)
    {
        topItmes.Add(item);
    }
    else
    {
        otherItmes.Add(item);
    }
}

topItmes.AddRange(otherItmes);

根据项目位于顶部的条件,您可以使用 LINQ 表达式来简化此代码。

于 2012-10-25T07:20:32.140 回答