J.D. Ray
J.D. Ray

Reputation: 727

Populating choices in an ASP.NET ComboBox from web.config

There are several answered questions detailing how to refer to a single value from web.config file. I'd like to maintain lists of values with which to populate combo boxes in the Views. Should I use some sort of key/value pair structure, with the key identifying the particular combo box to populate?

<add key="CalculationMethod" value="Fixed"/>
<add key="CalculationMethod" value="Cost Plus"/>
<add key="CalculationMethod" value="Formula"/>

I'm not sure how I'd read this, or if that would even work (don't keys have to be unique?). The IntelliSense for web.config doesn't seem to allow much in the appSettings section that looks applicable to a more robust structure like

<list name="CalculationMethod">
    <item value="Fixed"/>
    <item value="Cost Plus"/>
    <item value="Formula"/>
</list>

I see that the root <configuration> has a lot of options for children, but which one to use, if any?

Upvotes: 0

Views: 715

Answers (1)

Rick S
Rick S

Reputation: 6586

You really should be using a database for something like this but if you must put it in the web.config then you could delimit the values and parse them out at run time.

<add key="CalculationMethod" value="Fixed,Cost Plus,Formula"/>

myComboBox.DataSource = new List<String>(lstrCalcMethodsFromWebConfig.Split(','));

Upvotes: 1

Related Questions