ListBox基本功能(ListBox1---ListBox换)

 虽然在之前有做过类似功能:但有些问题,这次就重写吧。

ListBox基本功能首先是列表项的添加,客户端实现代码添加在listbox实例化代码中间,例如:
<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>

若在服务器端实现,为避免每次加载时执行添加列表项,上述代码包含在下面代码中:
if(!IsPostBack)
{
}
WebForm页面上须添加2个listbox(listbox1和lixtbox2)和2个命令按钮,listbox1不为空。列表项从listbox1添加到listbox2须在Button1单击事件中调用Add方法:
ListBox2.Items.Add(ListBox1.SelectedValue);

若要从listbox2中删除列表项的话须在Button2单击事件中调用Remove方法:
ListBox2.Items.Remove(ListBox2.SelectedValue);

 

列表项从listbox1添加到listbox2后,列表项从listbox1中删除:
int i=0;
while(i<ListBox1.Items.Count)
{
if(ListBox1.Items[i].Selected==true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
else
i+=1;
}

这样只能实现单项添加,想要实现多项添加,首先设置ListBox1的SelectionMode属性值Multiple,ListBox1允许多项选中。

 

这样只能实现单项添加,想要实现多项添加,首先设置ListBox1的SelectionMode属性值Multiple,ListBox1允许多项选中。

在Button1单击事件中添加
foreach(ListItem MyItem in ListBox1.Items)
if(MyItem.Selected==true)
ListBox2.Items.Add(MyItem);

想要一次清空ListBox2中所有选项可在Button2单击事件中调用clear方法,
ListBox2.Items.Clear();

若列表项已经添加,不允许二次添加,Button1单击事件中的代码包含在:
if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)
{
}


ListBox与数据库绑定就是指定他的DataSource和DataTextField属性,
ListBox2.DataSource=数据源;
ListBox2.DataTextField="字段名";
ListBox2.DataBind();

 

----------------------------------------------------------------------------------------------------------------------

我的做法:

控件ListBox1和ListBox2,btn1(>),btn2(>>),btn3(<),btn4(<<)

ListBox邦定控件根据ddl关联。

多选:

ListBox1和ListBox2 设置SelectionMode属性值Multiple

后台做法:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Eflow_WkHourGrpAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
 
    }
    protected void but1_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < ListBox1.Items.Count)
        {
            if (ListBox1.Items[i].Selected == true)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
            else
                i += 1;
        }
        /*
        if (ListBox2.Items.FindByValue(ListBox1.SelectedValue) == null)
        {
            DataAccess.MessageObject.Show(ListBox1.SelectedValue.ToString());
        }
        */
       /*
        foreach (ListItem MyItem in ListBox1.Items)
            if (MyItem.Selected == true)
                ListBox2.Items.Add(MyItem);
        */
    }
    protected void but2_Click(object sender, EventArgs e)
    {
        ListBox1.DataBind();
        ListBox2.DataSource = ListBox1.Items;
        ListBox2.DataBind();
        ListBox1.Items.Clear();
    }
    protected void but3_Click(object sender, EventArgs e)
    {
        int i = 0;
        while (i < ListBox2.Items.Count)
        {
            if (ListBox2.Items[i].Selected == true)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
            else
                i += 1;
        }
    }
    protected void but4_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Clear();
        ListBox1.DataBind();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值