简体   繁体   English

在XML中将DataTextField设置为字符串后,DropDownList重复

[英]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: 我正在将XML读入C#的数据集中,然后使用在XML项中找到的ID号填充DropDownList ,如下所示:

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. 但是,这似乎在我的DropDownList创建了我不想要的重复项。

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? 是否可以将下拉菜单中的最后一项作为“新ID”之类的名称,当用户选择该下拉菜单时,该下拉菜单将被一个TextBox代替,他/她可以在其中输入新的(唯一的)ID?

For the first part of your question, 对于您的问题的第一部分,
do idDropDownList.Clear() before DataBind() DataBind()之前执行idDropDownList.Clear() DataBind()

For the second - there are many options 第二个-有很多选择
For example: 例如:
1. Append one more row to DataSet before Databind(); 1.在Databind()之前向DataSet追加一行;
2. have a hidden TextBox 2.有一个隐藏的TextBox
3. Handle SelectedIndexChanged event: hide DropDownList and show TextBox 3.处理SelectedIndexChanged事件:隐藏DropDownList并显示TextBox
4. Handle LostFocus event of TextBox, perform databind and switch visibility of controls back. 4.处理TextBox的LostFocus事件,执行数据绑定并将控件的可见性切换回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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