简体   繁体   English

转发器控制:移动单个项目

[英]Repeater Control : Move individual item

I have a repeater control binded to a generic List<> . 我有一个绑定到通用List<>的转发器控件。

The repeaters' items are all in its own <div> tag, so it gives a good block visual effect, as well as a standard placeholder for all the items. 中继器的项目都在其自己的<div>标记中,因此它具有良好的整体视觉效果,并且是所有项目的标准占位符。

With custom sorting, I have been able to arrange the results among its own standard, but now I need something that isn't standard. 通过自定义排序,我已经能够将结果按其自己的标准排列,但是现在我需要的不是标准的东西。

I need to be able to take a selected item in a <div> block, and move it either to the bottom or the top of the repeaters' item list. 我需要能够将选定的项目放在<div>块中,并将其移动到中继器项目列表的底部或顶部。

Is there any way I am able to do this? 有什么办法可以做到这一点? Either by binding, or using the repeaters' repeater_ItemDataBound() method? 是通过绑定,还是使用转发器的repeater_ItemDataBound()方法?

The code is quite long.. So I will post what I think would be the 'need to know'. 代码很长。.因此,我将发布我认为是“需要知道”的内容。

Populating the generic list 填充通用列表

                        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- Sorting the data, then binding -相同方法-排序数据,然后绑定

    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();

Generic List Class (With sorting) 通用列表类(带排序)

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

Real-world application of my question If there is a 'stickied' match - it must appear on top of the matches regardless of date, status or name 我的问题现实世界中的应用如果存在“粘性”匹配项-无论日期,状态或名称如何,匹配项都必须显示在匹配项的顶部

I would recommend that you order the data in your list before you see ditto the repeater. 我会建议您订购清单中的数据,你看到同上中继之前。 If this is not possible given structure of your data and/or the item template of the repeater, could you please include samples of the data and the repeater so that we may help further? 如果在给定数据结构和/或转发器项目模板的情况下无法做到这一点,请您提供数据和转发器的样本,以便我们提供进一步的帮助?

EDIT after asker posted code 提问者发布代码后进行编辑

rather than setting challenge as the data source for the repeater you should separate all the items that need to be on the top of the list into a new list and then append the other other items to the end of the top items. 而不是将质询设置为转发器的数据源,您应该将需要在列表顶部的所有项目都分离到一个新列表中,然后将其他项目添加到顶部项目的末尾。

something like this: 像这样的东西:

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

depending on what the criteria is for an item to be on the top, you may be able to use a LINQ expression to simplify this code. 根据使某项位于顶部的标准,您可以使用LINQ表达式来简化此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM