ASP.Net两个ListBox控件的左移与右移

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

拖拽两个ListBox控件和四个button控件,ListBox控件的属性SelectionMode设置为Multiple

ListBox.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBox.aspx.cs" Inherits="ListBox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        .listbox1{
            width: 100px;
            height: 200px;
        }
        .listbox2{
            width: 100px;
            height: 200px;
            margin-left: 150px;
            margin-top: -200px;
        }
        .btn{
            width: 30px;
            height: 150px;
            margin-left: 100px;
            margin-top: -200px;
            text-align: center;
        }
        input{
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="listbox1">
        <asp:ListBox ID="ListBox1" runat="server" Height="174px" Width="91px" SelectionMode="Multiple"></asp:ListBox>
         </div>
        <div class="listbox2">
        <asp:ListBox ID="ListBox2" runat="server" Height="170px" Width="116px" SelectionMode="Multiple"></asp:ListBox>
        </div>
        <div class="btn">
        <asp:Button ID="Button1" runat="server" Text="&gt;" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="&lt;" OnClick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" Text="&gt;&gt;" OnClick="Button3_Click" />
        <asp:Button ID="Button4" runat="server" Text="&lt;&lt;" OnClick="Button4_Click" />
        </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;
public partial class ListBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ArrayList list = new ArrayList();
            for (int i = 0; i <= 10; i++)
            {
                list.Add(i.ToString());
            }
            ListBox1.DataSource = list;
            ListBox1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int count = ListBox1.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox1.Items[index];
            if (ListBox1.Items[index].Selected == true)
            {
                ListBox1.Items.Remove(item);
                ListBox2.Items.Add(item);
                index--;
            }
            index++;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int count = ListBox2.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox2.Items[index];
            if (ListBox2.Items[index].Selected == true)
            {
                ListBox2.Items.Remove(item);
                ListBox1.Items.Add(item);
                index--;
            }
            index++;
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int count = ListBox1.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox1.Items[index];
            ListBox1.Items.Remove(item);
            ListBox2.Items.Add(item);
        }
        index++;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        int count = ListBox2.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox2.Items[index];
            ListBox2.Items.Remove(item);
            ListBox1.Items.Add(item);
        }
        index++;
    }
}

原文链接:https://www.idaobin.com/archives/1122.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

26 + = 30