Garth Marenghi
Garth Marenghi

Reputation: 2037

Setting selected value of dropdownlist causes other dropdownlists to also get that selected value

I have several dropdownlists that I want to fill with drivers. The aspx is as follows:

<asp:DropDownList ID="DriversPolePositionDropDownList" runat="server" />
<br />
<asp:DropDownList ID="Drivers2ndPositionDropDownList" runat="server" />
<br />
<asp:DropDownList ID="Drivers3rdPositionDropDownList" runat="server" />

etc.

The problem is that after each dropdownlist has been filled with drivers, and I want to set the selected item of one dropdownlist it does so in all the dropdownlists. The dropdownlist getting filled with drivers is a separate step than selecting the value for the ddl's to display.

void AddDriver(string driverName, int driverId)
{
   ListItem item = new ListItem(driverName, driverId.ToString());
   AddItemToDropDownLists(item);
}

and then

string DriverPolePosition
{
   get { return DriversPolePositionDropDownList.SelectedValue; }
   set { DriversPolePositionDropDownList.SelectedValue = value; }
}

The code to set the selected value (it's only one dropdownlist I try to have the value of selected):

    predictionDetailsView.DriverPolePosition = 
response.SelectedDrivers[(int)DriverPositions.PolePosition];

How can I make it so that each dropdownlist can have its own selected value?

Upvotes: 1

Views: 728

Answers (1)

Garth Marenghi
Garth Marenghi

Reputation: 2037

Right. So the problem was this piece of code:

AddItemToDropDownLists(item);

In the AddItemToDropDownLists method I gave all my dropdownlists the same list item. This makes them behave the same. The solution was to make a new ListItem for every dropdownlist.

Upvotes: 2

Related Questions