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

來源:互聯網
上載者:User
控制項

我在用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 +                                 ");");
                      }
                    在該方法中,SourceControlName是要綁定雙擊事件的控制項,TargetControlName是接收雙擊事件選定項的控制項。    
                    這裡有一個問題,如何讓對象作為參數傳給javascript的change函數,我這裡採用的是用  SourceControlName  ,TargetControlName 來傳遞兩個ListBox的Name, 然後與“document.Form1.“組合成一個串來傳遞給javascript的change函數,即
                            SourceControlName = "document.Form1." +  SourceControlName;
                           TargetControlName = "document.Form1." +  TargetControlName;

        第三:如何為控制項添加雙擊事件?
                    用ControlName.Attributes.Add(“屬性名稱”,“函數名稱或代碼”);



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。