日斋
日新月异

做网站,有时候会设计两个ListBox  当双击ListBoxA里面的项 可以把该项 增加到 ListBoxB中 同时remove掉ListBoxA的该项可是.net中ListBox并没有双击的事件,在网上看了资料,实现的方法不少,有用javascript方法实现的也有重写ListBox控件的。。。这里介绍下用Javascript的方法

假设有两个ListBox要处理

 

CS文件中的代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initListBox();
            ListBox1.Attributes.Add("ondblclick", "ChangeItem('" + ListBox1.ClientID + "','" + ListBox2.ClientID + "')");
            ListBox2.Attributes.Add("ondblclick", "ChangeItem('" + ListBox2.ClientID + "','" + ListBox1.ClientID + "')");
        }
    }

 

Js代码

function ChangeItem(l1,l2)
{
    var cc = document.getElementById(l1).options[window.document.getElementById(l1).selectedIndex].value;
    var dd = document.getElementById(l1).options[window.document.getElementById(l1).selectedIndex].text;
    //alert(dd + ":" + cc);
    document.getElementById(l1).options.remove(window.document.getElementById(l1).selectedIndex);
    var op = new Option(dd,cc,false,false);
    document.getElementById(l2).options.add(op);
}
 

同样这两段代码可以应用在更多的ListBox,相比重写ListBox要简单一点。

posted on 2010-12-12 16:41  李承隆  阅读(482)  评论(0编辑  收藏  举报