功能:实现选中一个“LIstbox中的内容”,点击“Button”后把内容添加到另外的“Listbox”中
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button"
οnclick="Button1_Click1" />
</div>
</form>
</body>
</html>
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
ArrayList al = new ArrayList();
al.Add("星期一");
al.Add("星期二");
al.Add("星期三");
this.ListBox1.DataSource = al;//指定数据源
this.ListBox1.DataBind();//执行绑定
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
this.ListBox2.Items.Clear();
for (int i = 0; i < this.ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
this.ListBox2.Items.Add(ListBox1.Items[i].Value);
}
}
for (int i = 0; i < this.ListBox1.Items.Count; i++)
{
if (this.ListBox1.Items[i].Selected)
{
this.ListBox1.Items.RemoveAt(i);
}
}
}
}