473,383 Members | 1,868 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,383 software developers and data experts.

Problem with setting DropDownList selectedindex on multiple controls

Hello, I hope this is where I should post this.
I have a problem with this code that I can't figure out...seems to be a bug in the dropdownlist control.
I am trying to set multiple dropdownlists to match retrieved data. When I set the first, everything is fine. Then I set the second and the first gets set to the second index. Can you see anything wrong with this code? Thanks

If DR.HasRows Then
Do While DR.Read()
Dim name As String = DR.Item("position").ToString
Dim Type As String = DR.Item("Type").ToString
Dim Value As String = DR.Item("PositionID").ToString

Select Case Type.ToUpper
Case "ACMC"
Dim ACMCselectedItem As New System.Web.UI.WebControls.ListItem
ACMCselectedItem = cboACMC.Items.FindByValue(Value)
If Not (ACMCselectedItem Is Nothing) Then
cboACMC.SelectedIndex = cboACMC.Items.IndexOf(ACMCselectedItem)
End If

Case "CASAG"
Dim CASAGselectedItem As New System.Web.UI.WebControls.ListItem
CASAGselectedItem = cboCASAG.Items.FindByValue(Value)
If Not (CASAGselectedItem Is Nothing) Then

cboCASAG.SelectedIndex = cboCASAG.Items.IndexOf(CASAGselectedItem) <<<--- (this step resets cboACMC.SelectedIndex as well!!! I have watched it happen in debug.)

End If
Case "DESK"
Dim item As New System.Web.UI.WebControls.ListItem
item.Text = name
item.Value = DR.Item("PositionID").ToString
lstDeskOffices.Items.Add(item)
End Select
Loop
End If

thanks for any help you can give me..

--------------------------------
From: David Austin

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>04vPTV01T0mbVXp08ODeUw==</Id>
Nov 18 '05 #1
2 1739
It's not a bug. This behavior is by design - poorly understood al beit.

The work around is to have separate items in the list. Here, you have two
dropdownlist boxes pointing to the same object.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"David Austin via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Hello, I hope this is where I should post this.
I have a problem with this code that I can't figure out...seems to be a
bug in the dropdownlist control.
I am trying to set multiple dropdownlists to match retrieved data. When I
set the first, everything is fine. Then I set the second and the first
gets set to the second index. Can you see anything wrong with this code?
Thanks

If DR.HasRows Then
Do While DR.Read()
Dim name As String = DR.Item("position").ToString
Dim Type As String = DR.Item("Type").ToString
Dim Value As String = DR.Item("PositionID").ToString

Select Case Type.ToUpper
Case "ACMC"
Dim ACMCselectedItem As New
System.Web.UI.WebControls.ListItem
ACMCselectedItem =
cboACMC.Items.FindByValue(Value)
If Not (ACMCselectedItem Is Nothing) Then
cboACMC.SelectedIndex =
cboACMC.Items.IndexOf(ACMCselectedItem)
End If

Case "CASAG"
Dim CASAGselectedItem As New
System.Web.UI.WebControls.ListItem
CASAGselectedItem =
cboCASAG.Items.FindByValue(Value)
If Not (CASAGselectedItem Is Nothing) Then

cboCASAG.SelectedIndex =
cboCASAG.Items.IndexOf(CASAGselectedItem) <<<--- (this step resets
cboACMC.SelectedIndex as well!!! I have watched it happen in debug.)

End If
Case "DESK"
Dim item As New
System.Web.UI.WebControls.ListItem
item.Text = name
item.Value = DR.Item("PositionID").ToString
lstDeskOffices.Items.Add(item)
End Select
Loop
End If

thanks for any help you can give me..

--------------------------------
From: David Austin

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>04vPTV01T0mbVXp08ODeUw==</Id>

Nov 18 '05 #2
And to add to what has already been said, You should take care as to not to add the same item to Different DropdownList Boxes lik

Dim lstItem as New ListItem("One", "1"
ddlItem1.Items.Add(lstItem
ddlItem2.Items.Add(lstItem

Regards

Trevor
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ashish Sharma | last post by:
I have a DropDownList inside a DataList on a page. I want to set the selectedindex property of the DropDownList to some value. I bind the DropDownList on the ItemCreated Event of the DataList. ...
1
by: Ashish Sharma | last post by:
I have a DropDownList inside a DataList on a page. I want to set the selectedindex property of the DropDownList to some value. I bind the DropDownList on the ItemCreated Event of the DataList. ...
4
by: PSL | last post by:
Hi, I am going through the training C# For Programmers made by TestOut and like the presentation. The only problem is that this tutorial has a nastie bug that renders this software practically...
3
by: RSH | last post by:
Hi, I am having this very bizarre occurance with 4 dropdown lists on my ASP .Net page. Each control has its own unique id. When the user selects a value from each of the dropdownlists it is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.