ListBox control is an web server control. The ListBox control is used to store (store) multiple items and it allows the user to select more than one item from the ListBox control. ListBox control is similar to dropdownlist control.

Dropdownlist control allows the user to select only one item at a time, on the other hand, ListBox control allows the user to select multiple items at the same time. So we can also say that ListBox is a multi-row selection box control.

A ListBox control uses the SelectionMode property to change the mode of a section from single to multiple. The selectionMode of the default Listbox control is single If you want to select multiple items from the list box, change the SelectionMode property to multiple.

ListBox Control example using C # in ASP.Net: -

How to use ListBox control in C #?

Some important properties of ListBox control

Items: Items property is used to add items to the Listbox control. This indicates the collection of items in the Listbox.

The main two things to be noted while adding a new object to the ListBox control, each item in the ListBox control has two attributes, such as Text and Value. When adding a new item to the list box we need to give two things text and a value for each item.

Text: Text property displays the text in the list box.

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

To add new items to the list box control, go to the list box item property and show the items like the screen below.


How to add a new item to Listbox control in ASP.Net

If you click on the items property of the Listbox, you have the screen below to add the item. Click the Add button to add the item to the Listbox.

Add some items with the item's text and value and use the add button to add items to the list box control.

Some other important properties of Listbox are:

  • ListBox1.Items.Count = Return the total number of items in the listbox.
  • ListBox1.Items.Add ("ItemName") = Add new item to listbox control.
  • ListBox1.Items.Insert (int index, "ItemName") = insert a new item in a particular location in the listbox control.
  • ListBox1.Items.Remove ("ItemName") = Remove item from listbox control.
  • ListBox1.Items.RemoveAt (int index) = Remove the item from any desired index of the listbox.
  • ListBox1.Items.Clear() = listbox control से सभी item clear करें।
  • ListBox1.SelectedItem.Text = Returns the text value of the selected item of the listbox.
  • ListBox1.SelectedValue = Returns the Value property of the selected item in the listbox.
  • ListBox1.SelectedIndex = Returns the index of the selected item of the list. (index always starts at 0).
  • Here, the code for bind listbox control is provided from the database.
  • ListBox1.DataSource = DataTable या DataSet
  • ListBox1.DataTextField = This binds the text to the list box. (It appears in the list box)
  • ListBox1.DataValueField = This binds the value to the list box. (It remains invisible in the Listbox)

Let's take an example to understand all the properties of Listbox control in c #.

Design the web page given below:

Here is the HTML code to design the web page: -


  <form id="form1" runat="server">



    <table align="center" class="style1" style="border: thin solid #008080">


        <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>



        <td style="text-align: center">



          <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>





        <td class="style3">

          <asp:Button ID="btncount" runat="server" CssClass="style4" 

            onclick="btncount_Click" Text="Count" Width="86px" />


          <asp:Button ID="btnselectedtext" runat="server" CssClass="style4" 

            onclick="btnselectedtext_Click" Text="Selected Text" />


          <asp:Button ID="btnselectedvalue" runat="server" CssClass="style4" 

            onclick="btnselectedvalue_Click" Text="Selected Value" />

 <asp:Button ID="btnselectedIndex" runat="server" CssClass="style4" 

            onclick="btnselectedIndex_Click" Text="Index" />

          <br />

 <asp:Button ID="btnclear" runat="server" CssClass="style4" onclick="btnclear_Click" 

            Text="Clear()" Width="83px" />


          <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click" 

            style="font-weight: 700" Text="ADD" Width="67px" />


          <asp:Button ID="btnremove" runat="server" CssClass="style4" 

            onclick="btnremove_Click" Text="Remove" />




        <td style="text-align: center">

          <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" 









Here, we have 7 button control to understand all the properties of Listbox control.

ListBox1.Items.Count :-

The example below shows the total number of items in the list box control: -

protected void btncount_Click(object sender, EventArgs e)


  Label1.Text = "The Count = "+ListBox1.Items.Count.ToString();



Example: How to get the selected item from Listbox control in

protected void btnselectedtext_Click(object sender, EventArgs e)


  Label1.Text = "Text = "+ListBox1.SelectedItem.Text;


ListBox1.SelectedValue :-

Example: How to get the selected value from Listbox control in

protected void btnselectedvalue_Click(object sender, EventArgs e)


  Label1.Text = "Value = " +ListBox1.SelectedValue;


ListBox1.SelectedIndex :-

Get the selected value from listbox control in c #:

protected void btnselectedIndex_Click(object sender, EventArgs e)


  Label1.Text = "Index = " +ListBox1.SelectedIndex.ToString();


ListBox1.Items.Clear() :-

Example: clear all items of Listbox control.

protected void btnclear_Click(object sender, EventArgs e)



   Label1.Text = "ListBox Cleared";


How to clear all items in Listbox in c #.

protected void btnadd_Click(object sender, EventArgs e)



   Label1.Text = "Item Added";


ListBox1.Items.Remove(“text”) :-

Example: how to remove items from Listbox control.

protected void btnremove_Click(object sender, EventArgs e)



   Label1.Text = "Item Removed";


How to remove items from Listbox in c #.

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

Example: Add a new item to a specific location in the Listbox control. The index indicates the location of the newly added item in the Listbox control.

protected void btnadd_Click(object sender, EventArgs e)




ListBox1.Items.RemoveAt(int index) :-

Example: removing an item from a specified location of the Listbox control. The index starts at 0.

protected void btnremove_Click(object sender, EventArgs e)



   Label1.Text = "Item Removed";



I am an engineer by profession but being a blogger is my old day's dream to create my site for those who are more curious about my birthplace than I am coming from (India). the current city lives in Navi Mumbai, India

Post a Comment


Previous Post Next Post

نموذج الاتصال