listbox实现添加删除转移及上下移动

该博客介绍了如何在ASP.NET中使用ListBox控件进行添加、删除、上下移动和转移列表项的操作。通过示例代码展示了在用户输入内容后将其添加到ListBox1,以及通过按钮实现列表项的上移、下移、移除至ListBox2和批量删除功能。此外,还涉及了从数据库加载数据到ListBox2。

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

using System.Data.SqlClient;

public partial class listbox实现添加删除转移及上下移动 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bandlist2();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.TextBox1.Text == string.Empty)
        {
            Response.Write("请输入内容!");

        }
        else
        {
            ListBox1.Items.Add(this.TextBox1.Text);
            this.TextBox1.Text = string.Empty;
        }


    }
    protected void btnUp_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.SelectedIndex > 0)
        {//获取当前选择信息
            string name = this.ListBox1.SelectedItem.Text;
            string id = this.ListBox1.SelectedValue;
            int index = this.ListBox1.SelectedIndex;
            //交换上一项与选择项的信息
            ListBox1.SelectedItem.Text = ListBox1.Items[index - 1].Text;
            ListBox1.SelectedItem .Value = ListBox1.Items[index - 1].Value;
            ListBox1.Items[index - 1].Text = name;
            ListBox1.Items[index - 1].Value = id;
            //设定上一项为当前选择项
            ListBox1.SelectedIndex--;
        }
    }
    protected void btnMove_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.SelectedValue == string.Empty)
        {
            Response.Write("<font color=red>请选择要移除的项</font>");

        }
        else
        {   string aa=this .ListBox1 .SelectedValue ;
       
            this.ListBox1.Items.Remove(aa);
            this.ListBox2.Items.Add(aa);
        }
    }
    protected void btnDown_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.SelectedIndex >=0 && this .ListBox1 .SelectedIndex <this .ListBox1 .Items .Count -1)
          
            {//获取当前选择信息
                string name = this.ListBox1.SelectedItem.Text;
                string id = this.ListBox1.SelectedValue;
                //获取选择项的索引值
                int index = this.ListBox1.SelectedIndex ;
                //交换下一项与选择项的信息
                ListBox1.SelectedItem.Text = ListBox1.Items[index + 1].Text;
                ListBox1.SelectedItem.Value = ListBox1.Items[index + 1].Value;
                ListBox1.Items[index + 1].Text = name;
                ListBox1.Items[index + 1].Value = id;
                //设定下一项为当前选择项

                this.ListBox1.SelectedIndex++;
        }
     
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {       //判断是否选择数据
        if (ListBox1.SelectedIndex > -1)
        {
            for (int m = ListBox1.Items.Count - 1; m >= 0; m--)
            {//判断获取的数据项是否被选择
                if (this.ListBox1.Items[m].Selected == true)
                {
                    ListBox1.Items.Remove(this.ListBox1.Items[m]);
                }
            }
        }
        else
        {
            Response.Write("请选择左边要删除的项");
        }
    }
    protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    private void bandlist2()
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        cn.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select shipname from orders",cn );
        DataSet ds = new DataSet();
        sda.Fill(ds, "orders");
        ListBox2.DataSource = ds.Tables["orders"].DefaultView;
        ListBox2.DataValueField = "shipname";
        ListBox2.DataBind();
        cn.Close();

    }
}

 

<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="listbox实现添加删除转移及上下移动.aspx.cs" Inherits="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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width ="50%" style ="border :solid 1px" >
            <tr>
                <td style ="width :10%; background-color :#00ee22;">
                    Listbox控件的应用
                </td>
            </tr>
            <tr>
                <td>
                    <table width ="100%"  style ="border :solid 1px">
                        <tr>
                            <td>
                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                            </td>
                            <td>
                                <asp:Button ID="btnAdd" runat="server" Text="添  加" onclick="btnAdd_Click" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:ListBox ID="ListBox1" runat="server" SelectionMode ="Multiple" ></asp:ListBox>
                            </td>
                            <td>
                                <asp:Button ID="btnUp" runat="server" Text="上  移" onclick="btnUp_Click" /><br />
                                <asp:Button ID="btnMove" runat="server" Text="转移->" onclick="btnMove_Click" /><br />
                                <asp:Button ID="btnDown" runat="server" Text="下  移" onclick="btnDown_Click" /><br />
                                <asp:Button ID="btnDel" runat="server" Text="删  除" onclick="btnDel_Click" />
                            </td>
                            <td>
                                <asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True"
                                    onselectedindexchanged="ListBox2_SelectedIndexChanged"></asp:ListBox>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值