D
devNorway
I have been struggling with a problem for days now, and searched for
related problems and solutions but had no luck.
I have two dropdown listboxes where the first is populated in page load
and the second is populated based on the input in the first. The first
dropdown is inside a "If Not Page.IsPostBackThey" if loop. Both have
autopostback set to true.
The problem is that when the user makes a choice in the first dropdown
an event is triggered that will populate the second dropdown. This
works okay, I can acess the selectedindex inside the code behind so
that the second dropdown gets populated.
The problem is to retrieve the choice that is made in the second
dropdown. I try to save the selctedItem.text or the selectedIndex but
always this is set to 1.
I have seen that a lot of other programmers have had this problem where
the second dropdown gets binded a second time before the selected
values could get retrieved. This could normally get resolved by putting
the binding inside a if not page.ispostback loop, but not in this case
where i can not bind the second drop down after a choice is made in the
first dropdown.
Im wondering if I have a bug somewhere. Have tried Enabling Viewstate
to no luck.
Im adding some code here, any help would be appreciated.
-----
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
GetMainResearchData()
GetSubResearchData(0)
lstMainCategory.Items.Insert(0, New ListItem("Please Select
Main Category", "None"))
lstMainCategory.SelectedIndex = 0
End If
End Sub
Private Sub lstMainCategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstMainCategory.SelectedIndexChanged
mainKey = lstMainCategory.SelectedIndex()
GetSubResearchData(mainKey)
End Sub
Private Sub lstSubCategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstSubCategory.SelectedIndexChanged
subKey = lstSubCategory.SelectedIndex() ' HERE THE VALUE IS ALWAYS
1
Session("subid") = subKey
End Sub
Private Sub GetMainResearchData()
Dim sqlText As String = "SELECT main_category_key,
main_category_title FROM ntnu_main_category ORDER BY
main_category_title"
Dim myCommand As SqlCommand = New SqlCommand(sqlText,
myConnection)
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
lstMainCategory.DataSource = myDataReader
lstMainCategory.DataBind()
Catch myException As Exception
Response.Write("Feilmelding " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
myConnection.Close()
End If
End Try
End Sub
Private Sub GetSubResearchData(ByVal mainCategory As Integer)
Dim sqlText As String = "SELECT sub_category_key,
sub_category_title FROM ntnu_sub_category WHERE
ntnu_sub_category.sub_category_key = " & mainCategory & ""
Dim myCommand As SqlCommand = New SqlCommand(sqlText,
myConnection)
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
lstSubCategory.DataSource = myDataReader
lstSubCategory.DataBind()
lstSubCategory.Items.Insert(0, New ListItem("Please Select
sub category", "None"))
lstSubCategory.SelectedIndex = 0
Catch myException As Exception
Response.Write("Feilmelding " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
myConnection.Close()
End If
End Try
End Sub
related problems and solutions but had no luck.
I have two dropdown listboxes where the first is populated in page load
and the second is populated based on the input in the first. The first
dropdown is inside a "If Not Page.IsPostBackThey" if loop. Both have
autopostback set to true.
The problem is that when the user makes a choice in the first dropdown
an event is triggered that will populate the second dropdown. This
works okay, I can acess the selectedindex inside the code behind so
that the second dropdown gets populated.
The problem is to retrieve the choice that is made in the second
dropdown. I try to save the selctedItem.text or the selectedIndex but
always this is set to 1.
I have seen that a lot of other programmers have had this problem where
the second dropdown gets binded a second time before the selected
values could get retrieved. This could normally get resolved by putting
the binding inside a if not page.ispostback loop, but not in this case
where i can not bind the second drop down after a choice is made in the
first dropdown.
Im wondering if I have a bug somewhere. Have tried Enabling Viewstate
to no luck.
Im adding some code here, any help would be appreciated.
-----
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
GetMainResearchData()
GetSubResearchData(0)
lstMainCategory.Items.Insert(0, New ListItem("Please Select
Main Category", "None"))
lstMainCategory.SelectedIndex = 0
End If
End Sub
Private Sub lstMainCategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstMainCategory.SelectedIndexChanged
mainKey = lstMainCategory.SelectedIndex()
GetSubResearchData(mainKey)
End Sub
Private Sub lstSubCategory_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstSubCategory.SelectedIndexChanged
subKey = lstSubCategory.SelectedIndex() ' HERE THE VALUE IS ALWAYS
1
Session("subid") = subKey
End Sub
Private Sub GetMainResearchData()
Dim sqlText As String = "SELECT main_category_key,
main_category_title FROM ntnu_main_category ORDER BY
main_category_title"
Dim myCommand As SqlCommand = New SqlCommand(sqlText,
myConnection)
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
lstMainCategory.DataSource = myDataReader
lstMainCategory.DataBind()
Catch myException As Exception
Response.Write("Feilmelding " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
myConnection.Close()
End If
End Try
End Sub
Private Sub GetSubResearchData(ByVal mainCategory As Integer)
Dim sqlText As String = "SELECT sub_category_key,
sub_category_title FROM ntnu_sub_category WHERE
ntnu_sub_category.sub_category_key = " & mainCategory & ""
Dim myCommand As SqlCommand = New SqlCommand(sqlText,
myConnection)
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
lstSubCategory.DataSource = myDataReader
lstSubCategory.DataBind()
lstSubCategory.Items.Insert(0, New ListItem("Please Select
sub category", "None"))
lstSubCategory.SelectedIndex = 0
Catch myException As Exception
Response.Write("Feilmelding " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
myConnection.Close()
End If
End Try
End Sub