Exforsys

Home arrow Technical Training arrow ASP.NET Training

ASP.NET List Controls

Page 4 of 4
Author : Exforsys Inc.     Published on: 26th Feb 2005    |   Last Updated on: 14th Mar 2011

ASP.NET Web Forms Controls

Ads

List Controls:

List controls provide the user with a number of options to choose from. A user has a choice to select a single option or list of options. The ListBox control provided by Asp.net controls collection. The listbox control has a collection property which is known as items. You can click on the property and populate the ListBox with the data and the value you desire. While populating the ListBox remember that the Text property of the ListBox is the text which will appear inside the it and the value is simple value. Usually we set the value to an integer which maps to some column in the database. Lets see how we can select a simple item in the ListBox control.

Sample Code
  1. private void ListBox2_SelectedIndexChanged(object sender, System.EventArgs e) {
  2.      string selectedItemTextFromListBox = ListBox1.SelectedItem;
  3.      // Returns the text
  4.      string selectedItemValueFromListBox = ListBox1.SelectedValue;
  5.      // returns the value
  6. }
Copyright exforsys.com


The listbox exposes the property of SelectedItem and SelectedValue which can be used to find the selected item or selected value. You can also set the SelectionMode of the ListBox to multiple which allows the user to select multiple items with the ctrl key being pressed while selecting. Here is a small piece of code you can use to find out all the selected items in the listbox.

Sample Code
  1. private void ListBox2_SelectedIndexChanged(object sender, System.EventArgs e) {
  2.      foreach(ListItem l in ListBox1.Items) {
  3.           if(l.Selected) {
  4.                selectedItems += l.Text;
  5.                selectedValue += l.Value;
  6.           }
  7.      }
  8. }
Copyright exforsys.com


The foreach loop will iterate the collection and if it finds the selected item it will assign to the string.

In the next tutorial we will see more advanced controls. 

References:

www.msdn.microsoft.com
www.asp.net

Ads


 
This tutorial is part of a ASP.NET Training tutorial series. Read it from the beginning and learn yourself.

ASP.NET Training

 

Comments