B
BryanS
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
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