Solved

Programitically setting selected dropdownlist item??

Posted on 2004-10-11
3
150 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now