【问题标题】:Get sequence of selection of items in listbox control in c#在c#中获取列表框控件中的项目选择序列
【发布时间】:2010-03-10 13:31:17
【问题描述】:

我有列表框控件(ASP.NET 控件在 aspx 页面上,语言 C#)。它有整数集合作为项目:

100, 200, 300, 400, 500, 600, 700. 好吗?

我在运行时随机选择列表项。如:

200, 500, 100, 300. 好吗?

我想要列表集合中的这个选定列表序列。我怎样才能做到这一点 ?请指导我。

【问题讨论】:

    标签: c# asp.net controls listbox


    【解决方案1】:

    对 anishmarokey 的回答稍作修改。如果您想将 ListItem 实际存储在 List 集合中,而不仅仅是字符串:

     protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           List<ListItem> list = new List<ListItem>();
           list.Add(ListBox1.SelectedItem);
           string testValue = list[0].Value; // this is how you access a listitem in the List
        }
    

    如果您这样做,您可以使用实际的列表框项目,而不仅仅是选择的数字。如果您想在每次回发后将值保留在“列表”中,您可以将“列表”放入会话对象或缓存中。

    【讨论】:

    • 如果他们只是想收集字符串值,你的解决方案会起作用,但有点不清楚他们需要什么,所以我的只是涵盖了他们需要做的任何事情。
    【解决方案2】:

    在选定的索引更改中。你可以添加。

     protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
     List<ListItem> list = new List<ListItem>();
                    list.Add(ListBox1.SelectedValue.ToString());
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 2012-10-29
      相关资源
      最近更新 更多