Solved

Dropdownlist SelectedItem.Value always return default no matter selection

Posted on 2003-11-14
4
849 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
ID: 9753245
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
ID: 9754450
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
ID: 9757303
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
ID: 9757440
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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