Solved

Dropdownlist SelectedItem.Value always return default no matter selection

Posted on 2003-11-14
4
839 Views
Last Modified: 2008-02-01
Hi, need some help troubleshooting this one.  I'm coding asp.net and c# and using a dropdownlist (ddl) control on a webform.  I bound the items in the Page_Load() from a ms access database.  The ddl lists the correct items in the browser but it doesn't matter what I select in the ddl, the SelectedItem.Value or Text will always return the default value (1st entry in list).  The strange thing is when I add to the list using the Items in the properties, then it works.  Appreciate any help you can provide.
0
Comment
Question by:yuen_wc
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 50 total points
Comment Utility
First thing you need to do is check to see if the list is empty, if not read the selecteditem.  It sounds like you are overwriting the list by bounding it each time.

Try this
if( !IsPostBack)
{
    .....
   ddl.DataBind();
}

Check to see if it is not a post back insures that it only binds it once. Remeber that the page_load event happens before the other events, like SelectedItemChange.
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
Yes, when the site is posted back then the object is created again on the server. When you recreate the items you loose the information.
0
 

Author Comment

by:yuen_wc
Comment Utility
IsPostBack did the trick.  Thank you for the help.  Off topic, if you have the same item appear multiple times in a database column and only want to bind it once, how would you do that?
0
 
LVL 9

Expert Comment

by:Joeisanerd
Comment Utility
The database has it multiple times? Then fix your query to only show the UNIQUE or DISTINCT rows for that column. If it is an issue of doubling the list, then you need to erase the dropdownlist before binding it. ddl.Items.Clear, then ddl.DataBind();
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

11 Experts available now in Live!

Get 1:1 Help Now