using System.Data.SqlClient;
public partial class listbox实现添加删除转移及上下移动 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bandlist2();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text == string.Empty)
{
Response.Write("请输入内容!");
}
else
{
ListBox1.Items.Add(this.TextBox1.Text);
this.TextBox1.Text = string.Empty;
}
}
protected void btnUp_Click(object sender, EventArgs e)
{
if (this.ListBox1.SelectedIndex > 0)
{//获取当前选择信息
string name = this.ListBox1.SelectedItem.Text;
string id = this.ListBox1.SelectedValue;
int index = this.ListBox1.SelectedIndex;
//交换上一项与选择项的信息
ListBox1.SelectedItem.Text = ListBox1.Items[index - 1].Text;
ListBox1.SelectedItem .Value = ListBox1.Items[index - 1].Value;
ListBox1.Items[index - 1].Text = name;
ListBox1.Items[index - 1].Value = id;
//设定上一项为当前选择项
ListBox1.SelectedIndex--;
}
}
protected void btnMove_Click(object sender, EventArgs e)
{
if (this.ListBox1.SelectedValue == string.Empty)
{
Response.Write("<font color=red>请选择要移除的项</font>");
}
else
{ string aa=this .ListBox1 .SelectedValue ;
this.ListBox1.Items.Remove(aa);
this.ListBox2.Items.Add(aa);
}
}
protected void btnDown_Click(object sender, EventArgs e)
{
if (this.ListBox1.SelectedIndex >=0 && this .ListBox1 .SelectedIndex <this .ListBox1 .Items .Count -1)
{//获取当前选择信息
string name = this.ListBox1.SelectedItem.Text;
string id = this.ListBox1.SelectedValue;
//获取选择项的索引值
int index = this.ListBox1.SelectedIndex ;
//交换下一项与选择项的信息
ListBox1.SelectedItem.Text = ListBox1.Items[index + 1].Text;
ListBox1.SelectedItem.Value = ListBox1.Items[index + 1].Value;
ListBox1.Items[index + 1].Text = name;
ListBox1.Items[index + 1].Value = id;
//设定下一项为当前选择项
this.ListBox1.SelectedIndex++;
}
}
protected void btnDel_Click(object sender, EventArgs e)
{ //判断是否选择数据
if (ListBox1.SelectedIndex > -1)
{
for (int m = ListBox1.Items.Count - 1; m >= 0; m--)
{//判断获取的数据项是否被选择
if (this.ListBox1.Items[m].Selected == true)
{
ListBox1.Items.Remove(this.ListBox1.Items[m]);
}
}
}
else
{
Response.Write("请选择左边要删除的项");
}
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void bandlist2()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select shipname from orders",cn );
DataSet ds = new DataSet();
sda.Fill(ds, "orders");
ListBox2.DataSource = ds.Tables["orders"].DefaultView;
ListBox2.DataValueField = "shipname";
ListBox2.DataBind();
cn.Close();
}
}
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="listbox实现添加删除转移及上下移动.aspx.cs" Inherits="listbox实现添加删除转移及上下移动" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width ="50%" style ="border :solid 1px" >
<tr>
<td style ="width :10%; background-color :#00ee22;">
Listbox控件的应用
</td>
</tr>
<tr>
<td>
<table width ="100%" style ="border :solid 1px">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="添 加" onclick="btnAdd_Click" />
</td>
</tr>
<tr>
<td>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode ="Multiple" ></asp:ListBox>
</td>
<td>
<asp:Button ID="btnUp" runat="server" Text="上 移" onclick="btnUp_Click" /><br />
<asp:Button ID="btnMove" runat="server" Text="转移->" onclick="btnMove_Click" /><br />
<asp:Button ID="btnDown" runat="server" Text="下 移" onclick="btnDown_Click" /><br />
<asp:Button ID="btnDel" runat="server" Text="删 除" onclick="btnDel_Click" />
</td>
<td>
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True"
onselectedindexchanged="ListBox2_SelectedIndexChanged"></asp:ListBox>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>