中继器控制:移动单个项目

时间:2012-10-25 06:20:23

标签: c# asp.net data-binding repeater

我有一个转发器控件绑定到通用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")
                                });
                        }

-Same Method-对数据进行排序,然后绑定

    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; }
}

我的问题的实际应用 如果存在'粘贴'匹配 - 它必须出现在匹配的顶部,而不管日期,状态或名称

1 个答案:

答案 0 :(得分:0)

我建议您在看到转发器的同时在列表中订购数据。如果根据您的数据结构和/或转发器的项目模板,这是不可能的,您能否请包括数据样本和转发器,以便我们可以进一步提供帮助?

在提问者发布代码后编辑

而不是将challenge设置为转发器的数据源,您应该将列表顶部需要的所有项目分成新列表,然后将其他其他项目附加到顶部项目的末尾。< / p>

类似的东西:

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表达式来简化此代码。

相关问题