469,920 Members | 2,331 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

Listbox dropdown SelectedIndex resets after postback

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.CloseConne ction)
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.CloseConne ction)
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

Apr 19 '06 #1
3 4262
I looked at your SQL query and i'm wondering if sub_category_key is unique
for each of the items you are returning in the GetSubResearchData function,
if not (sa your query suggests) and you are binding the sub_category_key to
the value field of the second dropdown then you actually have just one item
from the dropdown's point of view .

look at the HTML source for the second dropdown and make sure that the
values differ for each of the items.

let me know if this helps
"devNorway" <no******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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.CloseConne ction)
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.CloseConne ction)
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

Apr 19 '06 #2
Thanks for your reply:)

I noticed this also but since im trying only to retrieve the
selectedIndex this shouldnt be a problem?

Im really not using the Values of the second dropdwon for anything..

Apr 20 '06 #3
Found the soution thanks to Onwuka Emeka here. I tried adding a primary
key for each sub category and used these for the listbox values. Then I
could easily retrieve the correct selectedIndex.

Apr 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Irene | last post: by
3 posts views Thread by Stimp | last post: by
7 posts views Thread by Lit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.