How can I set a selected value of a dropdownlist?

By Dillon.damien ·
I am working in (vb). I have a drop down list populated from table 1. I am trying to save the selected value to table 2. However, it does not seem to matter which value I select in the dropdownlist, it always defaults to the first item in the list.

How can I set a variable equal to the selected item in the dropdownlist? Do I do this on the ASP side or vb side. I am trying to apply combobox logic to this problem but it does not seem to work.

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -


by jgarcia102066 In reply to How can I set a selected ...

This totally depends on how you are populating your combobox. Show us the code on how you are populating your combobox and we could point you in the right direction.

Collapse -

Drop down style property at a guess

by Tony Hopkinson In reply to How can I set a selected ...

Default is to be able to enter free format text. If that's what you have then you ae bound to the text property.
If you can't then it's bound to the selected / selectedIndex property.

Collapse -

Here is the code I am using...

by Dillon.damien In reply to Drop down style property ...

I am using datasets/dataadapters to connect to an Access database. I am populating the Drop down list with the below code:

Pubic Sub PopulateDropDown()

Dim i As Integer, strMovieName As String
For i = 1 To objDS.Tables("Movies").Rows.Count
strMovieName = objDS.Tables("Movies").Rows(i - 1).Item("MovieName")

End Sub

This seems to work fine. The dropdown populates with the data in column 'MovieName' of 'Movie' table.

I have the following code behind a button to save Actor data to a second table:

Dim objRow As DataRow
objRow = objDS.Tables("Actor").NewRow
objRow.Item("Actor") = txtActor.Text
objRow.Item("MovieName") = cboMovie.SelectedItem.Text

'cboMovie is the dropdownlist

I was hoping that whichever movie name I selected in the in the dropdown would be saved to the 'MovieName' column in the 'Actor' table. However, the same value always gets saved (i.e: the first in the list). Any idea how I can get the selected value to save to the 'Actor' table or how I could set a variable equal to the selected value in the drop down.
I am doing it this way so that it is only possible to add actor information to a Movie which already exists in the database.

Collapse -

Let's see if this helps.

by jgarcia102066 In reply to Here is the code I am usi ...

There are many different approaches to accomplishing a task when programming which is why I was asking how you were populating the dropdownlist. The way you set up the dropdownlist determines how you get the data back out.

What you need to do to capture the data value is to use:


Good Luck.

Collapse -

Same result...

by Dillon.damien In reply to Let's see if this helps.

I tried to set a variable to cboMovie.SelectedItem.ToString as you suggested but unfortunately I am getting the same result, only the first value in the drop down list is being saved. My updated code is below:

Behind the dropdown list's 'selectedItemchanged' method i have put movieName = cboMovie.SelectedItem.ToString

Then behind the save button I have changed the line below to equal MovieName:

objQuoteRow.Item("MovieName") = MovieName

No luck...
Is it possible I have to change something on the ASP side? My ASP code is below:

asp:DropDownList ID="cboMovie" runat="server" Style="z-index: 101; left: 33px; position: absolute;
top: 69px" Width="200px"


Collapse -

Postback Issue?

by jgarcia102066 In reply to Same result...

Are you populating the dropdownlist in the page_load event? If so, you need to check for a postback event like the code below:

If Not IsPostBack Then


End If

I'm not completely sure about the syntax, it's been awhile since I've worked in VB.

Your clues indicate a postback issue. I apologize if this is Mickey Mouse to you but since I don't have a frame of reference as to your experience with ASP programming, I'm starting with the basics.

Let me know if this fixed the issue for you.

Collapse -

That did it...

by Dillon.damien In reply to Postback Issue?

The code now works as I want it to. Thanks a lot for your help!!

Back to Web Development Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums