原创: ASP.NET ListBox 添加移除操作,包括js、后台
前台:
<div style="float: left">
<!-- <span style="font-weight: bold;">产品信息</span><br />-->
<asp:ListBox runat="server" Height="350px" Width="230px" SelectionMode="Multiple"
ID="lbAppAllList"></asp:ListBox>
</div>
<div style="float: left; padding-left: 60px; margin-right: 60px; margin-top: 80px">
<asp:Button ID="butAdd" Style="" runat="server" Text=" >> " OnClick="butAdd_Click"
Visible="false" />
<input type="button" name="Submit" value=" >> " onclick="addIt()" style="display: block" />
<br />
<br />
<br />
<asp:Button ID="butRemove" Style="" runat="server" Text=" << " OnClick="butRemove_Click"
Visible="false" />
<input type="button" name="Submit2" value=" << " onclick="deleteIt()" style="display: block" />
</div>
<div style="float: left">
<!-- <span style="font-weight: bold;">产品信息</span><br />-->
<asp:ListBox runat="server" Height="350px" Width="230px" SelectionMode="Multiple"
ID="LbAPPList"></asp:ListBox>
</div>
<br />
<div style="clear: both; margin-left: 25%;">
<asp:Button ID="butConfirm" Style="text-align: center;" runat="server" Text=" 确 定 "
OnClick="butConfirm_Click" OnClientClick="return GetValue();" />
</div>
<asp:HiddenField runat="server" ID="hidAPPID" />
<script language="JavaScript" type="text/javascript">
<!--
function addIt() {
var input = document.getElementById("<%=lbAppAllList.ClientID%>");
var output = document.getElementById("<%=LbAPPList.ClientID%>");
for (i = 0; i < input.length; i++) {
if (input[i].selected == true) {
if (output.length == 0) {
var option = new Option();
option.text = input[i].innerText;
option.value = input[i].value;
output.add(option);
}
var isExist = false;
for (j = 0; j < output.length; j++) {
if (output[j].text == input[i].innerText) {
isExist = true;
break;
}
}
if (isExist == false) {
var option = new Option();
option.text = input[i].innerText;
option.value = input[i].value;
output.add(option);
}
}
}
}
function deleteIt() {
var output = document.getElementById("<%=LbAPPList.ClientID%>");
for (i = 0; i < output.length; i++) {
if (output[i].selected == true) {
output.options.removeChild(output[i--]);
}
}
}
function GetValue() {
var strlist = document.getElementById("<%=LbAPPList.ClientID%>"); //获取Listbox
var str = "";
//遍历Listbox,取得选中项的值
if (strlist.options.length > 0) {
for (var i = 0; i < strlist.options.length; i++) {
// if (strlist.options[i].selected == true) {
var j = strlist.options[i].value;
str += j + ","; //把Value值串起来
// }
}
var strValue = str.replace(/,$/, ""); //去掉最后一个逗号
//alert(strValue);
var hid = document.getElementById("<%=hidAPPID.ClientID%>");
hid.value = strValue;
alert(hid.value);
}
else {
var hid = document.getElementById("<%=hidAPPID.ClientID%>");
hid.value = str;
}
}
// -->
</script>
后台:
//添加
protected void butAdd_Click(object sender, EventArgs e)
{
int count = lbAppAllList.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = lbAppAllList.Items[index];
if (lbAppAllList.Items[index].Selected == true)
{
lbAppAllList.Items.Remove(item);
LbAPPList.Items.Add(item);
index--;
}
index++;
}
}
//移除
protected void butRemove_Click(object sender, EventArgs e)
{
int count = LbAPPList.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = LbAPPList.Items[index];
if (LbAPPList.Items[index].Selected == true)
{
LbAPPList.Items.Remove(item);
lbAppAllList.Items.Add(item);
index--;
}
index++;
}
}
//确定
protected void butConfirm_Click(object sender, EventArgs e)
{
string[] atts = strAppID.Split(',');
if (atts.Length > 0 && atts[0].ToString() != "")
{
for (int i = 0; i < atts.Length; i++)
{
string[] list = atts[i].Split('*');
model.APPID = list[1];
model.ITEMNAME = list[0];
}
}
}
转载请标注出处!