473,837 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IsPostBack They" 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.tex t 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.EventArg s) Handles MyBase.Load

If Not Page.IsPostBack Then
GetMainResearch Data()
GetSubResearchD ata(0)
lstMainCategory .Items.Insert(0 , New ListItem("Pleas e Select
Main Category", "None"))
lstMainCategory .SelectedIndex = 0
End If
End Sub

Private Sub lstMainCategory _SelectedIndexC hanged(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lstMainCategory .SelectedIndexC hanged
mainKey = lstMainCategory .SelectedIndex( )
GetSubResearchD ata(mainKey)
End Sub

Private Sub lstSubCategory_ SelectedIndexCh anged(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lstSubCategory. SelectedIndexCh anged
subKey = lstSubCategory. SelectedIndex() ' HERE THE VALUE IS ALWAYS
1
Session("subid" ) = subKey
End Sub
Private Sub GetMainResearch Data()
Dim sqlText As String = "SELECT main_category_k ey,
main_category_t itle FROM ntnu_main_categ ory ORDER BY
main_category_t itle"
Dim myCommand As SqlCommand = New SqlCommand(sqlT ext,
myConnection)

Try
myConnection.Op en()
myDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)
lstMainCategory .DataSource = myDataReader
lstMainCategory .DataBind()

Catch myException As Exception
Response.Write( "Feilmeldin g " & myException.ToS tring())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Cl ose()
myConnection.Cl ose()
End If
End Try
End Sub

Private Sub GetSubResearchD ata(ByVal mainCategory As Integer)

Dim sqlText As String = "SELECT sub_category_ke y,
sub_category_ti tle FROM ntnu_sub_catego ry WHERE
ntnu_sub_catego ry.sub_category _key = " & mainCategory & ""
Dim myCommand As SqlCommand = New SqlCommand(sqlT ext,
myConnection)
Try
myConnection.Op en()
myDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)
lstSubCategory. DataSource = myDataReader
lstSubCategory. DataBind()
lstSubCategory. Items.Insert(0, New ListItem("Pleas e Select
sub category", "None"))
lstSubCategory. SelectedIndex = 0

Catch myException As Exception
Response.Write( "Feilmeldin g " & myException.ToS tring())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Cl ose()
myConnection.Cl ose()
End If
End Try
End Sub

Apr 19 '06 #1
3 4431
I looked at your SQL query and i'm wondering if sub_category_ke y is unique
for each of the items you are returning in the GetSubResearchD ata function,
if not (sa your query suggests) and you are binding the sub_category_ke y 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.goo glegroups.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.IsPostBack They" 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.tex t 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.EventArg s) Handles MyBase.Load

If Not Page.IsPostBack Then
GetMainResearch Data()
GetSubResearchD ata(0)
lstMainCategory .Items.Insert(0 , New ListItem("Pleas e Select
Main Category", "None"))
lstMainCategory .SelectedIndex = 0
End If
End Sub

Private Sub lstMainCategory _SelectedIndexC hanged(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lstMainCategory .SelectedIndexC hanged
mainKey = lstMainCategory .SelectedIndex( )
GetSubResearchD ata(mainKey)
End Sub

Private Sub lstSubCategory_ SelectedIndexCh anged(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lstSubCategory. SelectedIndexCh anged
subKey = lstSubCategory. SelectedIndex() ' HERE THE VALUE IS ALWAYS
1
Session("subid" ) = subKey
End Sub
Private Sub GetMainResearch Data()
Dim sqlText As String = "SELECT main_category_k ey,
main_category_t itle FROM ntnu_main_categ ory ORDER BY
main_category_t itle"
Dim myCommand As SqlCommand = New SqlCommand(sqlT ext,
myConnection)

Try
myConnection.Op en()
myDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)
lstMainCategory .DataSource = myDataReader
lstMainCategory .DataBind()

Catch myException As Exception
Response.Write( "Feilmeldin g " & myException.ToS tring())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Cl ose()
myConnection.Cl ose()
End If
End Try
End Sub

Private Sub GetSubResearchD ata(ByVal mainCategory As Integer)

Dim sqlText As String = "SELECT sub_category_ke y,
sub_category_ti tle FROM ntnu_sub_catego ry WHERE
ntnu_sub_catego ry.sub_category _key = " & mainCategory & ""
Dim myCommand As SqlCommand = New SqlCommand(sqlT ext,
myConnection)
Try
myConnection.Op en()
myDataReader =
myCommand.Execu teReader(Comman dBehavior.Close Connection)
lstSubCategory. DataSource = myDataReader
lstSubCategory. DataBind()
lstSubCategory. Items.Insert(0, New ListItem("Pleas e Select
sub category", "None"))
lstSubCategory. SelectedIndex = 0

Catch myException As Exception
Response.Write( "Feilmeldin g " & myException.ToS tring())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Cl ose()
myConnection.Cl ose()
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
4051
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 scrolled back to the top, so the selected item is mostly no longer visible. How can I keep the position, so that the selected item is still shown?
1
2190
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 with no additional code. I just inherited from the exisiting ListBox class and put the control on a page. I put this control a blank page along with a button to perform a post back. I select an item in the list box and then click the button...
4
1865
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. The dropdown just maintains its default value (element 0).
3
6109
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 the list back to the database. First of all, I implemented the re-ordering through client-side javascript, which worked great (also it didn't require a postback every time an item was re-ordered). For testing purposes, I'm outputting the contents...
5
3352
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 lstPatientID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPatientID.SelectedIndexChanged txtDebug.Text &= lstPatientID.SelectedIndex.ToString txtDebug.Text &= lstPatientID.SelectedIndex.ToString
10
3311
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
2921
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
5636
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 browser the list box scroll position is at the top.
1
3445
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 foreach (Product p in manager.Products) { ASP.MasterDetailDropDown productMasterDetail; productMasterDetail = (ASP.MasterDetailDropDown)LoadControl("~/controls/MasterDetailDropDown.ascx"); ...
0
9685
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10886
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5674
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.