Introduction:

Here we will explain how to use ListBox in asp.net or what is ListBox in asp.net or how to get ListBox selected items in asp.net with example.

Description:

Asp.net ListBox control is used to create a single- or multi-selection drop-down list. The ListBox control is similar to the DropDownList control with two main differences. First, the ListBox control requires more screen real estate because it always displays a certain number of list items. Second, The ListBox control enables a user to select multiple items by setting the SelectionMode property to ListSelectionMode.Multiple. When it is rendered on the page, it is implemented through <select/> HTML tag.

The ListBox control also supports data binding. To bind the control to a data source, first create a data source, such as a System.Collections.ArrayList, that contains the items to display in the control. Next, use the Control.DataBind method to bind the data source to the ListBox control. Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the Text and Valueproperties, respectively, of each list item in the control. The ListBox control will now display the information from the data source.

Example:

ListBox.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs"
Inherits="AspDotNet.ListBox" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div><b>DropDownList Demo</b></div>
            <table>
                <tr>
                    <td>SelecSelect as Option:</td>
                    <td>
                        <asp:ListBox ID="ListBox1" AutoPostBack="true" runat="server"
                          
                    Width="150" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
                       
                    </asp:ListBox>    
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><b>Selected Item:</b></td>
                </tr>
                <tr>
                    <td>Item Value: </td>
                    <td>
                  <asp:Label ID="itemvalue" runat="server" Text="Label"></asp:Label>
                            </td>
                </tr>
                <tr>
                    <td>Item Text: </td>
                    <td>
                        <asp:Label ID="itemtext" runat="server" Text="Label"></asp:Label>
                                </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>


ListBox.aspx.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class ListBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList list = new ArrayList();
                list.Add(new ListItem("Item1", "I001"));
                list.Add(new ListItem("Item2", "I002"));
                list.Add(new ListItem("Item3", "I003"));
                list.Add(new ListItem("Item4", "I004"));
                list.Add(new ListItem("Item5", "I005"));
                ListBox1.DataSource = list;
                ListBox1.DataTextField = "Text";
                ListBox1.DataValueField = "Value";
                ListBox1.DataBind();
            }
        }               protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            itemtext.Text = ListBox1.SelectedItem.Text;
            itemvalue.Text = ListBox1.SelectedItem.Value;
        }
    }
}

Above example when you change selected item then called SelectedIndexChanged event where you can get selected value and text.

Let’s see output:

You can add its option items by directly writing into .aspx page directly or dynamically add at run time or bind through database.

Following are some important properties that are very useful.

 

Properties

Description

Rows

No. of rows (items) can be set to display in the List.

SelectionMode

Single or Multiple. If multiple, it allows user to select multiple items from the list by holding Ctrl or Shift key.

SelectedValue

Get the value of the Selected item from the dropdown box.

SelectedIndex

Gets or Sets the index of the selected item in the dropdown box.

SelectedItem

Gets the selected item from the list.

Items

Gets the collection of items from the dropdown box.

DataTextField

Name of the data source field to supply the text of the items. (No need to set when you are adding items directly into .aspx page.)

DataValueField

Name of the data source field to supply the value of the items. (No need to set when you are adding items directly into .aspx page.)

DataSourceID

ID of the datasource component to provide data. (Only used when you have any DataSource component on the page, like SqlDataSource, AccessDataSource etc.)

DataSource

The datasource that populates the items in the listbox box. (Generally used when you are dynamically generating the items from Database.)

AutoPostBack

true or false. If true, the form is automatically posted back to the server when user changes the dropdown list selection. It will also fire OnSelectedIndexChanged method.

AppendDataBoundItems

true or false. If true, the statically added item (added from .aspx page) is maintained when adding items dynamically (from code behind file) or items are cleared.

OnSelectedIndexChanged

Method name that fires when user changes the selection of the dropdown box. (Fires only when AutoPostBack=true.)

 


I hope this will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like