Introduction
The ListBox control, a container of list items, can be used to create and display a list of items and select one or multiple items from such list of items. However, you can control the number of list items displayed in the control, and adjust the size of the control, that is, height and width. In order to work with a ListBox control, simply drag-and-drop an instance of the control from the toolbox into your web form
Adding List Items to the ListBox Control
You populate data in a ListBox control using the list items. You can add the list items through the .aspx page as shown in the following code snippet:
<asp:ListBox ID="ListBox1" runat="server" Height="125px"
Width="214px">
<asp:ListItem Value="1">James</asp:ListItem>
<asp:ListItem Value="2">Jimmy</asp:ListItem>
<asp:ListItem Value="3">Jonny</asp:ListItem>
<asp:ListItem Value="4">Johnson</asp:ListItem>
<asp:ListItem Value="5">Jack</asp:ListItem>
<asp:ListItem Value="6">Jill</asp:ListItem>
<asp:ListItem Value="7">Jollie</asp:ListItem>
</asp:ListBox>
You can also add list items to the ListBox control programmatically using the overloaded Add() method of the Items property of this control as shown in the following code snippet:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateListItems();
}
private void PopulateListItems()
{
ListBox1.Items.Add("James");
ListBox1.Items.Add("Jimmy");
ListBox1.Items.Add("Jonny");
ListBox1.Items.Add("Johnson");
ListBox1.Items.Add("Jack");
ListBox1.Items.Add("Jill");
ListBox1.Items.Add("Jollie");
}
Removing List Items from the ListBox Control
You can remove a list item from the ListBox control using the RemoveAt() method that accepts the index number of the list item that you need to remove from the collection of list items
ListBox1.Items.RemoveAt(0);
To remove all the list items from the ListBox control, use the following code:
ListBox1.Items.Clear();
Retrieving Multiple Values from ListBox
ListBox control can be set to allow user to select multiple values from the listbox itself. There is a properties named SelectionMode.You can set this values to be "Single" or "Multiple".
Below is the code snippet you can use to retrive the multiple values of the ListBox
protected void Button1_Click(object sender, EventArgs e)
{
string str = String.Empty;
for (int i = 0; i < ListBox1.Items.Count - 1; i++)
{
if (ListBox1.Items[i].Selected)
str += ListBox1.Items[i].Text+",";
}
str = str.Substring(0,str.LastIndexOf(‘,'));
Label1.Text = str;
}
Binding Data to a ListBox Control
To bind data to the ListBox control programmatically, we need to set the DataTextField and the DataValueField properties appropriately and then make a call to the DataBind() method shown as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable oDt = getData();
listBox.DataSource = oDt;
listBox.DataValueField = "CountryID";
listBox.DataTextField = "CountryName";
listBox.DataBind();
}
}
The
DataTextField property is used to retrieve the contents of the Text property of the control, whereas the
DataValueField property is used to retrieve the contents of the Value property of the control. Note that the Text property contains text which is what is displayed in the web page and a Value property which is in the HTML.
Handling ListBox Control Events
The
SelectedIndexChanged event in the ListBox class is fired whenever the
SelectedIndex in the ListBox changes as and when your web page postbacks to the Web Server.
In order for the Postback to happen when the user select the values of the ListBox, you need to set the property
AutoPostBack = true
The following code snippet shows how this event can be used:
private void lstBox_SelectedIndexChanged(object sender,System.EventArgs e)
{
Response.write(lstBox.SelectedItem.Value);
}
Conclusion
This article is very useful for those that just starting to learn .NET. It covers the basic things about the ListBox control that you would use in your daily programming activities.
Other Related and Popular Articles :
Author Profile : Sanjay Shravan
How would you rate the quality of this content?
Poor
Excellent
Comments
Leave New Comments
Article Content copyright by
Sanjay Shravan
Everything else Copyright © by
WorldofASP.NET
2008