FAQ - Umbraco

Question
Accessing Drop Down Lists Programatically
Answer

The code snippets below provide examples for how to programatically access Umbraco data types with pre values, for example the Umbraco drop down list:

Populating a drop down list with pre values

using System.Collections;
using umbraco.cms.businesslogic.datatype;

var values = PreValues.GetPreValues(YOUR_DATA_TYPE_ID_HERE);
 
foreach (DictionaryEntry de in values) { 
    PreValue value = (PreValue)de.Value; 
    ListItem li = new ListItem(); 
    li.Text = value.Value; 
    li.Value = value.Id.ToString(); 
    dropDownList.Items.Add(li); 
}

The above code queries Umbraco for the pre-values for the data type you specify and returns a collection of DictionaryEntry's. We then loop through, adding list items to your drop down list.

 

Saving the selected value

dropDownList.SelectedItem.Value

Simply get the selected item's value using the code above and save it in the node.

If you found this information useful, please share it!


This FAQ was last updated on Wednesday, September 30, 2015

Contact Information

To find out more about Ava solutions you can contact us in a number of ways:
Follow Us...