Question: Dropdown List Control

G

Guest

Hi Guys, I have a table with country field for each user. Now when i go into
modify mode, i load the dropdown list with all the countries from the
datbase. I need to preselect the index for the appropriate country assigned
for that user. Lets say if i set Tom for Canada, and when i bring his record
in modify mode, i should see canada instead of first county in the drop down
list.

Can this be done?

Thanks in advane.

Manny
 
K

Karl Seguin

yourDropDownList.SelectedIndex =
yourDropDownList.IndexOf(yourDropDownList.Items.FindByValue("CanadasValue"))

without much code it's hard to help you any further...but it's something
like that...."CanadasValue" would be the value part of the item bound to the
dropdown..

Karl
 
C

Craig Deelsnyder

Hi Guys, I have a table with country field for each user. Now when i go
into
modify mode, i load the dropdown list with all the countries from the
datbase. I need to preselect the index for the appropriate country
assigned
for that user. Lets say if i set Tom for Canada, and when i bring his
record
in modify mode, i should see canada instead of first county in the drop
down
list.

Can this be done?

Thanks in advane.

Manny

A full example:
http://www.dotnetjunkies.com/HowTo/B6506FDB-B5DD-4452-ABA0-37206419BD3D.dcik
 
G

Guest

In the aspx page, i have
<asp:dropdownlist id="dListOfficeCode" runat="server"
DataTextField="Description" DataValueField="OfficeCode" />

In Codebehind this is how i bind:

myConnection = New SqlConnection(conStr)
Dim myCommand As SqlCommand = New SqlCommand("Select OfficeCode
as OfficeCode, Description as Description from OfficeCode order by
Description", myConnection)
Dim myDataReader As SqlDataReader
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dListOfficeCode.DataSource = myDataReader
dListOfficeCode.DataBind()
dListOfficeCode.Items.FindByValue(

Catch myException As Exception
Response.Write("An error has occurred: " &
myException.ToString())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
End If

dListOfficeCode.SelectedIndex = 0
End Try

Would this help?

Manny
 
J

JV

Funny. This is exactly what I was writing about (see post earlier today
starting with "IMHO")
 
K

Karl Seguin

From what I told you, you should be able to figure it out:

dListOfficeCode.Items.SelectedIndex =
dListOffice.Items.IndexOf(dListOfficeCode.Items.FindByValue("canada"))

Karl
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top