Mark A selected item on first post. please help

G

Guest

I have tried this with no luck. What i want is to show the selected item on
the first post. the selected item is NY but the first in the list shows as
selected.


If Page.IsPostBack = False Then

Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New SqlCommand("Get_States",
myConnection)

Dim Statetypes As SqlDataReader

myConnection.Open()

Statetypes = myCommand.ExecuteReader

StateCode.DataSource = Statetypes
StateCode.DataTextField = "State"
StateCode.DataValueField = "StateId"


' Databind and display the list of favorite product items
StateCode.DataBind()
myConnection.Close()

Dim i As Integer
For i = 0 To StateCode.Items.Count - 1
Dim Statechk As String = StateCode.Items(i).Value
Dim chk = StateCode.DataValueField
If Statechk = customerDetails.State Then
StateCode.SelectedItem.Selected = True
End If
Next
end if
 
K

Ken Dopierala Jr.

Hi,

Your problem is in this code:
For i = 0 To StateCode.Items.Count - 1
Dim Statechk As String = StateCode.Items(i).Value
Dim chk = StateCode.DataValueField
If Statechk = customerDetails.State Then
StateCode.SelectedItem.Selected = True
End If
Next

The line below doesn't do anything new. It is constantly setting the
selected item to be selected again.

StateCode.SelectedItem.Selected = True

You would want to do something like this in its place:

StateCode.Items(i).Selected = True

Good luck! Ken.
 
K

Ken Dopierala Jr.

Hi,

I forgot to mention after you set the Selected property to True then put in
an Exit For to exit you from the loop:

If Statechk = customerDetails.State Then
StateCode.Items(i).Selected = True
Exit For 'Get out before it finds another one! Just kidding....
End If

Good luck! Ken.
 

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

Top