Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programitically setting selected dropdownlist item??

Posted on 2004-10-11
3
Medium Priority
?
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 1000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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