• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

Pre selection of drop down listbox item is not working ?

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

0
brgdotnet
Asked:
brgdotnet
1 Solution
 
Anurag AgarwalPython DeveloperCommented:
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
 
0
 
brgdotnetcontractorAuthor Commented:
This anuragal dude is pretty sharp!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now