四大列表控件之ListBox控件(多选的下拉列表)

本文介绍了ASP.NET中的ListBox控件,它支持多选功能,适用于下拉列表场景。通过示例展示了如何绑定数据,并讨论了ListBox1_SelectedIndexChanged事件处理程序及其运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Default.aspx内容`

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    学生列表:<br/>
        <asp:ListBox ID="ListBox1" runat="server" Height="200px" Width="200px" 
            Font-Size="Small" onselectedindexchanged="ListBox1_SelectedIndexChanged" 
            AutoPostBack="True"></asp:ListBox>
    <br />
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
    
    </div>
    </form>
</body>
</html>

向ListBox1控件绑定数据

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = new DataSet();//
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "张一", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "李三", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 5, "周五", 100 });
                this.ListBox1.DataSource = ds.Tables["stu"];
                this.ListBox1.DataValueField = "stuNo";
                this.ListBox1.DataTextField = "stuName";
                this.ListBox1.DataBind();

            }
        }

事件函数ListBox1_SelectedIndexChanged

 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Label1.Text = "你选择的学生是:学号 " + this.ListBox1.SelectedValue.ToString() + " 姓名 " + this.ListBox1.SelectedItem.Text.ToString();
        }

这是运行结果:
这是运行结果
这是程序设计图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值