从转发器存储元素再次使用

时间:2014-10-17 13:15:21

标签: c# repeater

我有一个列表,我像讨论板一样使用。每个用户旁边都有一个框,其中有一个颜色和它们的首字母,我想在框中随机显示一个颜色(从列表中),但如果该人已经出现,则重复使用相同的颜色。我已经让这些盒子的颜色随机出现了,但我不知道如何使用转发器中的一个项目来解决它们是否应该重新使用颜色而不是显示新的颜色随意。

HTML:

<asp:Repeater runat="server" ID="rptThread" OnItemDataBound="rptThread_ItemDataBound">
            <ItemTemplate>    
                <li class="media">
                <a class="pull-left" href="#">
                    <div class="foo hidden-xs" runat="server" id="divColour"><center> <span><asp:Label runat="server" id="lblInitials"></asp:Label></span> </center></div>
                </a>
                <div class="media-body">
                    <ul class="list-inline meta text-muted">
                    <li><i class="fa fa-calendar"></i> <asp:Label runat="server" id="lblDate"></asp:Label></li>
                    <li><i class="fa fa-user"></i> <a href="#"><asp:Label runat="server" id="lblName"></asp:Label></a></li>
                    </ul>
                    <p><asp:Label runat="server" id="lblText"></asp:Label></p>
                </div>
                </li>
            </ItemTemplate>
        </asp:Repeater>

的ItemDataBound:

  DataRowView nRow = null;
  ActiveDirectory AD = new ActiveDirectory();
  GeneralFunctions G = new GeneralFunctions();
    ArrayList Colours = G.getColours();
    Random rnd = new Random();
    int r = rnd.Next(Colours.Count);


    switch (e.Item.ItemType)
    {
        case ListItemType.Item:
        case ListItemType.AlternatingItem:
            nRow = (DataRowView)e.Item.DataItem;
            ((Label)e.Item.FindControl("lblInitials")).Text = AD.getInitialsFromAD(nRow["ThreadPerson"].ToString());
            ((Label)e.Item.FindControl("lblDate")).Text = nRow["ThreadDate"].ToString();
            ((Label)e.Item.FindControl("lblName")).Text = AD.getFullNameFromSID(nRow["ThreadPerson"].ToString());
            ((Label)e.Item.FindControl("lblText")).Text = nRow["ThreadText"].ToString();
            ((HtmlGenericControl)e.Item.FindControl("divColour")).Attributes.Add("style", "background-color: " + (string)Colours[r]);
            break;
    }

颜色列表只是:

public ArrayList getColours()
        {
            ArrayList Colours = new ArrayList();
            Colours.Add("#22447a");
            Colours.Add("#761e10");
            Colours.Add("#256908");
            Colours.Add("#422562");
            etc (redacted).....
            return Colours;
        } 

1 个答案:

答案 0 :(得分:1)

创建用户对颜色的字典缓存。在处理ItemDataBound事件时,检查用户的颜色是否在缓存中,如果使用它,否则为它们选择一种颜色并将其粘贴在缓存中以供下次使用。

//this should be declared and initialized at the Page level so it is available across all runs of the ItemDataBound
var userColorCache = new Dictionary<string, string>();

DataRowView nRow = null;
ActiveDirectory AD = new ActiveDirectory();
GeneralFunctions G = new GeneralFunctions();
ArrayList Colours = G.getColours();
Random rnd = new Random();

switch (e.Item.ItemType)
{
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
        nRow = (DataRowView)e.Item.DataItem;
        var personInitials = AD.getInitialsFromAD(nRow["ThreadPerson"].ToString());
        string userColor = null;
        if (userColorCache.ContainsKey(personInitials)) {
          userColor = userColorCache[personInitials];
        } else {
          int r = rnd.Next(Colours.Count);
          userColor = (string)Colours[r];
          userColorCache.Add(personInitials, userColor)
        }


        ((Label)e.Item.FindControl("lblInitials")).Text = personInitials;
        ((Label)e.Item.FindControl("lblDate")).Text = nRow["ThreadDate"].ToString();
        ((Label)e.Item.FindControl("lblName")).Text = AD.getFullNameFromSID(nRow["ThreadPerson"].ToString());
        ((Label)e.Item.FindControl("lblText")).Text = nRow["ThreadText"].ToString();
        ((HtmlGenericControl)e.Item.FindControl("divColour")).Attributes.Add("style", "background-color: " + userColor);
        break;
}

编辑:将低于DataItem分配的人的缓存逻辑移动到本地变量。