两个listbox 无刷新互动

时间:2023-03-09 09:20:25
两个listbox 无刷新互动

页面代码:

 <table border="0" cellpadding="0" cellspacing="0" style="width: 32%">
<tr>
<td>
<asp:ListBox ID="lbLeft" runat="server" Width="150px" Height="200" DataTextField="Name" DataValueField="ID" SelectionMode="Multiple"></asp:ListBox>
</td>
<td><input id="btnAll" onclick="SelectAll()" type="button" class="C_input02" value=">>" />
<br /><br />
<input id="btnAdd" onclick="SelectOne()" type="button" class="C_input02" value=">" /><br /><br />
<input id="btnDel" onclick="DelOne()" type="button" class="C_input02" value="<" /><br /><br />
<input id="btnDAll" onclick="DelAll()" type="button" class="C_input02" value="<<" />
</td>
<td>
<asp:ListBox ID="lbRight" runat="server" Width="150px" SelectionMode="Multiple" Height="200"></asp:ListBox>
</td>
</tr>
</table>
<input id="txtProvider" runat="server" style="display:none" />

javascript代码:

 <script type="text/javascript">
function SelectAll()
{
var lst1=window.document.getElementByIdx_x_x_x("<%=lbLeft.ClientID %>");
var length = lst1.options.length;
var string = window.document.getElementByIdx_x_x_x("<%=txtProvider.ClientID %>")
for(var i=0;i<length;i++)
{
var v = lst1.options[i].value;
var t = lst1.options[i].text;
var lst2=window.document.getElementByIdx_x_x_x("<%=lbRight.ClientID %>");
lst2.options[i] = new Option(t,v,true,true);
string.value+=v+",";
}
} function DelAll()
{
var lst2=window.document.getElementByIdx_x_x_x("<%=lbRight.ClientID %>");
var string = window.document.getElementByIdx_x_x_x("<%=txtProvider.ClientID %>")
var length = lst2.options.length;
for(var i=length;i>0;i--)
{
lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
}
string.value = "";
} function SelectOne()
{
var string = window.document.getElementByIdx_x_x_x("<%=txtProvider.ClientID %>")
var lst1=window.document.getElementByIdx_x_x_x("<%=lbLeft.ClientID %>");
var lst2=window.document.getElementByIdx_x_x_x("<%=lbRight.ClientID %>");
var lstindex=lst1.selectedIndex;
var length = lst2.options.length;
var isExists = false;
if(lstindex<0)
return;
else if(length != null)
{
for(var i=0;i < length; i++)
{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
{
isExists = true;
}
}
}
else
{
return;
}
if (isExists == false)
{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
string.value+=v+",";
}
else
{
alert("所选条目数据已经存在");
return false;
}
} function DelOne()
{
var lst2=window.document.getElementByIdx_x_x_x("<%=lbRight.ClientID %>");
var string = window.document.getElementByIdx_x_x_x("<%=txtProvider.ClientID %>")
var lstindex=lst2.selectedIndex;
if(lstindex>=0)
{
var v = lst2.options[lstindex].value+";";
var va = lst2.options[lstindex].value+",";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
string.value = string.value.replace(va,"");
}
}
</script>

兼容IE、火狐