Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > When I set the DropDownList.SelectedIndex of ONE DropDownList they ALL change!

Reply
Thread Tools

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

 
 
S_K
Guest
Posts: n/a
 
      11-13-2007
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

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      11-14-2007
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.

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


"S_K" <> wrote in message
news: ups.com...
> 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
>



 
Reply With Quote
 
 
 
 
S_K
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 2:52 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> 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.
>
> --
> Eliyahu Goldin,
> Software Developer
> Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
>
> "S_K" <steve_kers...@yahoo.com> wrote in message
>
> news: ups.com...
>
>
>
> > 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- Hide quoted text -

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

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      11-14-2007
I mean every ddl should have it's own item collection, not shared with the
other ddls.

How do you populate the ddls?

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


"S_K" <> wrote in message
news: ps.com...
> On Nov 14, 2:52 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
>> 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.
>>
>> --
>> Eliyahu Goldin,
>> Software Developer
>> Microsoft MVP
>> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
>>
>> "S_K" <steve_kers...@yahoo.com> wrote in message
>>
>> news: ups.com...
>>
>>
>>
>> > 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- Hide quoted text -

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



 
Reply With Quote
 
S_K
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 8:29 am, S_K <steve_kers...@yahoo.com> wrote:
> On Nov 14, 2:52 am, "Eliyahu Goldin"
>
>
>
>
>
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> > 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.

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

>
> > "S_K" <steve_kers...@yahoo.com> wrote in message

>
> >news: oups.com...

>
> > > 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- Hide quoted text -

>
> > - 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- Hide quoted text -
>
> - 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

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      11-14-2007
> 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.

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


"S_K" <> wrote in message
news: oups.com...
> On Nov 14, 8:29 am, S_K <steve_kers...@yahoo.com> wrote:
>> On Nov 14, 2:52 am, "Eliyahu Goldin"
>>
>>
>>
>>
>>
>> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
>> > 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.

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

>>
>> > "S_K" <steve_kers...@yahoo.com> wrote in message

>>
>> >news: oups.com...

>>
>> > > 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- Hide quoted text -

>>
>> > - 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- Hide quoted text -
>>
>> - 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Email my List of 50,000 Genuine Opportunity Seekers for $17 One-TimePayment and They are All Yours To Keep Forever RG HTML 0 09-05-2008 03:17 PM
Do all MCSE's think they know more than all other MCSE's? Red Swingline Stapler MCSE 8 12-09-2006 03:39 AM
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
MCSE exam # 70-216. They they show you the score?? Jack Black Microsoft Certification 1 12-09-2004 11:54 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57