小编在做一个项目时被C# listbox双击难住了,虽然是很基础的内容,但是查到的信息不是很准确,我就只好集百家之鉴了,总结了一个准确的方法,来和大家分享。
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
}
if(!Page.IsStartupScriptRegistered("listbox1"))
{
ListBox_DblClick("listbox1",Page,ListBox1,"ListBox1","ListBox2");
}
if(!Page.IsStartupScriptRegistered("listbox2"))
{
ListBox_DblClick("listbox2",Page,ListBox2,"ListBox2","ListBox1");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
下面是另一段代码,这是实现C# listbox双击的关键部分,好仔细研究哦!
public void ListBox_DblClick(string Key,Page page,System.Web.UI.WebControls.WebControl webcontrol,
string RemoveListBox,string AddListBox)
{
RemoveListBox = "document.Form1." + RemoveListBox;
AddListBox = "document.Form1." + AddListBox;
string mflistboxjs = ";
mflistboxjs += "{";
mflistboxjs += "var addOption=document.createElement('option'); \n";
mflistboxjs += "var index1; \n";
mflistboxjs += "if(RemoveListBox.length==0)return(false);\n";
mflistboxjs += "index1=RemoveListBox.selectedIndex; \n ";
mflistboxjs += "if(index1<0)return(false);\n";
mflistboxjs += "addOption.text=RemoveListBox.options(index1).text; \n";
mflistboxjs += "addOption.value=RemoveListBox.value; \n";
mflistboxjs += "AddListBox.add(addOption); \n";
mflistboxjs += "RemoveListBox.remove (index1) \n";
mflistboxjs +="}";
mflistboxjs += "";
page.RegisterStartupScript(Key,mflistboxjs);
webcontrol.Attributes.Add("onDblClick","change(" + RemoveListBox + "," + AddListBox + ");");
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
现在对怎么样实现C# listbox双击事件一目了然了吧!
【编辑推荐】