Questions

How can I set a selected value of a dropdownlist?

Tags:
+
1 Votes
Locked

How can I set a selected value of a dropdownlist?

Dillon.damien
I am working in ASP.net (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.
  • +
    0 Votes
    jgarcia102066

    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.

    +
    0 Votes
    Tony Hopkinson

    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.

    +
    0 Votes
    Dillon.damien

    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")
    cboMovie.Items.Add(strMovieName)
    Next

    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.

    +
    0 Votes
    jgarcia102066

    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:

    cboMovie.SelectedItem.ToString

    Good Luck.

    +
    0 Votes
    Dillon.damien

    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"

    Thanks.

    +
    0 Votes
    jgarcia102066

    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

    [YOUR POPULATE METHOD HERE]

    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.

    +
    0 Votes
    Dillon.damien

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

    +
    0 Votes
    Tony Hopkinson

    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.

  • +
    0 Votes
    jgarcia102066

    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.

    +
    0 Votes
    Tony Hopkinson

    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.

    +
    0 Votes
    Dillon.damien

    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")
    cboMovie.Items.Add(strMovieName)
    Next

    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.

    +
    0 Votes
    jgarcia102066

    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:

    cboMovie.SelectedItem.ToString

    Good Luck.

    +
    0 Votes
    Dillon.damien

    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"

    Thanks.

    +
    0 Votes
    jgarcia102066

    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

    [YOUR POPULATE METHOD HERE]

    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.

    +
    0 Votes
    Dillon.damien

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

    +
    0 Votes
    Tony Hopkinson

    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.