Duplicating DropDownListItems Without Looping

2019-05-30 07:51发布

问题:

This works like a charm... loading DropDownList2 with all the items from DropDownList1 without looping:

DropDownList2.DataSource = DropDownList1.Items;
DropDownList2.DataBind();

But, the data from the item text of DropDownList1 is copied into both the text and value fields of DropDownList2. Is there anyway to get both the text and the value fields to populate properly?

回答1:

Does this work?

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


回答2:

Try setting the DataValueField and DataTextField properties to "Value" and "Text" respectively. That should work.