Programitically setting selected dropdownlist item??
Posted on 2004-10-11
Hello I'm using c#.net. I have a dropdownlist control on the page that I build up the items for programatically like so in the code behind. I'm checking for a variable that's passed through the querystring, and if it equals the value of the collection item I'm looping through when building the list, then I want it to be the selected item. Problem is it selects the item. but always reselects the same item once I try to select another one from the list, and it just always goes back to the same selection as what was set when initially landing to the page. What am I doing wrong please, thanks in advance!!
System.Collections.SortedList sortList = new System.Collections.SortedList();
sortList["1"] = "price lowest to highest";
sortList["2"] = "price highest to lowest";
sortList["5"] = "product name";
foreach(DictionaryEntry item in sortList)
System.Web.UI.WebControls.ListItem listItem = new System.Web.UI.WebControls.ListItem();
listItem.Text = item.Value.ToString();
listItem.Value = item.Key.ToString();
if(Convert.ToInt32(_sort) == Convert.ToInt32(item.Key))
this._DdSort.SelectedValue = item.Key.ToString();