ASP.NET页面上所有HtmlSelect中的SelectedIndex属性都是一样的
我有一些 HtmlSelect(a'la asp.net's DropDownList),其 ID 类似于 Select1、Select2、...、Select13。我创建静态项目列表:
for (int i = 0; i < tab.Length; i++)
_listItems[i] = (new ListItem { Text = tab[0, i], Value = tab[1, i], Selected=false });
然后为每个 HtmlSelect 控件分配该列表。分配新的 SelectedIndex 属性:
var HtmlSelectControl = ((HtmlSelect)this.FindControl(String.Format("Select{0}", controlNumber)));
HtmlSelectControl.Items.AddRange(_listItems);
HtmlSelectControl.SelectedIndex = controlNumber - 1;
问题是,当我设置 Select2 控件的 SelectedIndex 属性(例如 =1)时,Select1 控件具有相同的 SelectedIndex 属性(其索引 =0)。为什么 ?
I have some HtmlSelect (a'la asp.net's DropDownList) with ID's like Select1, Select2, ..., Select13. I create the static List of items:
for (int i = 0; i < tab.Length; i++)
_listItems[i] = (new ListItem { Text = tab[0, i], Value = tab[1, i], Selected=false });
then I assign that list for each HtmlSelect control & assign a new SelectedIndex property:
var HtmlSelectControl = ((HtmlSelect)this.FindControl(String.Format("Select{0}", controlNumber)));
HtmlSelectControl.Items.AddRange(_listItems);
HtmlSelectControl.SelectedIndex = controlNumber - 1;
The problem is, when I set the SelectedIndex property of the Select2 control (e.g. =1), the Select1 control has the same SelectedIndex property (which has that index =0). Why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将所有选择控件绑定到同一项目列表。当您在选择控件上设置 SelectedIndex 时,它是您设置为选定的项目。由于所有选择控件都引用相同的项目列表,因此在其中一个控件上设置 SelectedIndex 属性会将其设置在所有其他控件上。
比较以下两个片段。第一个与您绑定到同一列表的情况相同:
第二个是您为每个下拉列表创建单独的列表。这是你需要做的:
You are binding all of the select controls to the same list of items. It is the items that you are seting as selected when you set the SelectedIndex on the select control. Since all of the select controls have references to the same list of items, setting the SelectedIndex property on one of them sets it on all of the others.
Compare the following two snippets. The first one is the same as yours where you are binding to the same list:
The second one is where you create a seperate list for each dropdown. This is what you need to do: