如何使用foreach创建List并按字母顺序将项添加到radiobuttonlist

时间:2014-04-29 09:07:03

标签: c# umbraco

我从Umbraco获得Text和键值并且100%工作,但是这样我就无法订购我得到的列表。该网站将使用不同的语言,我需要按radiobuttonlist按值排序(通过语言表示),而不是按Umbraco中的键排序。

是否可能?< / p>

foreach (umbraco.cms.businesslogic.Dictionary.DictionaryItem d2 in d1.Children)
{
    translation = "";
    translation = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(d2.key).Value(lang);

    ListItem list; //start a list

    list = new ListItem(translation, d2.key); //save each item on it

    rbl_items.Items.Add(list); //add them to my radiobuttonlist
}

1 个答案:

答案 0 :(得分:1)

您可以先在foreach内将每个元素添加到SortedList。然后,您可以将RadioButtonlist绑定到此 SortedList ,如this article所示。

SortedList list= new SortedList();

foreach (umbraco.cms.businesslogic.Dictionary.DictionaryItem d2 in d1.Children)
{
    translation = "";
    translation = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(d2.key).Value(lang);

    list.Add(translation, d2.key);
}

//bind to RadioButtonList 
RadioButtonList1.DataSource = list;
RadioButtonList1.DataValueField = "Key";
RadioButtonList1.DataTextField="Value";
RadioButtonList1.DataBind();