• listbox多选实现上下移动 js版和服务器版


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906021128.aspx.cs" Inherits="ListBoxs_X200906021128" %>
    <!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>
        <script type="text/javascript">
            function MoveUP(fElement)
            {
                if (fElement.options.length == 0 || fElement.options[0].selected) return;
                for (var i = 1; i < fElement.options.length; i++)
                {
                    if (fElement.options[i].selected)
                    {
                        var text = fElement.options[i].text;
                        var value = fElement.options[i].value;
                        var selected = fElement.options[i].selected;
    
                        fElement.options[i].text = fElement.options[i - 1].text;
                        fElement.options[i].value = fElement.options[i - 1].value;
                        fElement.options[i].selected = fElement.options[i - 1].selected;
                        
                        fElement.options[i - 1].text = text;
                        fElement.options[i - 1].value = value;
                        fElement.options[i - 1].selected = selected;
                    }
                }
            }
    
            function MoveDown(fElement)
            {
                if (fElement.options.length == 0 || fElement.options[fElement.options.length - 1].Selected) return;
                
                for (var i = fElement.options.length - 1; i > -1; i--)
                {
                    if (fElement.options[i].selected)
                    {
                        var text = fElement.options[i + 1].text;
                        var value = fElement.options[i + 1].value;
                        var selected = fElement.options[i + 1].selected;
    
                        fElement.options[i + 1].text = fElement.options[i].text;
                        fElement.options[i + 1].value = fElement.options[i].value;
                        fElement.options[i + 1].selected = fElement.options[i].selected;
    
                        fElement.options[i].text = text;
                        fElement.options[i].value = value;
                        fElement.options[i].selected = selected;
                    }
                }
            }
            
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="fListBox" runat="server" Height="200px" SelectionMode="Multiple" Width="200px"></asp:ListBox>
            <br />
            <br />
            --- Server Side ------------------------------------------------------
            <br />
            <br />
            <asp:Button ID="btnUp" runat="server" onclick="btnUp_Click" Text="Up" />
            <asp:Button ID="btnDown" runat="server" onclick="btnDown_Click" Text="Down" />
            <br />
            <br />
            --- Client Side ------------------------------------------------------
            <br />
            <br />
            <input type="button" value="UP" onclick="MoveUP(document.getElementById('fListBox'));" />
            <input type="button" value="Down" onclick="MoveDown(document.getElementById('fListBox'));" />
            <br />
             用 Ctrl + 鼠标 或 Ctrl + Shift 鼠标,实现多选或间断多选
        </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 ListBoxs_X200906021128 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 0; i < 20; i++)
                    fListBox.Items.Add(i.ToString());
            }
        }
    
        protected void btnUp_Click(object sender, EventArgs e)
        {
            // 没有项,或第一个选中节点已经是第一项,则不处理
            if (this.fListBox.Items.Count == 0 || this.fListBox.Items[0].Selected) return; 
            for (int i = 1; i < this.fListBox.Items.Count; i++)
            {
                if (this.fListBox.Items[i].Selected)
                    this.ChangeProperty(this.fListBox.Items[i - 1], this.fListBox.Items[i]);
            }
        }
    
        protected void btnDown_Click(object sender, EventArgs e)
        {
            // 没有项,或最后一个选中节点已经是第一项,则不处理
            if (this.fListBox.Items.Count == 0 || this.fListBox.Items[this.fListBox.Items.Count - 1].Selected) return;
            for (int i = this.fListBox.Items.Count-1; i >-1 ; i--)
            {
                if (this.fListBox.Items[i].Selected)
                    this.ChangeProperty(this.fListBox.Items[i], this.fListBox.Items[i+1]);
            }
        }
    
        private void ChangeProperty(ListItem fFront, ListItem fCurrent)
        {
            string fText = fFront.Text;
            string fValue = fFront.Value;
            bool fSelected = fFront.Selected;
    
            fFront.Text = fCurrent.Text;
            fFront.Value = fCurrent.Value;
            fFront.Selected = fCurrent.Selected;
    
            fCurrent.Text = fText;
            fCurrent.Value = fValue;
            fCurrent.Selected = fSelected;
        }
    }

  • 相关阅读:
    Spark的协同过滤.Vs.Hadoop MR
    IAAS: IT公司去IOE-Alibaba系统构架解读
    八种Docker容器开发模式解析
    Docker的特性解析
    Docker的官网在线--中文教程
    三维重建:深度相机方案对比-KinectFusion的基本原理(尺度)
    ES: 机器学习、专家系统、控制系统的数学映射
    算法设计之—常用算法之-分支界定法
    图像特征综述
    CaptCha的现状与未来
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234255.html
Copyright © 2020-2023  润新知