zoukankan      html  css  js  c++  java
  • webform中listbox运用,2个相互传值练习1:

    AppendDataBoundItems(将数据绑定项追加到静态声明列表项上)属性改为Ture;SelectionMode(列表的选择模式改为多项)属性改为Multiple

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {  if(!IsPostBack)
            {
                testDataContext _contect = new testDataContext();
                 ListBox1.DataSource=  _contect.Nation;
                 ListBox1.DataTextField = "Name";
                 ListBox1.DataValueField = "Code";
                 ListBox1.DataBind();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {     
               int n = 0;
               //用foreach去循环判断ListBox1.Items集合中每一项是否被选中
               foreach(ListItem item in ListBox1.Items )
               {
                   //首先判断是否有没有被选中,需要在外定义一个int n=0;变量,如果n大于零有被选中,就可以在另一个listbox中添加
                   if(item.Selected)
                   {
                       n++;
    
                   }
               }
                 //判断n的值
                   if (n == 0)
                   {
    
    
                   }
                   else
                   {   
                       foreach(ListItem item in ListBox1.Items)
                       {
                           //如果被选中添加
                           if (item.Selected)
                           {   
                               //如果在listbox2中含有添加的,进行判断,如果重复不操作,没有的添加进去
                               if (ListBox2.Items.Contains(item))
                               { }
                               else
                               { ListBox2.Items.Add(item); }
                           }
                       }
                   }
    
               }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //给ListBox2全部添加时先清空下
            ListBox2.Items.Clear();//清空ListBox2数据
            foreach (ListItem item in ListBox1.Items)
            {   
                ListBox2.Items.Add(item);
            }
        }
    }
  • 相关阅读:
    在虚拟机VM中安装的Ubuntu上安装和配置Hadoop
    初识Hadoop
    Hold住:坚持的智慧
    《人生若只如初见》读后感
    EAS部署:linux 下安装EAS后启动不了服务
    修改Tomcat默认端口
    IntelliJ IDEA工具使用总结
    Mac OSX 包管理工具
    Mac 下安装、卸载Java 7
    MySQL快速命令
  • 原文地址:https://www.cnblogs.com/franky2015/p/4872145.html
Copyright © 2011-2022 走看看