webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test5_1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>候选国家: 你喜欢的国家:</div>
<asp:ListBox ID="lbxSource" runat="server" Width="94px" Height="150px" SelectionMode="Multiple">
<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:ListItem Value="7">英国</asp:ListItem>
</asp:ListBox>
<asp:Button ID="btn_select" runat="server" Text="加入" style="position:relative;top:-70px;left2px;" OnClick="btn_select_Click"/>
<asp:ListBox ID="lbxDestnition" runat="server" Height="150px" Width="90px" SelectionMode="Multiple"></asp:ListBox>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test5_1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_select_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)
{
lbxDestnition.Items.Add(item);
}
index++;
}
//这段代码有缺陷,框1选中的国家,加入框2时,没有做重复项判断,所以会重复添加内容。
}
}
}