Use:
int[] iMyList = Listbox.GetSelectedIndices();
//iMyList.length
//iMyList[0]
//Combo.items[iMyList[0]] - OR CLOSE
It is the array(list) of selected items indexes, which you can use to fetch the actual value or text.
Question
Hi I am using the following code to show data in list box and when a user selects items in it and presses a forward key(button) I take the selected items and show them in a another listbox, but Unfortunately I am getting empty string for selected values.
Code:-
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();
}
}
Solution
Use:
int[] iMyList = Listbox.GetSelectedIndices();
//iMyList.length
//iMyList[0]
//Combo.items[iMyList[0]] - OR CLOSE
It is the array(list) of selected items indexes, which you can use to fetch the actual value or text.