zoukankan      html  css  js  c++  java
  • asp.net ListBox单选、全选、清除等功能

    1:注意要得把两个listbox的selectectionmode属性设置为mutiple

    2 后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class TestListBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                ListBox2Bind();
              
            }
        }
    
        private void ListBox2Bind()
        {
            string[] str = new string[] { "北京","上海","福建","广东","浙江" };
            for (int i = 0; i < str.Length - 1; i++)
            {
                ListBox2.Items.Add(str[i]);
            }
    
        }
        protected void btnAdd_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);
                    ListBox3.Items.Add(item);
                    index--;
                }
                index++;
            }
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            int count = ListBox3.Items.Count;
            int index = 0;
            for (int i = 0; i < count; i++)
            {
                ListItem item = ListBox3.Items[index];
                if (ListBox3.Items[index].Selected == true)
                {
                    ListBox3.Items.Remove(item);
                    ListBox2.Items.Add(item);
                    index--;
                }
                index++;
            }
    
        }
        protected void AddAll_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);
                ListBox3.Items.Add(item);
            }
            index++;
        }
        protected void DelAll_Click(object sender, EventArgs e)
        {
            int count = ListBox3.Items.Count;
            int index = 0;
            for (int i = 0; i < count; i++)
            {
                ListItem item = ListBox3.Items[index];
                ListBox3.Items.Remove(item);
                ListBox2.Items.Add(item);
            }
            index++;
        }
    }
    

     3:效果图

  • 相关阅读:
    Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类
    ssh通过pem文件登陆服务器
    maven私服nexus上传第三方jar包以及下载
    springboot不占用端口启动
    springboot2.x纯注解整合dubbo
    mysql5.7 group by语法 1055
    java读取pdf文本转换html
    java 记录对象前后修改的内容(工具类)
    在docker中运行mysql实例
    Linux编辑启动停止重启springboot jar包脚本
  • 原文地址:https://www.cnblogs.com/thbbsky/p/3092842.html
Copyright © 2011-2022 走看看