asp.net_ListBox应用

记录ListBox的基本应用。

使用的数据库文件:

链接:https://pan.baidu.com/s/1w_3qnRU7zcCfJEIYq3Ik-g
提取码:l4y0

一、实现获取ListBox文本内容并输出到文本框当中

在这里插入图片描述

前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test01.aspx.cs" Inherits="test01" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:ListBox ID="ListBox1" runat="server" DataSourceID="AccessDataSource1" SelectionMode="Multiple"
            DataTextField="CAI_NAME" DataValueField="ID" Height="168px" Width="107px"></asp:ListBox>
    &nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="选择" Height="23px" 
            onclick="Button1_Click" Width="51px" />
&nbsp;<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/test.mdb" SelectCommand="SELECT * FROM [TB_CAI]">
        </asp:AccessDataSource>
        <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Height="95px" Width="183px" 
            TextMode="MultiLine"></asp:TextBox>
        <br />
        <br />
    </div>
    </form>
</body>
</html>

1.如何绑定数据源:在我之前的博客有提到:

asp.net_css应用

2.SelectionMode="Multiple"表示此ListBox是可以多选的,而TextMode="MultiLine"代表多行文本框

后端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test01 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //one
        //TextBox1.Text = "";
        //for (int i = 0; i < ListBox1.Items.Count;i++ )
        //{
        //    if(ListBox1.Items[i].Selected)
        //    {
        //        TextBox1.Text += ListBox1.Items[i].Text + ",";
        //    }
        //}

        //two

        foreach(ListItem li in ListBox1.Items)
        {
            if(li.Selected)
            {
                TextBox1.Text += li.Text + ",";
            }
        }
    
    }
}

one注释的代码代表的是另一种方法,即使用for循环,但是相对于foreach来说需要写更多代码,复杂度更高

二、点击按钮事件实现ListBox内容调换

这个示例有点像很多软件的自定义功能选择等等,先上图

在这里插入图片描述

单选或者多选用户列表的选项,点击按钮,就会变更到授权列表。

前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test02.aspx.cs" Inherits="test02" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <br />
&nbsp; 用户列表&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        授权<br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server"  Height="170px" Width="108px" SelectionMode ="Multiple" ></asp:ListBox>
    &nbsp;
        <asp:Button ID="Button1" runat="server" Text=">>" onclick="Button1_Click" />
&nbsp;<asp:Button ID="Button2" runat="server" Text="<<" onclick="Button2_Click" />
        &nbsp;<asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
            Text=">" />
&nbsp;
        <asp:Button ID="Button4" runat="server" Text="<" onclick="Button4_Click" />
&nbsp;
        <asp:ListBox ID="ListBox2" runat="server" Height="170px" Width="108px" SelectionMode ="Multiple">
        </asp:ListBox>
    </div>
    </form>
</body>
</html>

后端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test02 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBox1.Items.Add("Administrator");
            ListBox1.Items.Add("Guest");
            ListBox1.Items.Add("小王");
            ListBox1.Items.Add("小刘");
            ListBox1.Items.Add("小李");

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            ListItem li = ListBox1.Items[0];
            ListBox1.Items.Remove(li);
            ListBox2.Items.Add(li);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            ListItem li = ListBox2.Items[0];
            ListBox2.Items.Remove(li);
            ListBox1.Items.Add(li);
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            ListItem li = ListBox1.Items[i];
            if (ListBox1.Items[i].Selected)
            {
                ListBox1.Items.Remove(li);
                ListBox2.Items.Add(li);
            }
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            ListItem li = ListBox2.Items[i];
            if (ListBox2.Items[i].Selected)
            {
                ListBox2.Items.Remove(li);
                ListBox1.Items.Add(li);
            }
        }
    }
}

拿button3按钮举例(’>’),基本思路是使用for循环去遍历用户列表的所有选项,然后每个选项判断是否被选中(ListBox1.Items[i].Selected),如果选中,在用户列表中删除这个选项,并在另一个列表增加此选项。

理论上这样的方法是行得通的,但是会出现一个bug:假设for循环遍历到第一个选项,即items[0],并假设确实选中了它,此选项被删除并添加到了另一个列表,但与此同时,下一个选项items[1]也就向上挪了一位,变成了items[0],导致我们在选中连续的n个选项的时候没办法全部进行转移,始终会漏几个选项。

所以在for循环的时候就可以改用从下遍历到上,即从最大遍历到最小

for (int i = ListBox1.Items.Count - 1; i >= 0; i–)

ListBox1.Items.Count-1的作用是防止越界

这样操作的话就可以避免上面的bug出现

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BeJav

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值