Sponsored Ad

Monday, April 5, 2010

How to Get the List box Selected Items in C# | Loop Through List Box Selection

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;

       }

1 comments:

  1. lame
    selecteditem returns a collection of selected items

    ReplyDelete

Sponsored Ad

Website Update

Followers