存储中继器中的元件以备再次使用

本文关键字:中继器 存储 | 更新日期: 2023-09-27 18:26:50

我有一个像讨论板一样使用的列表。每个用户旁边都有一个盒子,里面有一种颜色和他们的首字母缩写,我想在盒子里(从列表中)随机显示一种颜色,但如果那个人已经出现了,可以重复使用相同的颜色。我已经让盒子处理了随机产生的颜色,但我不知道如何使用中继器中的物品来确定它们是否应该重复使用颜色,而不是随机显示新颜色。

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>

项目数据绑定:

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

存储中继器中的元件以备再次使用

创建一个用户到颜色的字典缓存。在处理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;
}

编辑:将数据项分配给局部变量下方的人员的缓存逻辑移动。