Default.aspx 源 码如下:
<%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
欢迎光临,请您点菜!</p>
<p>
<asp:ListBox ID="lbxSource" runat="server" Height="118px"
SelectionMode="Multiple" style="margin-right: 105px" Width="135px">
<asp:ListItem Value="0">鸡蛋汤</asp:ListItem>
<asp:ListItem Value="1">海带汤</asp:ListItem>
<asp:ListItem Value="2">万峦猪脚</asp:ListItem>
<asp:ListItem Value="3">炸猪排</asp:ListItem>
<asp:ListItem Value="4">上海醉蟹</asp:ListItem>
<asp:ListItem Value="5">红烧狮子头</asp:ListItem>
<asp:ListItem Value="6">排骨炖白菜</asp:ListItem>
</asp:ListBox>
<asp:Button ID="btnSelect" runat="server" οnclick="btnSelect_Click" Text="点菜" />
<asp:Button ID="btnDelete" runat="server" οnclick="btnDelete_Click"
style="height: 21px" Text="取消" />
<asp:ListBox ID="lbxDest" runat="server" Height="118px"
SelectionMode="Multiple" style="margin-right: 105px" Width="135px">
</asp:ListBox>
</p>
</asp:Content>
设计如图所示:
注意设置listbox属性里的SelectionMode为Multiple(多选)。
Default.aspx.cs 源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
int count = lbxSource.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem Item=lbxSource.Items[index];
if (lbxSource.Items[index].Selected == true)
{
lbxSource.Items.Remove(Item);
lbxDest.Items.Add(Item);
index--;
}
index++;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
int count = lbxDest.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem Item = lbxDest.Items[index];
if (lbxDest.Items[index].Selected == true)
{
lbxDest.Items.Remove(Item);
lbxSource.Items.Add(Item);
index--;
}
index++;
}
}
}
}