binding to current value in a drop down list when populating from a database

T

TB

Hi All:

The following is probably a newbie question, but please bear with me:

I am populating a drop down list with items from a database, and would
the default selected item to be the current value of the corresponding
field. I am doing something like this:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))
ddlexample.DataBind()

the variable "Currvalue" contains the current value of "ID"

But the result is that the first item of the list is always displayed
as the selected item, not the one the corresponds to the actual value
of the field "ID"

What I am doing wrong here?

Thanks

TB
 
S

Siva M

Set the SelectedIndex property after the DataBind() call.

Hi All:

The following is probably a newbie question, but please bear with me:

I am populating a drop down list with items from a database, and would
the default selected item to be the current value of the corresponding
field. I am doing something like this:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))
ddlexample.DataBind()

the variable "Currvalue" contains the current value of "ID"

But the result is that the first item of the list is always displayed
as the selected item, not the one the corresponds to the actual value
of the field "ID"

What I am doing wrong here?

Thanks

TB
 
T

TB

Following your advice, I just tried:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.DataBind()
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))

But the result is still the same: the selected item is still the first
in the list, not the current value of the ID field.

TB
 
P

Phillip Williams

This can be the case if the currvalue was not found in the list. While
developing asp.net programming skills, I would suggest expanding the code
lines for clarity and easy debugging, such as:



Dim li As ListItem = ddlexample.Items.FindByValue(currvalue)
If li Is Nothing Then
'this will throw an exception that the value was not found
'which you will see on the web page. After you complete the
'debugging you can comment out this line
Throw New Exception("No list item with the value = " & currvalue & " was
found")
Else
ddlexample.ClearSelection()
li.Selected = True
End If
 
S

Siva M

Does the ID value exist in the list?

Following your advice, I just tried:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.DataBind()
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))

But the result is still the same: the selected item is still the first
in the list, not the current value of the ID field.

TB
 
T

TB

Sorry for not replying before. Everything works now. Some bug testing
revealed an incorrect use of the variable currvalue. Also, it is clear
that databinding must take place before the selecteditem property.

Thanks for your help.

TB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top