开发者

How to copy items from one DropDownList to another

How can I copy items hardcoded from 开发者_运维技巧one dropdown box to another keeping the keys and values?

drpTypes.Items.Add(new ListItem("Tipos de Acções", "1"));
drpTypes.Items.Add(new ListItem("Tipos de Combustível", "2"));
drpTypes.Items.Add(new ListItem("Tipos de Condutor", "3"));

drpTypesCreateEdit.Items.AddRange(drpTypes.Items);


AddRange wants an array of ListItems. you can do it like this (C# 3+).

drpTypesCreateEdit.Items.AddRange(drpTypes.Items.OfType<ListItem>().ToArray()); 


Agree with Anthony's comment above.

However, since the selected ListItems will still refer to the same objects of the original DropDownList, there will be unintended side-effects when changing fields/properties.

For example:

drpTypes.Items.Add(new ListItem("Tipos de Acções", "1"));
drpTypes.Items.Add(new ListItem("Tipos de Combustível", "2"));
drpTypes.Items.Add(new ListItem("Tipos de Condutor", "3"));

drpTypesCreateEdit.Items.AddRange(drpTypes.Items);

drpTypes.SelectedValue = "2";
drpTypesCreateEdit.SelectedValue = "3";

Both drpTypes and drpTypesCreateEdit now have SelectedValue of "3", whereas that is clearly not the intent of the above code.

Instantiating new ListItem objects instead of just selecting the original object will fix this:

drpTypesCreateEdit.Items.AddRange(drpTypes.Items.Cast<ListItem>().Select(x => New ListItem(x.Text, x.SelectedValue)).ToArray();


I couldn't figure this out in VB.NET and it took me awhile to find this answer. Duplicating DropDownListItems Without Looping

DropDownList2.DataSource = DropDownList1.Items
DropDownList2.DataTextField = "Text"
DropDownList2.DataValueField = "Value"
DropDownList2.DataBind()

This will do a deep copy between two dropdownlists.

Perhaps an admin can remove this answer and link the two questions together.


This would be one of the easier ways..

drpTypes.Items.Add(new ListItem("Tipos de Acções", "1"));
drpTypes.Items.Add(new ListItem("Tipos de Combustível", "2"));
drpTypes.Items.Add(new ListItem("Tipos de Condutor", "3"));

foreach(ListItem li in drpTypes.Items)
{
    drpTypesCreateEdit.Items.Add(li);
}

Do you need something more elaborate?


This is also good for copying DropDownList items to another.

Example : it will copy all items from DropDownList1 to DropDownList2.

Items In DropDownList1 :

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="A" Value="1"></asp:ListItem>
    <asp:ListItem Text="B" Value="2"></asp:ListItem>
    <asp:ListItem Text="C" Value="3"></asp:ListItem>
    <asp:ListItem Text="D" Value="4"></asp:ListItem>
    <asp:ListItem Text="E" Value="5"></asp:ListItem>
</asp:DropDownList>

1- First Copy DropDownList1 to Array.

ListItem[] ListitemsArray = new ListItem[DropDownList1.Items.Count];
DropDownList1.Items.CopyTo(ListitemsArray, 0);

2- : Now Copy from Array to DropDownList2.

            foreach (ListItem liItems in ListitemsArray)
            {
                DropDownList2.Items.Add(liItems);
            }

This works for me!!!!!!!!!!!!!


I did it this way directly from one drop down to another cutting the need to copy to an array first.

  foreach(ListItem listItem in myDropDownList.Items)
    {
       myOtherDropDownList.Items.Add(listItem)
    }


I used like this

comboBox2.Items.AddRange(comboBox3.Items.OfType<string>().ToArray());


in vb i used

Dim cb As CheckBoxList=new CheckBoxList()
cb.Items.AddRange(CheckBoxList_Source.Items.Cast(Of ListItem)().Select(Function(f) New ListItem(f.Text, f.Value)).ToArray())

or

Dim cb As CheckBoxList=new CheckBoxList()
cb.DataSource = CheckBoxList_Source.Items
cb.DataBind()

but this bind Value with the Text instead the true value of original Items

these are the only way i found to clone elements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜