将双击事件添加到 net
墨初 知识笔记 74阅读
我在用dotnet做项目的过程中遇到了一个ListBox问题:在一个ListBox中双击,选中的项目被添加到另一个ListBox中,但是ListBox控件本身没有这个事件,那么如何实现呢?想到了客户端脚本javascrit,最后通过查阅相关资料解决了这个问题。现在写出来分享给大家,希望对你有帮助。这里有三个问题:第一,双击要执行的javascript代码是什么?1函数change()2 { 3 varaddoption=document。createelement(' option ');4varindex1五个if(document . form 1 . listbox 1 . length==0)返回(false);6index1=文档。form 1 . listbox 1 . selectedindex;7if(index10)返回(false);8addOption.text=document。Form1.ListBox1.options(索引1)。文本;9addOption.value=document。form 1 . listbox 1 . value;10
document.Form1.ListBox2.add(addOption);11 document.Form1.ListBox1.remove (index1);
12 }
13
第二:如何将 javas cript 代码转换为C#代码?
1 public static void ListBox_DblClick(Page page,System.Web.UI.WebControls.WebControl webcontrol,string SourceControlName,string TargetControlName)
2 {
3 SourceControlName = "document.Form1." + SourceControlName;
4 TargetControlName = "document.Form1." + TargetControlName;
5 string js = "<s cript language=javas cript> function change(SourceControlName,TargetControlName)";
6 js += "{";
7 js += "var addOption=document.createElement('option'); \n";
8 js += " var index1; \n";
9 js += "if(SourceControlName.length==0)return(false);\n";
10 js += " index1=SourceControlName.selectedIndex; \n ";
11 js += " if(index1<0)return(false);\n";
12 js += " addOption.text=SourceControlName.options(index1).text; \n";
13 js += "addOption.value=SourceControlName.value; \n";
14 js += "TargetControlName.add(addOption); \n";
15 js += "SourceControlName.remove (index1) \n";
16 js +="}";
17 js += "</s cript>";
18 //注册该 javas cript ;
19 page.RegisterStartups cript("",js);
20 //为控件添加双击事件;
21 webcontrol.Attributes.Add("onDblClick","change(" + SourceControlName + "," + TargetControlName + ");");
22 }
23
24
在该方法中,SourceControlName是要绑定双击事件的控件,TargetControlName是接收双击事件选定项的控件。 2 {
3 SourceControlName = "document.Form1." + SourceControlName;
4 TargetControlName = "document.Form1." + TargetControlName;
5 string js = "<s cript language=javas cript> function change(SourceControlName,TargetControlName)";
6 js += "{";
7 js += "var addOption=document.createElement('option'); \n";
8 js += " var index1; \n";
9 js += "if(SourceControlName.length==0)return(false);\n";
10 js += " index1=SourceControlName.selectedIndex; \n ";
11 js += " if(index1<0)return(false);\n";
12 js += " addOption.text=SourceControlName.options(index1).text; \n";
13 js += "addOption.value=SourceControlName.value; \n";
14 js += "TargetControlName.add(addOption); \n";
15 js += "SourceControlName.remove (index1) \n";
16 js +="}";
17 js += "</s cript>";
18 //注册该 javas cript ;
19 page.RegisterStartups cript("",js);
20 //为控件添加双击事件;
21 webcontrol.Attributes.Add("onDblClick","change(" + SourceControlName + "," + TargetControlName + ");");
22 }
23
24
这里有一个问题,如何让对象作为参数传给javas cript的change函数,我这里采用的是用 SourceControlName ,TargetControlName 来传递两个ListBox的Name, 然后与“document.Form1.“组合成一个串来传递给javas cript的change函数,即
SourceControlName = "document.Form1." + SourceControlName;
TargetControlName = "document.Form1." + TargetControlName;
第三:如何为控件添加双击事件?
用ControlName.Attributes.Add(“属性名称”,“函数名称或代码”);

标签: