【问题标题】:ListBox selected Items list not shownListBox 选定的项目列表未显示
【发布时间】:2014-04-17 10:57:44
【问题描述】:

您好,我正在使用以下代码在列表框中显示数据,当用户选择其中的项目并按下前进键(按钮)时,我会将所选项目显示在另一个列表框中,但不幸的是我得到了所选值的空字符串。

代码:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class AssignRolesToUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<int,string> roles = new Dictionary<int,string>();
        roles.Add(1,"Manager");
        roles.Add(2,"ASM");
        roles.Add(3,"SM");
        roles.Add(4,"RM");
        Dictionary<int, string> Users = new Dictionary<int, string>();
        Users.Add(1,"Ganesh");
        Users.Add(2,"ABC");
        Users.Add(3,"XYZ");
        Users.Add(4,"DEF");
        Users.Add(5,"MNO");
        ListBox1.SelectionMode = ListSelectionMode.Multiple;
        ListBox1.DataSource = Users.Values;
        ListBox1.DataBind();
        ComboBox1.DataSource = roles.Values;
        ComboBox1.SelectedIndex = 0;
        ComboBox1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // GetSelectedIndices
        List<string> AU = new List<string>();
        foreach (ListItem item in ListBox1.Items)
        {
            if (item.Selected)
            {
                AU.Add(ListBox1.SelectedValue);
            }  
        }
        ListBox2.DataSource = AU;
        ListBox2.DataBind();
    }
}

【问题讨论】:

    标签: listbox selected


    【解决方案1】:

    用途:

    int[] iMyList = Listbox.GetSelectedIndices();
    //iMyList.length
    //iMyList[0]
    //Combo.items[iMyList[0]] -  OR CLOSE
    

    它是所选项目索引的数组(列表),您可以使用它来获取实际值或文本。

    【讨论】:

    • 嗨 Anthony,我在属性中找不到任何 selecteditems 属性。我在 ASPX 页面上使用 ListBox。
    • 我可以使用这种技术吗:- 我想从代码隐藏的页面加载中填充 DDL 和 LISTBOX1 中的数据,之后当用户选择项目并按下按钮时,我将在客户端处理它,获取数据从 listbox1 并将选定的数据传递给 listbox2。我想我可以通过互联网找到。我不明白的是如何从服务器端获取填充到 listbox2 中的数据,或者如何在最后一次 savebtn 点击时将其传递到服务器端。(我希望我很清楚)。
    • 列表框上还有一个changeevent,您可以对其进行监控,并为您提供当前的更改/选择。等
    • 不,Anthony,这个想法是绝对减少到服务器的往返,这个应用程序的用户不耐烦,所以我需要确保他们想要对数据做的所有事情都不应该涉及到服务器的往返,相反,我让他们在浏览器中完成所有工作而无需往返,最后当他们单击保存按钮时,它应该保存在服务器上。
    • 酷。不要为此打折使用 Ajax。由于部分发布,您的应用往往会感觉到客户端。
    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2012-02-02
    • 1970-01-01
    相关资源
    最近更新 更多