Pre selection of drop down listbox item is not working ?

Posted on 2009-02-11
Last Modified: 2012-06-27
I have a drop down list box control and I need to set it to the value which is read from the database. If I trace into the code, then I can see that at line 8 the value stored in Client.purchase.Name.ToString() is the correct value of "Birthday". Yet when the web form actually displays, the valued displayed in the drop down listbox is incorrect. It is set to "Easter" Does anyone know what the problem is? Why isn't the default value being set to the correct value of "Birthday" ?
1          DropDownList purchasereason = (DropDownList)prizegiveaway.FindControl("purchasereason");

2            purchasereason.DataSource = ClientService.Getpurchases(UserState.ServiceProviderId);

3            purchasereason.DataTextField = "Name";

4            purchasereason.DataValueField = "Id";

5            purchasereason.DataBind();

6            if (Client.purchase.Name != null)

7            {

8                purchasereason.SelectByValue(Client.purchase.Name.ToString());

9            }

10           else

11           {

12               purchasereason.AddSelect(); // Will default to "-- Select --"

13           }

Open in new window

Question by:brgdotnet
    LVL 11

    Accepted Solution

    you are calling function "SelectByValue" and you are passing the "Name" value which is your DataTextField
    Do this
    7            {
    8                purchasereason.SelectByValue(Client.purchase.Id.ToString());
    It should work
    LVL 2

    Author Closing Comment

    This anuragal dude is pretty sharp!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    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

    16 Experts available now in Live!

    Get 1:1 Help Now