C# 存储中继器中的元素以供再次使用

C# 存储中继器中的元素以供再次使用,c#,repeater,C#,Repeater,我有一个列表,就像一个讨论板。每个用户旁边都有一个框,框中有颜色和他们的姓名首字母,我想基本上在框中随机显示一种颜色(从列表中),但如果该用户已经出现,请重复使用相同的颜色。我已经让这些盒子使用随机产生的颜色,但我无法想出如何使用转发器中的项目来确定它们是否应该重复使用颜色,而不是随机显示新颜色 HTML: 颜色列表如下: public ArrayList getColours() { ArrayList Colours = new ArrayList()

我有一个列表,就像一个讨论板。每个用户旁边都有一个框,框中有颜色和他们的姓名首字母,我想基本上在框中随机显示一种颜色(从列表中),但如果该用户已经出现,请重复使用相同的颜色。我已经让这些盒子使用随机产生的颜色,但我无法想出如何使用转发器中的项目来确定它们是否应该重复使用颜色,而不是随机显示新颜色

HTML:

颜色列表如下:

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;
}
//这应该在页面级别声明和初始化,以便在ItemDataBound的所有运行中都可用
var userColorCache=new Dictionary();
DataRowView nRow=null;
ActiveDirectory AD=新的ActiveDirectory();
GeneralFunctions G=新的GeneralFunctions();
ArrayList colors=G.getcolors();
随机rnd=新随机();
开关(例如Item.ItemType)
{
案例列表项目类型。项目:
案例ListItemType.AlternatingItem:
nRow=(DataRowView)e.Item.DataItem;
var personInitials=AD.getInitialsFromAD(nRow[“ThreadPerson”].ToString());
字符串userColor=null;
if(userColorCache.ContainsKey(personInitials)){
userColor=userColorCache[personInitials];
}否则{
int r=rnd.Next(colors.Count);
userColor=(字符串)颜色[r];
添加(personInitials,userColor)
}
((标签)e.Item.FindControl(“lblInitials”)。Text=人名初始值;
((标签)例如Item.FindControl(“lblDate”)).Text=nRow[“ThreadDate”].ToString();
((标签)例如Item.FindControl(“lblName”).Text=AD.getFullNameFromSID(nRow[“ThreadPerson”].ToString());
((标签)例如Item.FindControl(“lblText”)).Text=nRow[“ThreadText”].ToString();
((HtmlGenericControl)e.Item.FindControl(“divcolor”)).Attributes.Add(“样式”,“背景色:”+userColor);
打破
}

编辑:将数据项赋值下的person的缓存逻辑移动到局部变量。

谢谢,但是AD.getInitialsFromAD(nRow[“ThreadPerson”].ToString())-ThreadPerson未在该点设置,是吗?哦,我没有注意到它使用的是nRow变量。只需在switch语句中向下移动其逻辑,或者在ItemDataBound事件中向上移动nRow变量的赋值。
public ArrayList getColours()
        {
            ArrayList Colours = new ArrayList();
            Colours.Add("#22447a");
            Colours.Add("#761e10");
            Colours.Add("#256908");
            Colours.Add("#422562");
            etc (redacted).....
            return Colours;
        } 
//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;
}