We help IT Professionals succeed at work.

Pre selection of drop down listbox item is not working ?

brgdotnet
brgdotnet asked
on
Medium Priority
297 Views
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

Comment
Watch Question

Python Developer
Commented:
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
 
Anurag
 

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
brgdotnetcontractor

Author

Commented:
This anuragal dude is pretty sharp!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.