查阅网上资料,参照做了一个listbox双击事件,可是不行,请问错在哪儿?谢谢了先?
Default.aspx中代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" Height="172px" Width="105px">
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
<asp:ListItem>ddd</asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Height="170px" Width="103px">
<asp:ListItem>eee</asp:ListItem>
<asp:ListItem>fff</asp:ListItem>
</asp:ListBox></div>
</form>
</body>
</html>
Default.aspx.CS中的代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
}
if (!Page.IsStartupScriptRegistered("listbox1"))
{
ListBox_DblClick("listbox1",Page,ListBox1,"ListBox1","ListBox2");
}
if (!Page.IsStartupScriptRegistered("listbox2"))
{
ListBox_DblClick("listbox2",Page,ListBox2,"ListBox2","ListBox1");
}
}
public void ListBox_DblClick(string Key,Page page,System.Web.UI.WebControls.WebControl webcontrol,string RemoveListBox,string AddListBox)
{
RemoveListBox = "document.Form1." + RemoveListBox;
AddListBox = "document.Form1." + AddListBox;
string mflistboxjs = "<script language=javascript> function change(RemoveListBox,AddListBox)";
mflistboxjs+="{";
mflistboxjs+="var addOption=document.createElement('option');\n";
mflistboxjs+="var index1;\n";
mflistboxjs+="if(RemoveListBox.length==0)return(false);\n";
mflistboxjs+="index1=RemoveListBox.selectedIndex;\n ";
mflistboxjs+="if(index1<0)return(false);\n";
mflistboxjs+="addOption.text=RemoveListBox.options(index1).text;\n";
mflistboxjs+="addOption.value=RemoveListBox.value;\n";
mflistboxjs+="AddListBox.add(addOption);\n";
mflistboxjs+="RemoveListBox.remove(index1);\n";
mflistboxjs+="}";
mflistboxjs+="</script>";
page.RegisterStartupScript(Key,mflistboxjs);
webcontrol.Attributes.Add("onDblClick","change("+RemoveListBox+","+AddListBox+");");
}
}
[此贴子已经被作者于2007-9-23 8:35:43编辑过]