一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net C#给ListBox的双击事件二种方法

时间:2022-06-25 04:28:56 编辑:袖梨 来源:一聚教程网

asp教程.net c#给listbox的双击事件二种方法

listbox的双击事件,在前台页面上listbox是没有双击事件的所以要通过脚本来添加,具体如下:

在前台页面上放置一个控件,简单测试时先往此控件中手动添加几个值,并在前台放一个隐藏域来存放你是否双击的值如:

,用true和false来表示。

   在前台上添加

在后台就这么写:

在pageload事件中写如下:

if(ispostback){

    if(hdvalue.value == “true”)

             lsb_**_dbclick();

}

然后在后台中添加lsb_**_dbclick()这个方法。在前台上方一个label用来显示你双击的项是否是你选择的。如:

  protected void lsb_**_dbclick(){

      this.label1.text = “双击了“+lsb_**.selecteditem.tostring();

}

这样就可以为listbox添加双击事件了。


方法二

通过在一个listbox中双击,把选中的项添加到另一个listbox中,但listbox控件本身并没有该事件,那么如何实现呢?我就想到了客户端脚本javascrit,通过查阅相关资料,终于把这个问题解决了,现在写出来与大家分享,希望能对大家有所帮助。
        这里有三个问题:
        第一:双击所要执行的网页特效代码是什么?
                    注意: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 = "";
                            //注册该 javascript ;
                           page.registerstartups教程cript("",js);
                            //为控件添加双击事件;
                           webcontrol.attributes.add("ondblclick","change(" + sourcecontrolname + "," + targetcontrolname     +                                 ");");
                      }
                    在该方法中,sourcecontrolname是要绑定双击事件的控件,targetcontrolname是接收双击事件选定项的控件。   
                    这里有一个问题,如何让对象作为参数传给javascript的change函数,我这里采用的是用  sourcecontrolname  ,targetcontrolname 来传递两个listbox的name, 然后与“document.form1.“组合成一个串来传递给javascript的change函数,即
                            sourcecontrolname = "document.form1." +  sourcecontrolname;
                           targetcontrolname = "document.form1." +  targetcontrolname;


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

热门栏目