//绑定ListBox1
private void ListBox1Bind()
{
.....
DataSet ds = new DataSet();
ListBox1.DataSource = ds.Tables[0].DefaultView; //ds 为 DataSet 对象
ListBox1.DataTextField = "textFld"; //textFld 为字段名称
ListBox1.DataValueField = "valueFld"; //valueFld 为字段名称
ListBox1.DataBind();
}
//添加
protected void Addbtn_Click(object sender, EventArgs e)
{
int i = 0;
while (i < ListBox1.Items.Count)
{
if (ListBox1.Items[i].Selected == true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
else
i += 1;
}
}
//删除
protected void Deletebtn_Click(object sender, EventArgs e)
{
int i = 0;
while (i < ListBox2.Items.Count)
{
if (ListBox2.Items[i].Selected == true)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.Remove(ListBox2.Items[i]);
}
else
i += 1;
}
}
//上移
protected void Upbtn_Click(object sender, EventArgs e)
{
//若不是第一行则上移
if (ListBox2.SelectedIndex > 0)
{
string name = ListBox2.SelectedItem.Text;
string ID = ListBox2.SelectedItem.Value;
int index = ListBox2.SelectedIndex;
ListBox2.SelectedItem.Text = ListBox2.Items[index - 1].Text;
ListBox2.SelectedItem.Value = ListBox2.Items[index - 1].Value;
ListBox2.Items[index - 1].Text = name;
ListBox2.Items[index - 1].Value = ID;
ListBox2.SelectedIndex--;
}
}
//下移
protected void Downbtn_Click(object sender, EventArgs e)
{
//若不是最后一行则下移
if (ListBox2.SelectedIndex >= 0 && ListBox2.SelectedIndex < ListBox2.Items.Count - 1)
{
string name = ListBox2.SelectedItem.Text;
string ID = ListBox2.SelectedItem.Value;
int index = ListBox2.SelectedIndex;
ListBox2.SelectedItem.Text = ListBox2.Items[index + 1].Text;
ListBox2.SelectedItem.Value = ListBox2.Items[index + 1].Value;
ListBox2.Items[index + 1].Text = name;
ListBox2.Items[index + 1].Value = ID;
ListBox2.SelectedIndex++;
}
}
//添加全部、删除清空
protected void Button5_Click(object sender, EventArgs e)
{
int i = 0;
while (i < ListBox2.Items.Count)
{
ListBox1.Items.Add(ListBox2.Items[i]);
i++;
}
ListBox2.Items.Clear();
}
转地址:http://hi.baidu.com/lzg1018/blog/item/6c0d4acd335d3f560eb345b7.html