Download
Academy
Current location: Downcodes.com -> Academy -> Programming -> .NET tutorial -> Using the ASP.NET 2.0 ListBox Control(3)
Recommend
HOT TOP10
Using the ASP.NET 2.0 ListBox Control(3)
Date: 2008-1-4 Author: Hit: View:[Large font Middle font Small font]
Creating the ListItem objects and adding them using ListBox. Items.Add() method
(Page 3 of 5 )

As we observed before, the ListBox control has a collection of list items. The items presented by the ListBox control are instances of the ListItem class. The ListItem class is used by other controls, such as the DropDownList and the RadioButtonList controls, to represent a list item and the associated value for that item. The text of a ListItem instance is represented by the ListItem.Text property and the value associated with the item is represented by the ListItem.Value property.

Usually, this value represents something important to the application and may not be important for the user, or the user doesn't really care about this value. For example, say that you are retrieving an employee id and employee name from the database and populating a ListBox control. You would associate the employee id (through the ListItem.Value) with the employee name (through the ListItem.Text) so the user would see the employee names in the list. When he selects an employee, however, you can do your work using the employee id instead of the employee name by retrieving the Value property of the selected list item.

In the previous section, we created five ListItem objects, and then we assigned string values to both the ListItem.Text and ListItem.Value properties. Finally, we added those objects to the ListBox control by using the Add() method of the Items Property of the ListBox control. You will find this pattern in many of the controls in the .NET Framework, where you use the Add() method of a property of a collection type to add items to the control. In our case, the ListBox.Items property is of type ListItemCollection, which means that you can use common collection's methods like Add() or Remove() to add or remove items from a ListBox control, and that you can use a foreach statement to iterate over the ListItem objects of the ListBox.Items ListItemCollection property.

You can think of the Items property as a reference to the ListItemCollection of the ListBox control you are working on; that's why you can call the ListItemCollection.Add() method to add a new list item to the control. If you are not familiar with C# Collections, you must consult other articles about this subject, because it's very important to understand what collections are and how they work.

The above code could also have been written as follows:

// checking if this page is loading for the first item or it's

// postback. If it's not a postback and the page is loading for

// the first time then we populate the ListBox control with the

// Listitem objects and if it's a PostBack we simply do nothing

  if (!IsPostBack)

{

//creating the ListItem objects and assigning the

// Text and Value Properties through the Constructor

  ListItem firstItem = new ListItem("Mick Joseph", "10");

  ListItem secondItem = new ListItem("Mary Johnson", "11");

  ListItem thirdItem = new ListItem("David Johnson", "12");

  ListItem FourthItem = new ListItem("Mark Roberts", "13");

  ListItem FifthItem = new ListItem("Paul Bill", "14");

// adding the ListItem objects to the Item collection of the

  ListBox Control

  ListBox1.Items.Add(firstItem);
 

  ListBox1.Items.Add(secondItem);

  ListBox1.Items.Add(thirdItem);

  ListBox1.Items.Add(FourthItem);

  ListBox1.Items.Add(FifthItem);

}

This code uses the ListItem's Constructor that accepts two string values. The first is the Text of the ListItem and the second is the Value of the ListItem.

(From: aspfree)

Relative article:
·Using the ASP.NET 2.0 ListBox Control(1)
·Using the ASP.NET 2.0 ListBox Control(2)
·Using the ASP.NET 2.0 ListBox Control(4)
·Using the ASP.NET 2.0 ListBox Control(5)
Relative software: