Solved

Programitically setting selected dropdownlist item??

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
A short film showing how OnPage and Connectwise integration works.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

15 Experts available now in Live!

Get 1:1 Help Now