How to use ListBox list box control in ASP. NET

  • 2021-07-21 08:09:56
  • OfStack

The ListBox control allows the user to select one or more items from a predefined list. It differs from the DropDownList control in that it not only displays multiple items at once, but also (optionally) allows the user to select multiple items.

1. Attributes

属性 作用
SelectionMode Single|Multiple 列表选择模式:单选|多选
Selected False|True 是否为选中状态

2. Examples

ListBox.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBox.aspx.cs" Inherits="WebControls_ListBox" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 293px;
        }
        .style2
        {
            width: 233px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h5>1, Radio list box </h5>
        Provinces: <asp:ListBox ID="lstProv"  runat="server">
            <asp:ListItem> Shandong </asp:ListItem>
            <asp:ListItem Selected="True"> Hebei </asp:ListItem>
            <asp:ListItem> Inner Mongolia </asp:ListItem>
            <asp:ListItem> Anhui </asp:ListItem>
        </asp:ListBox>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text=" Submit " onclick="btnSubmit_Click" />
        <hr />
        What you chose is: <asp:Label ID="lblState" runat="server"></asp:Label>
 
        <hr />
        <h5>
            2, Multiple selection list box </h5>
        Provinces: <asp:ListBox ID="lstProv2" runat="server" SelectionMode="Multiple">
            <asp:ListItem> Shandong </asp:ListItem>
            <asp:ListItem Selected="True"> Hebei </asp:ListItem>
            <asp:ListItem> Inner Mongolia </asp:ListItem>
            <asp:ListItem> Anhui </asp:ListItem>
        </asp:ListBox>
        &nbsp; Hint: Ctrl+ Click <br />
        <asp:Button ID="btnSubmit2" runat="server" Text=" Submit "
            onclick="btnSubmit2_Click"  />
        <hr />
        What you chose is: <asp:Label ID="lblState2" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

ListBox.aspx.cs


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class WebControls_ListBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblState.Text = lstProv.SelectedValue;
    }
    protected void btnSubmit2_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        for (int i = 0; i < lstProv2.Items.Count; i++)
        {
            if (lstProv2.Items[i].Selected == true)
            {
                str = string.Format("{0}{1},",str,lstProv2.Items[i].Value);
            }
        }
        str = str.Substring(0, str.Length - 1); // Remove the last 1 A " , "
        lblState2.Text = str;
    }
}


Related articles: