在HTML页面中加入一个函数:
<script language="javascript">
function change()
{
var addOption=document.createElement("option")
var index1
if(document.form1.ListBox1.length==0)return(false);
index1=document.form1.ListBox1.selectedIndex
if(index1<0)return(false);
addOption.text=document.form1.ListBox1.options(index1).text
addOption.value=document.form1.ListBox1.value
document.form1.ListBox2.add(addOption);
document.form1.ListBox1.remove (index1)
}
</script>在代码里面加入:
ListBox1.Attributes["onDblClick"] = "change()";
<script language="javascript">
function change()
{
var addOption=document.createElement("option")
var index1
if(document.form1.ListBox1.length==0)return(false);
index1=document.form1.ListBox1.selectedIndex
if(index1<0)return(false);
addOption.text=document.form1.ListBox1.options(index1).text
addOption.value=document.form1.ListBox1.value
document.form1.ListBox2.add(addOption);
document.form1.ListBox1.remove (index1)
}
</script>在代码里面加入:
ListBox1.Attributes["onDblClick"] = "change()";
{
int i;
ListItemCollection ListItems=new ListItemCollection();
foreach(ListItem li in ListBox1.Items)
{
if (li.Selected)
{
ListBox2.Items.Add(new ListItem(li.Text,li.Value));
ListItems.Add(li);
}
}
foreach(ListItem li1 in ListItems)
{
i=ListBox1.Items.IndexOf(li1);
ListBox1.Items.Remove(li1);
if (i >= ListBox1.Items.Count) i=ListBox1.Items.Count-1;
ListBox1.SelectedIndex=i;
}
Page.RegisterStartupScript("message","<script language=Javascript>document.all.ListBox1.options.multiple=null;</script>");
}
else
Page.RegisterStartupScript("message","<script language=Javascript>window.alert('请选择一个!')</script>");
if(ListBox1.Items.Count<=0)
{
btnAddOne.Enabled=false;
btnAddAll.Enabled=false;
btnDelOne.Enabled=true;
btnDelAll.Enabled=true;
}
if(ListBox2.Items.Count>0)
{
btnDelOne.Enabled=true;
btnDelAll.Enabled=true;
}
private void btnAddOne_Click(object sender, System.EventArgs e)
{
if(ListBox1.SelectedIndex>=0)
{
int i;
ListItemCollection ListItems=new ListItemCollection();
foreach(ListItem li in ListBox1.Items)
{
if (li.Selected)
{
ListBox2.Items.Add(new ListItem(li.Text,li.Value));
ListItems.Add(li);
}
}
foreach(ListItem li1 in ListItems)
{
i=ListBox1.Items.IndexOf(li1);
ListBox1.Items.Remove(li1);
if (i >= ListBox1.Items.Count) i=ListBox1.Items.Count-1;
ListBox1.SelectedIndex=i;
}
Page.RegisterStartupScript("message","<script language=Javascript>document.all.ListBox1.options.multiple=null;</script>");
}
else
Page.RegisterStartupScript("message","<script language=Javascript>window.alert('请选择一个!')</script>");
if(ListBox1.Items.Count<=0)
{
btnAddOne.Enabled=false;
btnAddAll.Enabled=false;
btnDelOne.Enabled=true;
btnDelAll.Enabled=true;
}
if(ListBox2.Items.Count>0)
{
btnDelOne.Enabled=true;
btnDelAll.Enabled=true;
} ShowData(); } private void btnAddAll_Click(object sender, System.EventArgs e)
{
if(ListBox1.Items.Count>0)
{
for(int i=0;i<ListBox1.Items.Count;i++)
{
string str1=ListBox1.Items[i].Text;
string str2=ListBox1.Items[i].Value;
ListBox2.Items.Add(new ListItem(str1,str2));
}
ListBox1.Items.Clear();
}
btnAddOne.Enabled=false;
btnAddAll.Enabled=false;
btnDelOne.Enabled=true;
btnDelAll.Enabled=true;
ShowData();
} private void btnDelOne_Click(object sender, System.EventArgs e)
{
if(ListBox2.SelectedIndex>=0)
{
int i;
ListItemCollection ListItems=new ListItemCollection();
foreach(ListItem li in ListBox2.Items)
{
if (li.Selected)
{
ListBox1.Items.Add(new ListItem(li.Text,li.Value));
ListItems.Add(li);
}
}
foreach(ListItem li1 in ListItems)
{
i=ListBox2.Items.IndexOf(li1);
ListBox2.Items.Remove(li1);
if (i >= ListBox2.Items.Count) i=ListBox2.Items.Count-1;
ListBox2.SelectedIndex=i;
}
}
else
Page.RegisterStartupScript("message","<script language=Javascript>window.alert('请选择一个!')</script>");
if(ListBox2.Items.Count<=0)
{
btnAddOne.Enabled=true;
btnAddAll.Enabled=true;
btnDelOne.Enabled=false;
btnDelAll.Enabled=false;
}
if(ListBox1.Items.Count>0)
{
btnAddOne.Enabled=true;
btnAddAll.Enabled=true;
}
ShowData();
} private void btnDelAll_Click(object sender, System.EventArgs e)
{
if(ListBox2.Items.Count>0)
{
for(int i=0;i<ListBox2.Items.Count;i++)
{
string str1=ListBox2.Items[i].Text;
string str2=ListBox2.Items[i].Value;
ListBox1.Items.Add(new ListItem(str1,str2));
}
ListBox2.Items.Clear();
}
btnDelOne.Enabled=false;
btnDelAll.Enabled=false;
btnAddOne.Enabled=true;
btnAddAll.Enabled=true;
ShowData();
}