为.net中的ListBox控件添加双击事件
点击次数:105 次 发布日期:2008-11-06 07:55:51 作者:源代码网
|
这里有三个问题: 第一:双击所要执行的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 ); "; js += " var index1; "; js += "if(SourceControlName.length==0)return(false); "; js += " index1=SourceControlName.selectedIndex; "; js += " if(index1<0)return(false); "; js += " addOption.text=SourceControlName.options(index1).text; "; js += "addOption.value=SourceControlName.value; "; js += "TargetControlName.add(addOption); "; js += "SourceControlName.remove (index1) "; js +="}"; js += "</script>"; //注册该 javascript ; page.RegisterStartupScript("",js); //为控件添加双击事件; webcontrol.Attributes.Add("onDblClick","change(" + SourceControlName + "," + TargetControlName + ");"); } 源代码网推荐 源代码网供稿. |
