当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 为.net中的ListBox控件添加双击事件

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 为.net中的ListBox控件添加双击事件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 100 ::
收藏到网摘: n/a


我在用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;
";
//注册该 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(“属性名称”,“函数名称或代码”);