DropDownList与ListBox关联及ListBox添加删除
前台代码:
(另外还有几个Hidden控件)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<table><tr align="left"><td align="right">管理员部门:</td><td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList></td></tr>
<tr><td colspan="2">
<table width="100%">
<tr>
<td>
<asp:ListBox ID="ListBox1" runat="server" Height="100" Width="100"></asp:ListBox></td>
<td style="width: 5px" valign="middle">
<table > <tr>
<td>
<a href="javascript:toright()"><img src="../Images/btn_toright.gif" style="border:0px;" /></a>
</td>
</tr>
<tr>
<td>
<a href="javascript:toleft()"><img src="../Images/btn_toleft.gif" style="border:0px;" /></a>
</td>
</tr></table>
</td>
<td>
<asp:ListBox ID="ListBox2" runat="server" Height="100" Width="100"></asp:ListBox></td>
</tr>
</table>
Javascript:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function toright()
{
var textstr="";
var valuestr="";
var thisid=0;
var i=0;
var len=document.getElementById('<%= ListBox1.ClientID %>').options.length;
var len2=document.getElementById('<%= ListBox2.ClientID %>').options.length;
i = document.getElementById('<%= ListBox1.ClientID %>').selectedIndex;
if(i>-1)
{
textstr=document.getElementById('<%= ListBox1.ClientID %>').options[i].text;
valuestr=document.getElementById('<%= ListBox1.ClientID %>').options[i].value;
thisid=i;
document.getElementById('<%= ListBox2.ClientID %>').options[len2] = new Option(textstr,valuestr,true,true);
document.getElementById('<%= ListBox1.ClientID %>').options[i].parentNode.removeChild(document.getElementById('<%= ListBox1.ClientID %>').options[i]);
if(document.getElementById('<%= ListBox1.ClientID %>').options.length>0)
{
document.getElementById('<%= ListBox1.ClientID %>').selectedIndex = 0;
}
if(document.getElementById('<%= Hidden1.ClientID %>').value=="")
document.getElementById('<%= Hidden1.ClientID %>').value=valuestr;
else
document.getElementById('<%= Hidden1.ClientID %>').value+="|"+valuestr;
}
}
function toleft()
{
var textstr="";
var valuestr="";
var thisid=0;
var i=0;
var len=document.getElementById('<%= ListBox2.ClientID %>').options.length;
var len2=document.getElementById('<%= ListBox1.ClientID %>').options.length;
document.getElementById('<%= Hidden1.ClientID %>').value="";
i = document.getElementById('<%= ListBox2.ClientID %>').selectedIndex;
if(i>-1)
{
textstr=document.getElementById('<%= ListBox2.ClientID %>').options[i].text;
valuestr=document.getElementById('<%= ListBox2.ClientID %>').options[i].value;
thisid=i;
document.getElementById('<%= ListBox1.ClientID %>').options[len2] = new Option(textstr,valuestr,true,true);
document.getElementById('<%= ListBox2.ClientID %>').options[i].parentNode.removeChild(document.getElementById('<%= ListBox2.ClientID %>').options[i]);
if(document.getElementById('<%= ListBox2.ClientID %>').options.length>0)
{
document.getElementById('<%= ListBox2.ClientID %>').selectedIndex=0;
}
}
for(i=0;i<document.getElementById('<%= ListBox2.ClientID %>').options.length;i++)
{
if(document.getElementById('<%= Hidden1.ClientID %>').value=="")
document.getElementById('<%= Hidden1.ClientID %>').value=document.getElementById('<%= ListBox2.ClientID %>').options[i].value;
else
document.getElementById('<%= Hidden1.ClientID %>').value+="|"+document.getElementById('<%= ListBox2.ClientID %>').options[i].value;
}
}
后台代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void initlist(string DepartmentCode,string mli)
{
string[] arr = mli.Split('|');
ListItem li = null;
ListBox1.Items.Clear();
ListBox2.Items.Clear();
string id = "";
string name = "";
if (DepartmentCode != "")
ds = userbll.GetList("UnitCode='" + Hidden7.Value + "' and DepartmentCode='" + DepartmentCode + "'");
else
ds = userbll.GetList("UnitCode='" + Hidden7.Value + "'");
Hidden1.Value = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
id = ds.Tables[0].Rows[i]["ID"].ToString();
name = ds.Tables[0].Rows[i]["CName"].ToString();
if (arr.Length > 0)
{
for (int j = 0; j < arr.Length; j++)
{
li = new ListItem(name, id);
if (id == arr[j])
{
ListBox2.Items.Add(li);
if (Hidden1.Value == "")
Hidden1.Value += id;
else
Hidden1.Value += "|" + id;
}
else
{
ListBox1.Items.Add(li);
}
}
}
else
{
li = new ListItem(name, id);
ListBox1.Items.Add(li);
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem li = null;
ListBox1.Items.Clear();
string id = "";
string name = "";
if (DropDownList1.SelectedValue != "0")
{
ds = userbll.GetList("UnitCode='" + Hidden7.Value + "' and DepartmentCode='" + DropDownList1.SelectedValue + "'");
}
else
{
ds = userbll.GetList("UnitCode='" + Hidden7.Value + "' and DepartmentCode is null");
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
id = ds.Tables[0].Rows[i]["ID"].ToString();
name = ds.Tables[0].Rows[i]["CName"].ToString();
li = new ListItem(name, id);
ListBox1.Items.Add(li);
}
}