C#, 3 comboboxes same ListItem[] - why does first 2 change on 3rd's selected index ?

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,566
Reaction score
3,437
the actual question is (since we don't have combo boxes in asp.net but rather drop down lists...hehe): if i have more than one dropdownlist with the same listitem collection and i change the second or third (and so on) 's selected index the previous dropdownlists before them also changes to their selected index for some odd reason - maybe i just need more coffee and an actual brake over time spent programming today ;)
...


this results in A, B and C having "d" as their selected item - :confused::eek::mad: not sure why, but no where i actually have them all on the same reference as in A=B=C . I guess i'm missing the theory on why of this somewhere a long the line :p

in the pageload for testing
Code:
ListItem[] items = new ListItem[]
                               {
                                   new ListItem("a", "a"),  //0
                                   new ListItem("b", "b"),
                                   new ListItem("c", "c"),
                                   new ListItem("d", "d"),  //3
                               };

        A.Items.AddRange(items);
        B.Items.AddRange(items);
        C.Items.AddRange(items);

        A.SelectedIndex = 1;
        B.SelectedIndex = 2;
        C.SelectedIndex = 3;

source for the aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="A" runat="server">
        </asp:DropDownList><br />
        <br />
        <asp:DropDownList ID="B" runat="server">
        </asp:DropDownList><br />
        <br />
        <asp:DropDownList ID="C" runat="server">
        </asp:DropDownList></div>
    </form>
</body>
</html>
 
Last edited:
each list item has a "selected" property.

Since you are creating one array of items and adding this to each drop down's item collection, you are "binding" to the same objects. You select item D in Dropdown C, D.Selected becomes true. Since drop down A and B are also pointing to item D, they also select D (rendering your first two .SelectedIndex = useless)

Create new Item objects for each dropdown.

Code:
//Create collection
ListItem[] items = new ListItem[]
{
    new ListItem("a", "a"),
    new ListItem("b", "b"),
    new ListItem("c", "c"),
    new ListItem("d", "d"),
};

//Iterate through collection and add new instances of ListItem objects to Dropdown Items collection
foreach (ListItem item in items) 
{
    A.Items.Add(new ListItem(item.Text, item.Value));
    B.Items.Add(new ListItem(item.Text, item.Value));
    C.Items.Add(new ListItem(item.Text, item.Value));
}

//Initialize selected index
A.SelectedIndex = 1;
B.SelectedIndex = 2;
C.SelectedIndex = 3;

Quick and dirty :P Obviously there are much better ways of coding the Add part like creating a method which takes a DropDownList object and your ListItem array or any dictionary type collection
 
Last edited:
each list item has a "selected" property.

Since you are creating one array of items and adding this to each drop down's item collection, you are "binding" to the same objects. You select item D in Dropdown C, D.Selected becomes true. Since drop down A and B are also pointing to item D, they also select D (rendering your first two .SelectedIndex = useless)

Create new Item objects for each dropdown.

Code:
//Create collection
ListItem[] items = new ListItem[]
{
    new ListItem("a", "a"),
    new ListItem("b", "b"),
    new ListItem("c", "c"),
    new ListItem("d", "d"),
};

//Iterate through collection and add new instances of ListItem objects to Dropdown Items collection
foreach (ListItem item in items) 
{
    A.Items.Add(new ListItem(item.Text, item.Value));
    B.Items.Add(new ListItem(item.Text, item.Value));
    C.Items.Add(new ListItem(item.Text, item.Value));
}

//Initialize selected index
A.SelectedIndex = 1;
B.SelectedIndex = 2;
C.SelectedIndex = 3;

Quick and dirty :P Obviously there are much better ways of coding the Add part like creating a method which takes a DropDownList object and your ListItem array or any dictionary type collection

cool thnx, i'll be using a 'lookupitem' class array and then just add them as ListItem(<name>,<id>) instead of the one listitem array :)
 
Couldn't you have just cloned the ListItem[] for each instance it is to be used?

For instance:
Code:
ListItem[] items = new ListItem[]
                               {
                                   new ListItem("a", "a"),  //0
                                   new ListItem("b", "b"),
                                   new ListItem("c", "c"),
                                   new ListItem("d", "d"),  //3
                               };

        [COLOR="Red"][b]A.Items = items.Clone();
        B.Items = items.Clone();
        C.Items = items.Clone();[/b][/COLOR]

        A.SelectedIndex = 1;
        B.SelectedIndex = 2;
        C.SelectedIndex = 3;
 
Top
Sign up to the MyBroadband newsletter
X