为.net中的ListBox控件添加双击事件-.NET教程,组件控件开发

来源:互联网 发布:小米家庭数据存储中心 编辑:程序博客网 时间:2024/05/09 11:18

refer to: http://www.sudu.cn/info/html/edu/20071226/25503.html

我在用dotnet做一个项目的过程中,遇到了一个listbox的问题:通过在一个listbox中双击,把选中的项添加到另一个listbox中,但listbox控件本身并没有该事件,那么如何实现呢?我就想到了客户端脚本javascrit,通过查阅相关资料,终于把这个问题解决了,现在写出来与大家分享,希望能对大家有所帮助。
        这里有三个问题:
        第一:双击所要执行的javascript代码是什么?
                    注意:javascript代码的语法要正确,即每一行都要以“;”结尾;
                    function change()
                        {
                             var addoption=document.createelement("option");
                             var index1;
                             if(document.form1.listbox1.length==0)return(false);
                              index1=document.form1.listbox1.selectedindex; 
                             if(index1<0)return(false);
                              addoption.text=document.form1.listbox1.options(index1).text;
                              addoption.value=document.form1.listbox1.value;
                             document.form1.listbox2.add(addoption);
                             document.form1.listbox1.remove (index1);
                         }
        第二:如何将 javascript 代码转换为c#代码?
                    public static void listbox_dblclick(page page,system.web.ui.webcontrols.webcontrol webcontrol,string                                 sourcecontrolname,string targetcontrolname)
                     {
                           sourcecontrolname = "document.form1." +  sourcecontrolname;
                           targetcontrolname = "document.form1." +  targetcontrolname;

 string js = "<script language=javascript> function change(sourcecontrolname,targetcontrolname)";
                           js += "{";
                           js +=     "var addoption=document.createelement( option ); \n";
                           js += "  var index1; \n";
                           js += "if(sourcecontrolname.length==0)return(false);\n";
                           js += "  index1=sourcecontrolname.selectedindex; \n ";
                           js += "  if(index1<0)return(false);\n";
                           js += " addoption.text=sourcecontrolname.options(index1).text; \n";
                           js += "addoption.value=sourcecontrolname.value; \n";
                           js += "targetcontrolname.add(addoption); \n";
                           js += "sourcecontrolname.remove (index1) \n";                            js +="}";
                           js += "</script>";
                            //注册该 javascript ;
                           page.registerstartupscript("",js);
                            //为控件添加双击事件;
                           webcontrol.attributes.add("ondblclick","change(" + sourcecontrolname + "," + targetcontrolname +                                 ");");
                      }
           &nbsp;        在该方法中,sourcecontrolname是要绑定双击事件的控件,targetcontrolname是接收双击事件选定项的控件。    
                    这里有一个问题,如何让对象作为参数传给javascript的change函数,我这里采用的是用  sourcecontrolname  ,targetcontrolname 来传递两个listbox的name, 然后与“document.form1.“组合成一个串来传递给javascript的change函数,即 
                            sourcecontrolname = "document.form1." +  sourcecontrolname;
                           targetcontrolname = "document.form1." +  targetcontrolname;

        第三:如何为控件添加双击事件?
                    用controlname.attributes.add(“属性名称”,“函数名称或代码”);


原创粉丝点击