简体   繁体   中英

DropDownList duplicates after setting DataTextField to string in XML

I am Reading an XML into a data set in C# and then I am populating a DropDownList with id numbers found in items in the XML, like this:

DataSet dataSet = new DataSet();
dataSet.ReadXml(myPathToXML);

Then populating:

idDropDownList.DataSource = dataSet;
idDropDownList.DataTextField = "id";
idDropDownList.DataBind();

However, this seems to create duplicates in my DropDownList which I don't want.

EDIT:

Would it be possible to have the last item in the dropdown as something like "New ID" and when the user selects that, the dropdown is replaced by a TextBox where he/she can enter a new (unique) ID?

For the first part of your question,
do idDropDownList.Clear() before DataBind()

For the second - there are many options
For example:
1. Append one more row to DataSet before Databind();
2. have a hidden TextBox
3. Handle SelectedIndexChanged event: hide DropDownList and show TextBox
4. Handle LostFocus event of TextBox, perform databind and switch visibility of controls back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM