DropDownList、ListItems 和 SelectedItem

发布于 2024-07-19 21:34:47 字数 476 浏览 6 评论 0原文

另请参阅:下拉菜单填充相同的列表项

经过一天的跟踪和调试,我终于发现用相同的项目集合填充 3 个 DropDownList 会导致最后选定的项目出现在所有 3 个列表中。

这似乎是由于项目的“选择性”是该项目的属性,而不是列表的属性。

但是,这似乎仅在以编程方式选择项目时出现。

编辑#2:每个人似乎都回答了错误的问题:以下是令我困惑的内容!

当用户通过控件选择项目时,应用程序似乎按预期工作。 -- 在 Web 界面中选择 3 个不同的项目会导致在数据库中输入正确的 3 个不同的项目。

谁能解释为什么会这样?

编辑:我要问的问题是为什么它在浏览器中起作用?

See also: Dropdowns filled with same list item

After a day of tracing and debugging, I finally figured out that populating 3 DropDownLists with the same collection of items causes the last selected item to appear in all 3 lists.

This appears to be due to the 'selectedness' of an item being a property of the item, rather than a property of the list.

However, this appears to be the case only when an item is selected programmatically.

edit #2: As everyone seems to be answering the wrong question: The following is what is confusing me!

The application appears to work as intended when the user selects items via the control. -- Selecting 3 different items in the Web interface results in the correct 3 different items being entered in the DB.

Can anyone explain why this is the case?

EDIT: The question I am asking is why does it work at all in the browser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-07-26 21:34:47

这与 BindingContext 和集合有关。 您需要复制该集合并绑定到这些副本。 我认为这样做是为了使母子视图更容易,您可以根据所选的母元素获取正确的子元素。

如果您的集合是 DataTable,则将下拉列表绑定到它将会使用它的 DefaultView。 为了避免这种情况,请将表打包在数据视图中;

obj.DataSource = new DataView(dataTable, "", "", DataViewRowState.CurrentRows);

This has to do with BindingContext and the collection. You need to make copies of the collection and bind to those. I think this was done to make mother-child views easier, where you can get the correct child element, based on the selected mother element.

If your collection is a DataTable, binding a dropdownlist to it will use it's DefaultView. To avoid this, package the table in dataviews;

obj.DataSource = new DataView(dataTable, "", "", DataViewRowState.CurrentRows);

心意如水 2024-07-26 21:34:47

这是因为您将相同的 ListItem 添加到两个不同的 DropDownList 控件。 每个 DropDownList 中显示的对象是相同的,因此更改该对象的值将反映在两个 DropDownList 中。

将下拉菜单视为查看对象集合的一种方式。 您点击下拉菜单并告诉它更改其选择的值实际上只会导致它循环遍历其集合中的项目并更改其选择值。 如果您对两个下拉列表使用相同的对象集合,则更改集合中的值将导致两个下拉列表看到相同的更改。

这就像房子里有两扇窗户,都可以看到同一个狗屋。 如果你告诉一个从 A 窗往外看的人去把狗屋漆成蓝色,即使你没有告诉从 B 窗往外看的人把“他的”狗屋漆成蓝色,他仍然会看到一只蓝色的狗房子。

It's because you're adding the same ListItem to two different DropDownList controls. The object that is being displayed in each of them is the same so changing the value of that object will be reflected in both DropDownLists.

Think of the dropdowns as being just a way of looking at a collection of objects. You poking the dropdown and telling it to change it's selected value really just results in it looping through the items in it's collection and changing their select value. If you use the same collection of objects for both drop downs, changing the values in the collection will result in both drop downs seeing the same change.

It's like having two windows in a house that both look out on the same dog house. If you were to tell a person looking out of window A to go and paint the dog house blue, even though you didn't tell the person looking out window B to paint "his" dog house, he's still going to see a blue dog house.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文