ListBox Control in ASP.Net

In previous asp.net server control post we had discussed about MultiView Control and DropDownList Control and FileUpload Control and CheckBox Control and RadioButton Control and Adrotator Control and TextBox Control.

ASP.Net ListBox Control

ListBox control is an asp.net web server control. ListBox control used to store the multiple items and allow user to select multiple item from listbox control. ListBox control is same as dropdownlist control.

The dropdownlist control allow user to select maximum only one item at a time, on other hand listbox control allow user to select multiple items same time. so we can also say listbox is a multi row selection box control.

In a ListBox control there is a SelectionMode property to change the mode of section from single to multiple. By default listbox control selection mode is single if you want to select multiple items from listbox, then just change the SelectionMode property to multiple.

ListBox Control Example in ASP.Net C#

How to use ListBox control in asp.net C#.

How to use ListBox control in ASP.Net C#.

How to use ListBox control in asp.net C#.

Some important Properties of ListBox Control.

Items : Items property used to add item into the listbox control. It is indicate the collection of items in the listbox.

There are main two things to consider while adding new item in listbox control.
Each Item of listbox control contain Text and Value attributes.

At time of adding new item in listbox we need to give two things text and values for each item.

Text : Text Property specify the Text display in the listbox.

Value : The value property is invisible value, but we can get the value while programming. Each item has a text and value with in it.

For add new items in listbox control go to the lsitbox Items property and add some items show like below screen.

How to add new items in listbox control in asp.net.

How to add new items in listbox control in asp.net.

If you click the items property of listbox you have a below screen to add some items. Here click Add button to add items to listbox.

How to add new items in listbox control in asp.net.

How to add new items in listbox control in asp.net.

Add some items with Text and Value of items and Add button to add items to listbox control.

How to add new items in listbox control in asp.net.

How to add new items in listbox control in asp.net.

The result of the listbox control with four items.

How to add new items in listbox control in asp.net.

How to add new items in listbox control in asp.net.

There are other some important properties of listbox :

ListBox1.Items.Count = Return the total count of items in the listbox.
ListBox1.Items.Add(“ItemName”) = Add the new item in to the listbox control.
ListBox1.Items.Insert(int index, “ItemName”) = Add the new item at specific position in listbox control.
ListBox1.Items.Remove(“ItemName”) = Remove the item from the listbox control.
ListBox1.Items.RemoveAt(int index) = Remove the item from the listbox at desired position index.
ListBox1.Items.Clear() = Clear all the items from listbox control.
ListBox1.SelectedItem.Text = Returns the Text value of selected item of the listbox.
ListBox1.SelectedValue = Returns the Value property of the selected item of the listbox.

ListBox1.SelectedIndex = Returns the Index of selected item of listbox. (Index always start from Zero).

Here, is the code for bind listbox control from database.
ListBox1.DataSource = DataTable OR DataSet
ListBox1.DataTextField = Bind the Text to listbox (It is visible in listbox)
ListBox1.DataValueField  = Bind the Value to listbox (It is Invisible in listbox)
ListBox1.DataBind();

 

Lets take an example to understand all the properties of listbox control in asp.net c#.
Design the asp.net web page like below:
Here is the HTML Code for design asp.net web page:

Here, we have seven button control for understand all property of listbox control.

ListBox1.Items.Count

Below example show the total number of item count in listbox control.

Count Total Number of items in listbox control in asp.net

Count Total Number of items in listbox control in asp.net

 

ListBox1.SelectedItem.Text

Example : How to get selected item from listbox control in asp.net.

Get Selected Item from listbox control in asp.net c#

Get Selected Item from listbox control in asp.net c#

ListBox1.SelectedValue

Example : How to get Selected Value from listbox control in asp.net.

Get Selected Value from listbox control in asp.net c#

Get Selected Value from listbox control in asp.net c#

 

ListBox1.SelectedIndex

Example: Get Selected Index in listbox control in asp.net.

Get Selected Index from listbox control in asp.net c#

Get Selected Index from listbox control in asp.net c#

 

ListBox1.Items.Clear()

Example : Clear all items in listbox control.

How to clear all items from listbox in asp.net c#.

How to clear all items from listbox in asp.net c#.

 

ListBox1.Items.Add(“text”);

Example : Add new item in listbox control.

How to Add new items in  listbox in asp.net c#.

How to Add new items in listbox in asp.net c#.

 

ListBox1.Items.Remove(“text”)

Example : Remove items from listbox control.

How to Remove Item from listbox in asp.net c#.

How to Remove Item from listbox in asp.net c#.

 

ListBox1.Items.Insert(int index, “text”);

Example : Add new item at specific location in listbox control. The index is the indicate the location of newly added item in listbox control.

 

ListBox1.Items.RemoveAt(int index)

Example : Remove items from listbox control at specify the index location. The index start from zero.

Here is the code behind c# code :

 

Leave a Reply

Your email address will not be published. Required fields are marked *

8 + 1 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">