做网站,有时候会设计两个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要简单一点。

相关文章:

  • 2021-09-09
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-24
  • 2022-12-23
  • 2021-08-18
  • 2022-01-19
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案