Solved

Programitically setting selected dropdownlist item??

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

Suggested Solutions

Title # Comments Views Activity
Adjust the codes 3 65
aspx ascx, c# 7 45
Pdf not loading 2 41
SQL State HYT00. Timeout expired proplem 8 44
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

752 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