【问题标题】:Repeater Control : Move individual item中继器控制:移动单个项目
【发布时间】:2012-10-25 06:20:22
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    提问者发布代码后编辑

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

    类似这样的:

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

    【讨论】:

    • 抱歉,回复时间过长.. 没看到更新。我可以看到这段代码的过程..会尽快尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多