ASP.NET页面上所有HtmlSelect中的SelectedIndex属性都是一样的

发布于 2024-09-08 23:03:01 字数 638 浏览 5 评论 0原文

我有一些 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 技术交流群。

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

发布评论

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

评论(1

匿名的好友 2024-09-15 23:03:01

您将所有选择控件绑定到同一项目列表。当您在选择控件上设置 SelectedIndex 时,它是您设置为选定的项目。由于所有选择控件都引用相同的项目列表,因此在其中一个控件上设置 SelectedIndex 属性会将其设置在所有其他控件上。

比较以下两个片段。第一个与您绑定到同一列表的情况相同:

        var _listItems = new List<ListItem>{ new ListItem("Item1"), new ListItem("Item2"), new ListItem("Item3")};
        for (int controlNumber = 1; controlNumber < 4; controlNumber++) {
            var HtmlSelectControl = ((HtmlSelect) this.FindControl(String.Format("Select{0}", controlNumber)));
            HtmlSelectControl.Items.AddRange(_listItems.ToArray());
            HtmlSelectControl.SelectedIndex = controlNumber - 1;
        }

第二个是您为每个下拉列表创建单独的列表。这是你需要做的:

        for (int controlNumber = 1; controlNumber < 4; controlNumber++) {
            var _listItems = new List<ListItem>{ new ListItem("Item1"), new ListItem("Item2"), new ListItem("Item3")};
            var HtmlSelectControl = ((HtmlSelect) this.FindControl(String.Format("Select{0}", controlNumber)));
            HtmlSelectControl.Items.AddRange(_listItems.ToArray());
            HtmlSelectControl.SelectedIndex = controlNumber - 1;
        }

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:

        var _listItems = new List<ListItem>{ new ListItem("Item1"), new ListItem("Item2"), new ListItem("Item3")};
        for (int controlNumber = 1; controlNumber < 4; controlNumber++) {
            var HtmlSelectControl = ((HtmlSelect) this.FindControl(String.Format("Select{0}", controlNumber)));
            HtmlSelectControl.Items.AddRange(_listItems.ToArray());
            HtmlSelectControl.SelectedIndex = controlNumber - 1;
        }

The second one is where you create a seperate list for each dropdown. This is what you need to do:

        for (int controlNumber = 1; controlNumber < 4; controlNumber++) {
            var _listItems = new List<ListItem>{ new ListItem("Item1"), new ListItem("Item2"), new ListItem("Item3")};
            var HtmlSelectControl = ((HtmlSelect) this.FindControl(String.Format("Select{0}", controlNumber)));
            HtmlSelectControl.Items.AddRange(_listItems.ToArray());
            HtmlSelectControl.SelectedIndex = controlNumber - 1;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文