What is ListBox control and how it is used in ASP.NET

What is ListBox control and how it is used in ASP.NET

0 435

ListBox in ASP.NET is a Standard Control which is used to display a list of items as the name implies. Out of these ltems we can select only one or many items as required.

What is ListBox Control and how it is used in ASP.NET – Step by Step Guide

ListBox in ASP.NET is a Standard Control which is used to display a list of items as the name implies. Out of these ltems we can select only one or many items as required.

Now,  first we will teach you that how we can insert this user control in our web form. To insert this user control in your web form follow these steps:

  1. First click on Toolbox in your Visual Studio (VS) Framework. When you will click on Toolbox then you will see many user controls and out of them you will also see ListBox control under Standard Control. Just drag it to your web form or double click on it. When you insert ListBox Control in your web form, then following coding will appear:

<asp:ListBox ID=”ListBox1″ runat=”server”></asp:ListBox>

  1. Now we will insert items in this ListBox. To insert items in this ListBox there are 2 ways. First is from .aspx page and another is from .aspx.cs page.
  2. Inserting Items from .aspx page:

To insert Items from .aspx page click between closing tag of asp:ListBox and opening tag of /asp:ListBox and then when you type “<” (opening tag) then automatically asp:Listitem appears then you may insert the items as below:

<asp:ListItem Text=”India” Value=”0″></asp:ListItem>

 

To understand the whole concept so far watch this video:

 

 

Suppose you want to a Listbox which is used to select countries. So the whole coding will be shown as below:

<asp:ListBox ID=”ListBox1″ runat=”server” Width=”200px”>

        <asp:ListItem Text=”India” Value=”0″></asp:ListItem>

        <asp:ListItem Text=”China” Value=”1″></asp:ListItem>

        <asp:ListItem Text=”USA” Value=”3″></asp:ListItem>

        <asp:ListItem Text=”Nepal” Value=”4″></asp:ListItem>

</asp:ListBox>

In above coding Value field shows the index of each item which is used in programming to access the item.

When you view this page in explorer then you will see a ListBox with 4 Options and out of them you can select only one.

  1. Inserting Items from .aspx.cs page:

 

There is an another method to insert items in ListBox and that is using .aspx.cs page. For this first Insert the ListBox Control in web form which looks like as below:

<asp:ListBox ID=”ListBox1″ runat=”server”></asp:ListBox>

Now to insert items in this ListBox just move to .aspx.cs page. In .aspx.cs page first click on Page_Load event then to insert countries just type the following coding:

protected void Page_Load(object sender, EventArgs e)

    {

        ListBox1.Items.Insert(0, “India”);

        ListBox1.Items.Insert(1, “Pak”);

        ListBox1.Items.Insert(2, “China”);

        ListBox1.Items.Insert(3, “USA”);

    }

To understand this logic, watch this video:

 

 

Remove any Item from ListBox:

To Remove any item from .aspx.cs page we use RemoveAt property of Listbox. For example if you want to delete China from the above list then we will use it’s index value to delete it. The index value of first item is 0, second item’s index value is 1, third items’s index value is 2 end so on. So it is clear that index value of China is 2. So the following coding will delete the item China from the list.

        ListBox1.Items.RemoveAt(2);

 

Append any new item to the end of the list

If you want to append any new item at the end of the list say Singapore then following coding will do it:

 

        ListBox1.Items.Add(“Singapore”);

 

Removes all items from the ListBox

 

To remove all items from the ListBox use the following coding:

 

        ListBox1.Items.Clear();

 

Count the Numbers of a ListBox

 

To count the total number of a list box use the following coding

 

ListBox1.Items.Count

 

The output will be 5 when you will run the program.

 

Selecting Multiple Items:

 

To select multiple items add SelectionMode=”Multiple” as below:

 

<asp:ListBox ID=”ListBox1″ runat=”server” Width=”200PX” SelectionMode=”Multiple”></asp:ListBox>

 

That’s it about ListBox Control in Brief. We will describe more about ListBox control in another article. Happy coding.

 

NO COMMENTS

Leave a Reply