473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4405
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Irene | last post by:
I'm developing a Web application where a user selects from a listbox which can have many items. The initial display only shows about 10 items. After a postback, the listbox is automatically...
1
by: chambersdon | last post by:
I have created a Web Custom Control that inherits from WebControls.ListBox but can't get it to retain the selectedIndex after a postback. As a test I created a very simple version of the control...
4
by: Simon Harvey | last post by:
Hi all, Am I being really stupid here: myDropDown.SelectedIndex = 2 I think this line should set the dropdown control's selected item to 2. But nothing seems to be happening on the page....
3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
5
by: david | last post by:
I have a question in the following. Any one could give me a help? Thanks David The dataset is declared and created at Class level. My source code is here: Private Sub...
10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
2
by: teo | last post by:
I have a Listbox, if I set EnableViewStarte = False the AutopostaBack fired by SelectedIndexChanged doesn't work. The 'SelectedIndexChanged' event should call
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.