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

StackOverflow https://stackoverflow.com/questions/23360257

  •  11-07-2023
  •  | 
  •  

Question

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. 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.

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
}
Was it helpful?

Solution

You could add each element to a SortedList first within the foreach. You can then bind the RadioButtonlist to this SortedList, as shown in 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top