熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

為.net中的ListBox控件添加雙擊事件

2013-11-13 10:06:23  來源: .NET編程 

  我在用dotnet做一個項目的過程中遇到了一個ListBox的問題通過在一個ListBox中雙擊把選中的項添加到另一個ListBox中但ListBox控件本身並沒有該事件那麼如何實現呢?我就想到了客戶端腳本javascrit通過查閱相關資料終於把這個問題解決了現在寫出來與大家分享希望能對大家有所幫助
        這裡有三個問題
        第一雙擊所要執行的javascript代碼是什麼?
                    注意javascript代碼的語法要正確即每一行都要以結尾
                    function change()
                        {
                             var addOption=documentcreateElement(option);
                             var index;
                             if(documentFormListBoxlength==)return(false);
                              index=documentFormListBoxselectedIndex; 
                             if(index<)return(false);
                              addOptiontext=documentFormListBoxoptions(index)text;
                              addOptionvalue=documentFormListBoxvalue;
                             documentFormListBoxadd(addOption);
                             documentFormListBoxremove (index);
                         }
        第二如何將 javascript 代碼轉換為C#代碼?
                    public static void ListBox_DblClick(Page pageSystemWebUIWebControlsWebControl webcontrolstring                                 SourceControlNamestring TargetControlName)
                     {
                           SourceControlName = documentForm +  SourceControlName;
                           TargetControlName = documentForm +  TargetControlName;

  string js = <script language=javascript> function change(SourceControlNameTargetControlName);
                           js += {;
                           js +=     var addOption=documentcreateElement( option ); \n;
                           js +=   var index; \n;
                           js += if(SourceControlNamelength==)return(false);\n;
                           js +=   index=SourceControlNameselectedIndex; \n ;
                           js +=   if(index<)return(false);\n;
                           js += addOptiontext=SourceControlNameoptions(index)text; \n;
                           js += addOptionvalue=SourceControlNamevalue; \n;
                           js += TargetControlNameadd(addOption); \n;
                           js += SourceControlNameremove (index) \n;                            js +=};
                           js += </script>;
                            //注冊該 javascript
                           pageRegisterStartupScript(js);
                            //為控件添加雙擊事件
                           webcontrolAttributesAdd(onDblClickchange( + SourceControlName + + TargetControlName +                                 ););
                      }
           &nbsp;        在該方法中SourceControlName是要綁定雙擊事件的控件TargetControlName是接收雙擊事件選定項的控件    
                    這裡有一個問題如何讓對象作為參數傳給javascript的change函數我這裡采用的是用  SourceControlName  TargetControlName 來傳遞兩個ListBox的Name 然後與documentForm組合成一個串來傳遞給javascript的change函數
                            SourceControlName = documentForm +  SourceControlName;
                           TargetControlName = documentForm +  TargetControlName;

        第三如何為控件添加雙擊事件?
                    用ControlNameAttributesAdd(屬性名稱函數名稱或代碼);


From:http://tw.wingwit.com/Article/program/net/201311/12569.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.