Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / November 2007

Tip: Looking for answers? Try searching our database.

When I set the DropDownList.SelectedIndex of ONE DropDownList they ALL change!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
S_K - 13 Nov 2007 23:39 GMT
Hi,

I have a list of 6 DropDownList boxes, from DropDownList1 to
DropDownList6, and I'm changing the SelectedIndex of each using a
foreach loop as follows:

foreach(PaymentReqDisplay thispayment in listPaymentReqDisplay)
       {
         if ((string)thispayment.TaxType == "FIT")
           this.DropDownList1.SelectedIndex =
(int)thispayment.PaymentRequirementID - 1;
         if ((string)thispayment.TaxType == "FUI")
           this.DropDownList2.SelectedIndex =
(int)thispayment.PaymentRequirementID - 1;
         if ((string)thispayment.TaxType == "SIT")
           this.DropDownList3.SelectedIndex =
(int)thispayment.PaymentRequirementID - 1;
         if ((string)thispayment.TaxType == "SUI")
           this.DropDownList4.SelectedIndex =
(int)thispayment.PaymentRequirementID - 1;
         if ((string)thispayment.TaxType == "ETF")
           this.DropDownList5.SelectedIndex =
(int)thispayment.PaymentRequirementID - 1;
         if ((string)thispayment.TaxType == "SDI")
           this.DropDownList6.SelectedIndex =
(int)thispayment.PaymentRequirementID - 1;
       }

The problem is that when I change, say, DropDownList4.SelectedIndex =
3; every other DropDownList's SelectedIndex changes as well!?

What am I doing wrong?

Thanks much for your help.
Steve
Eliyahu Goldin - 14 Nov 2007 09:52 GMT
Do all ddls share the same items?

When you set the SelectedIndex property, it actually sets the Selected
property of the item you select. Thus the same item  becomes selected in all
ddls it is participating.

The solution is to replicate the item collection for every single ddl.

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> Hi,
>
[quoted text clipped - 31 lines]
> Thanks much for your help.
> Steve
S_K - 14 Nov 2007 15:29 GMT
On Nov 14, 2:52 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> Do all ddls share the same items?
>
[quoted text clipped - 46 lines]
>
> - Show quoted text -

So you are saying that by setting the SelectIndex of (say) the first
ddl ALL of the ddls in that group get set along with it?
So how do I replicate the item collection for every single ddl? I
don't understand what you mean. Could you give me a code example?

Thanks so much for your help.
Steve
Eliyahu Goldin - 14 Nov 2007 16:16 GMT
I mean every ddl should have it's own item collection, not shared with the
other ddls.

How do you populate the ddls?

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> On Nov 14, 2:52 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
[quoted text clipped - 58 lines]
> Thanks so much for your help.
> Steve
S_K - 14 Nov 2007 16:32 GMT
> On Nov 14, 2:52 am, "Eliyahu Goldin"
>
[quoted text clipped - 63 lines]
>
> - Show quoted text -

I FOUND THE PROBLEM!!!

This is what happend.
In my code I called a method to fill the Items value for each
DropDownList using a ListItem as in the code:

for (int i = 0; i < listPaymentReq.Count; i++)
     {
       ListItem li = new
ListItem(listPaymentReq[i].Payment_Req_Code.ToString(),
         listPaymentReq[i].Payment_Req_ID.ToString());

       DropDownList1.Items.Insert(i, li);
       DropDownList2.Items.Insert(i, li);
       DropDownList3.Items.Insert(i, li);
.
.
.     }

Note that the "li" is a pointer and I'm inserting the SAME Items value
into each ddl! This apparently means that the SelectedIndex for each
ddl is also a pointer pointing to the same location as well.

You learn something new everyday.

thanks for the help.

Steve
Eliyahu Goldin - 14 Nov 2007 16:51 GMT
>  This apparently means that the SelectedIndex for each
> ddl is also a pointer pointing to the same location as well.

SelectedIndex is not a pointer, it is a property and when you set or get it,
some code runs.

When you "set" it, asp.net goes to the Items collection and mark the item as
"Selected" be setting li.Selected = true.

When you "get" the SelectedIndex on another ddl, asp.net goes to the Items
collection, finds the item with Selected=true and returns it's index. This
causes the effect you observed and successfully fixed.

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

>> On Nov 14, 2:52 am, "Eliyahu Goldin"
>>
[quoted text clipped - 94 lines]
>
> Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.