[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Pre selection of drop down listbox item is not working ?

Posted on 2009-02-11
Medium Priority
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

Anurag Agarwal earned 1200 total points
ID: 23619069
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

Author Closing Comment

ID: 31545920
This anuragal dude is pretty sharp!

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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