Listbox dropdown SelectedIndex resets after postback

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
 
O

Onwuka Emeka

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
 
D

devNorway

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..
 
D

devNorway

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top