user5382234
user5382234

Reputation: 63

Bind Same Data To Multiple Drop Down Lists

I have 10 drop down lists on my page 5 are for employee names and they are labeled ddlemp1, ddlemp2, ddlemp3, ddlemp4, ddlemp and I have 5 drop down lists for level and they are labeled ddllvl1, ddllvl2, ddllvl3, ddllvl4, ddllvl5

Is it possible to bind the same items to all my drop down lists that are like ddlemp and bind a second set of choices to all drop down lists that are named like ddllvl?

And I am populating the drop down lists like so:

this.ddlemp1.Items.Insert(1, new ListItem("Fran", "14"));
this.ddlemp1.Items.Insert(2, new ListItem("George", "3")):

EDIT
Would a repeater work in my instance as I have one drop down list of each on a row in my table, and from my understanding of a repeater Each element I need to be "repeated" should fall under the same repetaer. So unless I could nest a ddllvl repeater inside my ddlemp repeater somehow?

<asp:Repeater ID="RepeateDropDownListSelections" runat="server">
<tr id="row6" runat="server">
  <td><asp:DropDownList ID="ddlemp6" runat="server" Height="16px" Width="278px"></asp:DropDownList></td>
  <td class="style2"><asp:DropDownList ID="ddllvl6" runat="server" Height="16px" Width="45px"></asp:DropDownList></td>
</tr>
</asp:Repeater>

Upvotes: 1

Views: 2572

Answers (1)

DavidG
DavidG

Reputation: 118987

Instead of adding the list items to your drop downs, instead add them to a separate list and bind that to each control. For example:

var employees = new List<ListItem>
{
    new ListItem("Fran", "14"),
    new ListItem("George", "3")
};

ddlemp1.DataSource = employees;
ddlemp1.DataBind();

ddlemp2.DataSource = employees;
ddlemp2.DataBind();

...

ddlemp5.DataSource = employees;
ddlemp5.DataBind();

Repeat the same for your other list.

Upvotes: 3

Related Questions