Linking 2 Drop Downs and Repeater Control

Discussion in 'ASP .Net' started by BryanS, Mar 16, 2005.

  1. BryanS

    BryanS Guest

    I am having trouble trying to link 2 drop down lists and
    a repeater control. What i want is 2 drop down lists, the
    first being a list of food categories. When a category is
    selected the second dropdown is populated with a list of
    the food belonging to the selected category (this part is
    working fine).

    Now I also have a repeater control on the page. When a
    food in the second dropdown is selected I want the
    nutritional data for it to be bound to the repeater. When
    the food is selected the selected value that is sent to
    the server always appears to be -1 (the place holder that
    appears first in the dropdown) and when the page reloads
    the food I chose is no longer selected.

    I am not sure what I am doing wrong that causes the
    selected value to get lost. I have included the code and
    would appreciate any help. Viewstate is enabled on all
    controls.

    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    Dim conOSOR As SqlConnection
    Dim cmdSelectFoodCategories As SqlCommand
    Dim dtrFoodCategories As SqlDataReader

    conOSOR = New SqlConnection(AppSettings
    ("connString"))
    conOSOR.Open()

    cmdSelectFoodCategories = New SqlCommand
    ("SELECT FC_ID,FC_Category_Description FROM FoodCategories
    ORDER BY FC_Category_Description", conOSOR)

    dtrFoodCategories =
    cmdSelectFoodCategories.ExecuteReader()
    DropFoodCategories.DataSource =
    dtrFoodCategories
    DropFoodCategories.DataTextField
    = "FC_Category_Description"
    DropFoodCategories.DataValueField = "FC_ID"
    DropFoodCategories.DataBind()
    DropFoodCategories.Items.Insert(0, New ListItem
    ("-- select category --", -1))
    dtrFoodCategories.Close()
    conOSOR.Close()
    End If
    End Sub

    Sub DropFoodCategories_SelectedIndexChanged(ByVal
    sender As System.Object, ByVal e As System.EventArgs)
    Handles DropFoodList.SelectedIndexChanged
    Dim conOSOR As SqlConnection
    Dim cmdSelectFoodList As SqlCommand
    Dim dtrFoodList As SqlDataReader

    conOSOR = New SqlConnection(AppSettings
    ("connString"))
    conOSOR.Open()

    cmdSelectFoodList = New SqlCommand("SELECT
    F_ID,F_Name FROM Food WHERE F_Category_ID = " &
    DropFoodCategories.SelectedValue & " ORDER BY F_Name ",
    conOSOR)

    dtrFoodList = cmdSelectFoodList.ExecuteReader()
    DropFoodList.DataSource = dtrFoodList
    DropFoodList.DataTextField = "F_Name"
    DropFoodList.DataValueField = "F_ID"
    DropFoodList.DataBind()
    DropFoodList.Items.Insert(0, New ListItem("--
    select food --", -1))
    dtrFoodList.Close()
    conOSOR.Close()
    End Sub

    Sub DropFoodList_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    DropFoodList.SelectedIndexChanged
    Dim conOSOR As SqlConnection
    Dim cmdSelectFoodData As SqlCommand
    Dim dtrFoodData As SqlDataReader
    Dim itemID As String

    conOSOR = New SqlConnection(AppSettings
    ("connString"))
    conOSOR.Open()

    cmdSelectFoodData = New SqlCommand("SELECT * FROM
    FOOD WHERE F_ID = " & DropFoodList.SelectedValue & " ",
    conOSOR)

    dtrFoodData = cmdSelectFoodData.ExecuteReader()
    rptFoodData.DataSource = dtrFoodData
    rptFoodData.DataBind()
    dtrFoodData.Close()
    conOSOR.Close()

    End Sub
     
    BryanS, Mar 16, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davmagic .Com

    Need Script For Drop Downs

    Davmagic .Com, Oct 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    473
    Davmagic .Com
    Oct 14, 2004
  2. JohnZing

    list of drop downs

    JohnZing, Jan 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    425
    JohnZing
    Jan 30, 2005
  3. jaysch
    Replies:
    0
    Views:
    1,275
    jaysch
    Apr 30, 2011
  4. Leah
    Replies:
    1
    Views:
    218
    Lev Burov
    Sep 23, 2004
  5. Colin Walls

    Ajax and cascading drop downs

    Colin Walls, Jul 7, 2005, in forum: Javascript
    Replies:
    5
    Views:
    162
    Colin Walls
    Jul 7, 2005
Loading...

Share This Page