简体   繁体   English

如何使用foreach创建列表并将项目添加到按字母顺序排列的单选按钮列表中

[英]How can I create a List with foreach and add items to radiobuttonlist alphabetical

I'm getting the Text and the key values from Umbraco and works 100%, but in this way I can't order the list I get. 我从Umbraco获取了Text和键值,并且可以100%工作,但是以这种方式我无法订购得到的列表。 The site will have different languages and I need the items to be ordered on the radiobuttonlist by value (which means by language) and not by the key in Umbraco. 该站点将使用不同的语言,我需要按值(即按语言)而不是Umbraco中的键在单选radiobuttonlist上订购商品。

Is it possible? 可能吗?

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
}

You could add each element to a SortedList first within the foreach . 您可以先在foreach中将每个元素添加到SortedList中 You can then bind the RadioButtonlist to this SortedList , as shown in this article . 然后,可以将RadioButtonlist绑定到此SortedList ,如本文所示。

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

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

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