两个ListBox的相互操作
两个ListBox的相互操作有四个按:全选,单选,反单选,反全选.
一.一种情况是这四个按钮用服务器控件:
二.第二种情况是用javascript实现:
一.一种情况是这四个按钮用服务器控件:
/// <summary>
/// 选中全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSelectAll_ServerClick(object sender, EventArgs e)
{
for (int i = 0; i < lb_Sourse.Items.Count; i++)
{
lb_NewName.Items.Add(new ListItem(lb_Sourse.Items[i].Text, lb_Sourse.Items[i].Value));
}
while(lb_Sourse.Items.Count > 0)
{
lb_Sourse.Items.Remove(lb_Sourse.Items[0]);
}
}
/// <summary>
/// 选中一个
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSelectOne_ServerClick(object sender, EventArgs e)
{
if (lb_Sourse.SelectedIndex != -1)
{
string StrPersonFlow = "";
for (int i = this.lb_Sourse.Items.Count; i > 0; i--)
{
if (this.lb_Sourse.Items[i - 1].Selected)
{
StrPersonFlow += this.lb_Sourse.Items[i - 1].Value + ",";
this.lb_NewName.Items.Add(this.lb_Sourse.Items[i - 1]);
this.lb_Sourse.Items.Remove(this.lb_Sourse.Items[i - 1]);
}
}
}
else
{
Msg.Text = "请选中人员!";
}
}
/// <summary>
/// 删除单个
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelOne_ServerClick(object sender, EventArgs e)
{
if (lb_NewName.SelectedIndex != -1)
{
string StrPersonFlow = "";
for (int i = this.lb_NewName.Items.Count; i > 0; i--)
{
if (this.lb_NewName.Items[i - 1].Selected)
{
this.lb_Sourse.Items.Add(this.lb_NewName.Items[i - 1]);
this.lb_NewName.Items.Remove(this.lb_NewName.Items[i - 1]);
StrPersonFlow = this.lb_NewName.Items[i - 1].Value + ",";
}
}
}
else
{
Msg.Text = "请选中人员!";
}
}
/// <summary>
/// 删除全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelAll_ServerClick(object sender, EventArgs e)
{
for (int i = 0; i < lb_NewName.Items.Count; i++)
{
lb_Sourse.Items.Add(new ListItem(lb_NewName.Items[i].Text, lb_NewName.Items[i].Value));
}
while (lb_NewName.Items.Count > 0)
{
lb_NewName.Items.Remove(lb_NewName.Items[0]);
}
}
/// 选中全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSelectAll_ServerClick(object sender, EventArgs e)
{
for (int i = 0; i < lb_Sourse.Items.Count; i++)
{
lb_NewName.Items.Add(new ListItem(lb_Sourse.Items[i].Text, lb_Sourse.Items[i].Value));
}
while(lb_Sourse.Items.Count > 0)
{
lb_Sourse.Items.Remove(lb_Sourse.Items[0]);
}
}
/// <summary>
/// 选中一个
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSelectOne_ServerClick(object sender, EventArgs e)
{
if (lb_Sourse.SelectedIndex != -1)
{
string StrPersonFlow = "";
for (int i = this.lb_Sourse.Items.Count; i > 0; i--)
{
if (this.lb_Sourse.Items[i - 1].Selected)
{
StrPersonFlow += this.lb_Sourse.Items[i - 1].Value + ",";
this.lb_NewName.Items.Add(this.lb_Sourse.Items[i - 1]);
this.lb_Sourse.Items.Remove(this.lb_Sourse.Items[i - 1]);
}
}
}
else
{
Msg.Text = "请选中人员!";
}
}
/// <summary>
/// 删除单个
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelOne_ServerClick(object sender, EventArgs e)
{
if (lb_NewName.SelectedIndex != -1)
{
string StrPersonFlow = "";
for (int i = this.lb_NewName.Items.Count; i > 0; i--)
{
if (this.lb_NewName.Items[i - 1].Selected)
{
this.lb_Sourse.Items.Add(this.lb_NewName.Items[i - 1]);
this.lb_NewName.Items.Remove(this.lb_NewName.Items[i - 1]);
StrPersonFlow = this.lb_NewName.Items[i - 1].Value + ",";
}
}
}
else
{
Msg.Text = "请选中人员!";
}
}
/// <summary>
/// 删除全部
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelAll_ServerClick(object sender, EventArgs e)
{
for (int i = 0; i < lb_NewName.Items.Count; i++)
{
lb_Sourse.Items.Add(new ListItem(lb_NewName.Items[i].Text, lb_NewName.Items[i].Value));
}
while (lb_NewName.Items.Count > 0)
{
lb_NewName.Items.Remove(lb_NewName.Items[0]);
}
}
二.第二种情况是用javascript实现:
function SelectOne()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var lst2=window.document.getElementById("<%=lb_NewName.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);
lst1.options[lstindex].parentNode.removeChild(lst1.options[lstindex]);
}
else
{
alert("所选条目已经存在");
return false;
}
}
function DelOne()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var lstindex=lst2.selectedIndex;
if(lstindex>=0)
{
// var v = lst2.options[lstindex].value+";";
var v = lst2.options[lstindex].value;
var t = lst2.options[lstindex].text;
lst1.options[lst1.options.length] = new Option(t,v,true,true);
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
}
}
function SelectAll()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var length = lst1.options.length;
for(var i=0;i<length;i++)
{
var v = lst1.options[i].value;
var t = lst1.options[i].text;
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
lst2.options[i] = new Option(t,v,true,true);
}
}
function DelAll()
{
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var length = lst2.options.length;
for(var i=length;i>0;i--)
{
lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
}
}
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var lst2=window.document.getElementById("<%=lb_NewName.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);
lst1.options[lstindex].parentNode.removeChild(lst1.options[lstindex]);
}
else
{
alert("所选条目已经存在");
return false;
}
}
function DelOne()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var lstindex=lst2.selectedIndex;
if(lstindex>=0)
{
// var v = lst2.options[lstindex].value+";";
var v = lst2.options[lstindex].value;
var t = lst2.options[lstindex].text;
lst1.options[lst1.options.length] = new Option(t,v,true,true);
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
}
}
function SelectAll()
{
var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
var length = lst1.options.length;
for(var i=0;i<length;i++)
{
var v = lst1.options[i].value;
var t = lst1.options[i].text;
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
lst2.options[i] = new Option(t,v,true,true);
}
}
function DelAll()
{
var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
var length = lst2.options.length;
for(var i=length;i>0;i--)
{
lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
}
}