Solved

Programitically setting selected dropdownlist item??

Posted on 2004-10-11
3
153 Views
Last Modified: 2010-08-05

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();      
this._DdSort.Items.Add(listItem);
if(Convert.ToInt32(_sort) == Convert.ToInt32(item.Key))
      this._DdSort.SelectedValue = item.Key.ToString();
}

0
Comment
Question by:animated405
3 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 250 total points
ID: 12275275
Does your page always post back when an index is changed, e.g. is the AutoEventWireup set to true for this control? If yes, you should put the foreach statement in an if clause, something like:

if (!this.IsPostBack)
{
  foreach....
}
0
 

Author Comment

by:animated405
ID: 12524682

Sorry for not responding, the comment was helpful...thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question