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:
<body>
    <form id="form1" runat="server">
    <div>
    
        <table align="center" class="style1" style="border: thin solid #008080">
            <tr>
                <td class="style2" 
                    style="text-align: center; border-bottom-style: solid; border-bottom-width: thin; border-bottom-color: #008080;">
                    ListBox Control in ASP.Net</td>
            </tr>
            <tr>
                <td style="text-align: center">
&nbsp;
                    &nbsp;
                    <asp:ListBox ID="ListBox1" runat="server" Height="121px" Width="98px">
                        <asp:ListItem Value="1">ABC</asp:ListItem>
                        <asp:ListItem Value="2">PQR</asp:ListItem>
                        <asp:ListItem Value="3">MNO</asp:ListItem>
                        <asp:ListItem Value="4">XYZ</asp:ListItem>
                    </asp:ListBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    <asp:Button ID="btncount" runat="server" CssClass="style4" 
                        onclick="btncount_Click" Text="Count" Width="86px" />
&nbsp;
                    <asp:Button ID="btnselectedtext" runat="server" CssClass="style4" 
                        onclick="btnselectedtext_Click" Text="Selected Text" />
&nbsp;
                    <asp:Button ID="btnselectedvalue" runat="server" CssClass="style4" 
                        onclick="btnselectedvalue_Click" Text="Selected Value" />
&nbsp;<asp:Button ID="btnselectedIndex" runat="server" CssClass="style4" 
                        onclick="btnselectedIndex_Click" Text="Index" />
                    <br />
&nbsp;<asp:Button ID="btnclear" runat="server" CssClass="style4" onclick="btnclear_Click" 
                        Text="Clear()" Width="83px" />
&nbsp;
                    <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" 
                        style="font-weight: 700" Text="ADD" Width="67px" />
&nbsp;&nbsp;
                    <asp:Button ID="btnremove" runat="server" CssClass="style4" 
                        onclick="btnremove_Click" Text="Remove" />
                </td>
            </tr>
            <tr>
                <td style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" 
                        ForeColor="#CC0000"></asp:Label>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>

ListBox1.Items.Count

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

protected void btncount_Click(object sender, EventArgs e)
 {
    Label1.Text = "The Count = "+ListBox1.Items.Count.ToString();
 }
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.

protected void btnselectedtext_Click(object sender, EventArgs e)
 {
    Label1.Text = "Text = "+ListBox1.SelectedItem.Text;
 }
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.

protected void btnselectedvalue_Click(object sender, EventArgs e)
 {
    Label1.Text = "Value = " +ListBox1.SelectedValue;
 }
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 from listbox control in asp.net.

protected void btnselectedIndex_Click(object sender, EventArgs e)
  {
    Label1.Text = "Index = " +ListBox1.SelectedIndex.ToString();
  }
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 from listbox control.

protected void btnclear_Click(object sender, EventArgs e)
  {
     ListBox1.Items.Clear();
     Label1.Text = "ListBox Cleared";
  }
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.

protected void btnadd_Click(object sender, EventArgs e)
 {
     ListBox1.Items.Add("Meera");
     Label1.Text = "Item Added";
 }
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.

protected void btnremove_Click(object sender, EventArgs e)
 {
     ListBox1.Items.Remove("Meera");
     Label1.Text = "Item Removed";
 }
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.

protected void btnadd_Click(object sender, EventArgs e)
 {
     ListBox1.Items.Insert(2,"Meera");
     Label1.Text = "Item Inserted";
 }

 

ListBox1.Items.RemoveAt(int index)

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

protected void btnremove_Click(object sender, EventArgs e)
 {
     ListBox1.Items.RemoveAt(2);
     Label1.Text = "Item Removed";
 }

Here is the code behind c# code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btncount_Click(object sender, EventArgs e)
    {
        Label1.Text = "The Count = "+ListBox1.Items.Count.ToString();
    }
    protected void btnselectedtext_Click(object sender, EventArgs e)
    {
        Label1.Text = "Text = "+ListBox1.SelectedItem.Text;
    }
    protected void btnselectedvalue_Click(object sender, EventArgs e)
    {
        Label1.Text = "Value = " +ListBox1.SelectedValue;
    }
    protected void btnselectedIndex_Click(object sender, EventArgs e)
    {
        Label1.Text = "Index = " +ListBox1.SelectedIndex.ToString();
    }
    protected void btnclear_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Clear();
        Label1.Text = "ListBox Cleared";
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Add("Meera");
        Label1.Text = "Item Added";
    }
    protected void btnremove_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Remove("Meera");
        Label1.Text = "Item Removed";
    }
}

 

Related ASP.Net Topics :

AdRotator Control in ASP.Net
FileUpload Control in ASP.Net C#


Subscribe us

If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.

We hope that this asp.net tutorial helped you to understand about ListBox control.


Next, asp.net tutorial we will understand about Literal Control.


Leave a Reply

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