Like that you can set DataTextField and DataValueField of DropDownList using "Key" and "Value" texts.
Like that you can set DataTextField and DataValueField of DropDownList using "Key" and "Value" texts : Dictionary list = new Dictionary(); list. Add("item 1", "Item 1"); list. Add("item 2", "Item 2"); list.
Add("item 3", "Item 3"); list. Add("item 4", "Item 4"); ddl. DataSource = list; ddl.
DataTextField = "Value"; ddl. DataValueField = "Key"; ddl.DataBind().
1 Someone already said that. But good try! Welcome to SO :) – scraimer Apr 30 '09 at 7:08.
When a dictionary is enumerated, it will yield KeyValuePair objects... so you just need to specify "Value" and "Key" for DataTextField and DataValueField respectively, to select the Value/Key properties. Thanks to Joe's comment, I reread the question to get these the right way round. Normally I'd expect the "key" in the dictionary to be the text that's displayed, and the "value" to be the value fetched.
Your sample code uses them the other way round though. Unless you really need them to be this way, you might want to consider writing your code as: list. Add(cul.
DisplayName, cod); (And then changing the binding to use "Key" for DataTextField and "Value" for DataValueField, of course. ) In fact, I'd suggest that as it seems you really do want a list rather than a dictionary, you might want to reconsider using a dictionary in the first place. You could just use a List>: string languageCodsList = service.
LanguagesAvailable(); var list = new List>(); foreach (string cod in languageCodsList) { CultureInfo cul = new CultureInfo(cod); list. Add(new KeyValuePair(cul. DisplayName, cod)); } Alternatively, use a list of plain CultureInfo values.
LINQ makes this really easy: var cultures = service. LanguagesAvailable() . Select(language => new CultureInfo(language)); languageList.
DataTextField = "DisplayName"; languageList. DataValueField = "Name"; languageList. DataSource = cultures; languageList.DataBind(); If you're not using LINQ, you can still use a normal foreach loop: List cultures = new List(); foreach (string cod in service.
LanguagesAvailable()) { cultures. Add(new CultureInfo(cod)); } languageList. DataTextField = "DisplayName"; languageList.
DataValueField = "Name"; languageList. DataSource = cultures; languageList.DataBind().
2 Actually, this is incorrect - see my comment on the accepted answer. – Winston Smith Apr 30 '09 at 7:51 Ah, I'd misread the question. It seems confusing to me to put them in a dictionary the "wrong" way round.
Will edit my answer. – Jon Skeet Apr 30 '09 at 8:19.
If the DropDownList is declared in your aspx page and not in the codebehind, you can do it like this. . Aspx: " DataValueField="Key" DataTextField="Value"> .
Aspx.Cs: protected void Page_Load(object sender, EventArgs e) { ddlStatus.DataBind(); // or use Page.DataBind() to bind everything } public Dictionary Statuses { get { // do database/webservice lookup here to populate Dictionary } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.