技术开发 频道

使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的


【IT168技术文档】

  这里使用到了两张表:部门表和员工表 SQL脚本如下:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Emp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[Emp] GO CREATE TABLE [dbo].[Emp] ( [EmpID] [int] IDENTITY (1, 1) NOT NULL , [EmpName] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL , [Age] [int] NULL , [Dept] [int] NULL , [DelFlag] [bit] NULL ) ON [PRIMARY] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Department]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[Department] GO CREATE TABLE [dbo].[Department] ( [DeptID] [int] IDENTITY (1, 1) NOT NULL , [Name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO
  前台代码:
<HTML> <HEAD> <title>ListBoxToListBox</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <link href="../CSS/BasicLayout.css" rel="stylesheet" type="text/css"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <style type="text/css"> .fsize { FONT-SIZE: 10pt } </style> </HEAD> <body MS_POSITIONING="GridLayout"> <script language="javascript"> function BindListEmp() { var DeptID=document.getElementById("ddlDept").value; var obj=AjaxMethod.GetEmpByDeptID(DeptID); if(obj.value!=null) { document.all("listEmployees").length=0; var ds=obj.value; if(ds != null && typeof(ds) == "object" && ds.Tables != null) { for(var i=0; i<ds.Tables[0].Rows.length; i++)      {      var name=ds.Tables[0].Rows[i].EmpName;        var id=ds.Tables[0].Rows[i].EmpID;        //alert(name);        //alert(id);        document.all("listEmployees").options.add(new Option(name,id));      } } else { } } else { } } </script> <script language="javascript"> function GetData() { listNewEmp = eval("document.FrmListBox.listNewEmp"); document.getElementById("txtEmpID").value=""; for(i=0;i<listNewEmp.length;i++) { document.FrmListBox.txtEmpID.value+=listNewEmp.options[i].value+","; } } function AddItem(ControlName) { Control = null; Control=eval("document.FrmListBox.listNewEmp"); var x=0; var i=0; var y=0; listEmployees=eval("document.FrmListBox.listEmployees"); listNewEmp=eval("document.FrmListBox.listNewEmp"); var j=listEmployees.length; for(i=0;i<j;i++) { if(listEmployees.options[i].selected==true) { //alert(Control.length); if(Control.length==0) { Control.add(new Option(listEmployees[i].text,listEmployees.options[i].value)); listNewEmp=eval("document.FrmListBox.listNewEmp"); continue; } else { for(x=0;x<listNewEmp.length;x++) { if(listEmployees.options[i].value==listNewEmp.options[x].value) { y++; } } } if(y==0) { Control.add(new Option(listEmployees[i].text,listEmployees.options[i].value)); listNewEmp=eval("document.FrmListBox.listNewEmp"); } } } } function RemoveItem(ControlName) { Control = null; Control=eval("document.FrmListBox.listNewEmp"); var j=Control.length; if(j==0) return; for(j;j>0;j--) { if(Control.options[j-1].selected==true) { Control.remove(j-1); } } } </script> <form id="FrmListBox" method="post" runat="server"> <table align="center" border="1" style="BORDER-COLLAPSE: collapse" borderColor="#ccccc" width="80%" class="fSize"> <TR> <TD style="WIDTH: 191px" align="right" width="191" height="30">公司部门:</TD> <TD height="30"><asp:dropdownlist id="ddlDept" runat="server" Width="112px"></asp:dropdownlist></TD> </TR> <TR> <TD style="WIDTH: 191px" align="right" width="191" height="30">项目成员:</TD> <TD height="30"> <TABLE id="Table3" cellSpacing="0" cellPadding="0" width="100%" border="0"> <TR align="center"> <TD style="WIDTH: 139px"> <asp:listbox id="listEmployees" runat="server" Width="141" Height="160" SelectionMode="Multiple"></asp:listbox></TD> <TD style="WIDTH: 33px"><INPUT class="buttoncss" style="WIDTH: 48px; HEIGHT: 24px" onclick="AddItem(this.name)" type="button" value=">>>>" name="btnReceSendToRight"><BR> <INPUT class="buttoncss" style="WIDTH: 48px; HEIGHT: 24px" onclick="RemoveItem(this.name)" type="button" value="<<<<" name="btnReceSendToLeft"> </TD> <TD align="left"> <asp:listbox id="listNewEmp" runat="server" Width="141" Height="160" SelectionMode="Multiple"></asp:listbox>&nbsp; </TD> </TR> </TABLE> </TD> </TR> <tr> <td height="30" align="right">存储listNew控件中所得到的值:</td> <td> <asp:TextBox id="txtEmpID" runat="server"></asp:TextBox></td> </tr> <TR> <TD align="center" colSpan="2" height="35">&nbsp; <asp:button id="btnSubmit" runat="server" Width="64" Text="确定" CssClass="redButtonCss" Height="24"></asp:button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </TD> </TR> </table> </TD></TR></TABLE> </form> </body> </HTML>
0
相关文章