下拉菜单填充相同的列表项
我有一个 Gridview,其中有两个下拉列表模板字段。 我在运行时将它们绑定到相同的列表项。
li = new listitem ("1","1");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("2","2");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("3","3");
dl1.items.add(li);
dl2.items.add(li);
dl1.selectedvalue = "2";
dl2.selectedvalue = "3";
执行完上面之后,dl1 & dl2 都显示“3”作为选定值。 为什么?
我知道在绑定时使用 2 个不同列表项的解决方法,但我想知道为什么会发生上述情况?
I have a Gridview in which i have two templatefields of dropdownlist. I bound them on runtime with same list item.
li = new listitem ("1","1");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("2","2");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("3","3");
dl1.items.add(li);
dl2.items.add(li);
dl1.selectedvalue = "2";
dl2.selectedvalue = "3";
After executing above, dl1 & dl2 both show me "3" as selected value. Why?
I know the work around of using 2 different listitems while binding but i wanna know why the above happens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅查看代码的最后一部分:您有一个列表项,并且它出现在两个不同的列表中。 但它仍然是一个对象。 您如何期望一个对象的单个属性 (SelectedValue) 具有两个不同的值?
Looking at just the last part of the code: you've got a single list item, and it's appearing in two different lists. But it's still one object. How would you expect one object to have two different values for a single property (SelectedValue)?
ListItem 类有一个属性“Selected”,用于标记该项目是否被选中。 我还没有检查 DDL SelectedValue 属性来了解它的作用,但我的猜测是 ListItem.Selected 属性被设置为 true,并且由于您在两个下拉列表中使用相同的对象,因此它被标记在两者中都被“选中”。
我确信如果这是一个多选列表,“2”和“3”都会被标记为“选定”。
The ListItem class has a property "Selected" which marks if the item is selected. I haven't checked the DDL SelectedValue property to see what it does, but my guess is that the ListItem.Selected property is being set to true, and since you are using the same object in both drop-down lists, it is being marked as 'selected' in both.
I'm sure if this was a multi-select list, both "2" and "3" would be marked as 'selected'.
您必须实例化每个下拉列表的每个列表项。
编辑:
乔恩描述了我想要表达的意思。 您只有一个具有值的对象。 因此,不要期望每个下拉列表都有不同的值。
当您将 dl1 设置为“3”时,它们都将获得相同的值,因为两个下拉列表都引用同一对象!
You have to instantiate each list item for each drop down list.
Edit:
Jon described what I want to mean. You have only one object that has a value. So don't expect different values for each drop down list.
When you set dl1 to "3" then both of them will get the same value because both drop down lists reference to same object!
该列表项在两个下拉列表之间共享。 当您为其中一个下拉列表设置选定值时,它会将列表项设置为被选中。 由于列表项正在共享,因此在两个下拉列表中都选择了该列表项
the listitem is being shared among the two drop downs. when you set the selected value for one of the drop downs it sets the list item as being selected. since the listitem is being shared it's selected in both drop downs
我认为这将是一个参考与价值的问题。 我确信如果从同一个列表项添加 d1 和 d2 将指向内存中的同一位置...
I would think it would be a ref vs value problem. I am sure both d1 and d2 would be pointing to the same spot in memory if the are added from the same list item...