您好我正在使用以下代码在列表框中显示数据,当用户选择其中的项目并按下前进键(按钮)时,我会将所选项目显示在另一个列表框中,但不幸的是我得到了选定值的空字符串。
代码: -
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();
}
}
答案 0 :(得分:0)
使用:
int[] iMyList = Listbox.GetSelectedIndices();
//iMyList.length
//iMyList[0]
//Combo.items[iMyList[0]] - OR CLOSE
它是所选项目索引的数组(列表),您可以使用它来获取实际值或文本。