This Sample post will guide you to get all selected values from a ASP.NET list box control. Just Pass the list box in the given function and it will return all selected values in comma separated format.
you can have selected values in different format or can use inside the loop.
/// <summary>
/// Gets the Selected Items from the Listbox
/// </summary>
/// <param name="lstBox"></param>
/// <returns></returns>
public static string sGetSelectedValues(ListBox lstBox)
{
string sSelected = "";
if (Convert.ToInt16(lstBox.SelectedIndex.ToString()) == -1)
return sSelected;
for (int i = 0; i < lstBox.Items.Count; i++)
{
if (lstBox.Items[i].Selected)
{
sSelected += lstBox.Items[i].Value + ",";
}
}
if (sSelected != "")
sSelected = sSelected.Substring(0, sSelected.Length - 1);
return sSelected;
}
lame
ReplyDeleteselecteditem returns a collection of selected items