在网上找了一下代码,不过本人愚笨,不知道怎么调用.
代码如下:
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 + ");");
}
请问怎么调用这个函数?在哪个地方调用?又是怎么样给它传递参数的?
谢谢各位!
代码如下:
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 + ");");
}
请问怎么调用这个函数?在哪个地方调用?又是怎么样给它传递参数的?
谢谢各位!
this.ListBox1.Attributes["ondblclick"]="additem();";然后在aspx页面加上你的操作javascript代码:
<script>
function additem()
{
...
}
</script>
可是这个函数是作什么用的呢?
请各位明示
这个方法执行后,在客户端输出一段javascript代码到HTML里。
所以,用我上面的方法就可以了,可以不需要这样来做,最后实现的功能应该是一样的。>>>>我是想给ListBox的ListItem添加双击事件,但不是给整个ListBox控件添加双击事件
其实,上面你找的那段代码也是给ListBox加客户端的双击事件,而不是给某个ListItem(见:webcontrol.Attributes.Add("onDblClick","....)。
另外,在客户端,你所指的ListItem就类似于是options,它“Retrieves a collection of the option objects in a select object.”,应该是不可以给它加事件的。
为什么不能给ListItem添加双击事件?还是搞不明白.人家究竟是怎么做得的?
你的意思是从左边ListBox移一项到右边吗?
你可以使用javascript向右边的ListBox里面加一项,value和text跟你左边选中项的值一样,然后把左边的选中项remove掉就可以了。>>>>为什么不能给ListItem添加双击事件?还是搞不明白.人家究竟是怎么做得的?
请问哪里有代码实现了呢?上面的代码应该不是的。
双击事件只要添加给控件本身就可以了。
一个例子很明了,不需解释!在"老猫的理想"那里有一个很好的例子.