WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 135 users


WorldofASP.NET >> ASP.NET >> Unedited ASP.NET

ListBox Control Explained

This article covers all you need to know about ListBox Control
Published Date : 11 Aug 2008
Author : Sanjay Shravan
Language : C#
Platform : .NET
Technology : ASP.NET
Views : 1804
Rating : (0 votes so far)



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.
Tag Cloud
  cannot start service from the command line or a debugger. a windows service must   generate random number asp.net   asp.net file upload progress   asp httpwebrequest   asp.net listbox control   httpwebrequest asp.net c#   asp.net encryption   smart device application   asp.net cookies encryption   asp.net upload ajax progress   edit update insert in a gridview c#   form view in asp.net   checkboxlist datasource   httpwebrequest in asp.net 2.0   httpwebrequest httpwebresponse   httpwebrequest .net   upload file progress bar asp.net   asp.net url parameters   "javascript in asp.net"   create httpwebrequest   asp.net tooltip   formview displaying inserted data   httpwebrequest create   asp.net file upload progress bar   cannot start service from the command line or a debugger   asp.net pass data from one page to another   datalist control   asp.net cookie shopping cart   asp.net httpwebrequest   tooltip asp.net   asp.net random number   contact us asp.net   feedback form in asp.net   httpwebrequest   asp.net delegate   httpwebresponse   frames image gallery thumbnails asp.net   formview asp.net   httpmodule httphandler   ajax updatepanel button   httpwebrequest vb.net





Other Related and Popular Articles :

Simple asp2 feedback form
A simple way to get user feedback via webpage email.

Designing a Membership Sign-Up and Login Page in ASP.NET
How to design user interface for registration and login in ASP.NET

Validate Email and Domain in a Web Form
Simple method to validate using javascript and ASP.NET validation control

Basics of XHTML-MP
Basics of XHTML-MP

Activate User Registration by Email in ASP.NET
How to activate registered user using System.Net.Mail.SMTPClient class

Implement captcha control library for ASP.NET
a captcha control, image, image handler, and supression class for form validation


Author Profile : Sanjay Shravan

Click here to view Author Profile


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

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card