webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test5_16.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>
请选择<asp:Label ID="Label1" runat="server" Text="你喜欢的电影"></asp:Label>:<br />
<asp:ListBox ID="ListBox1" runat="server" Height="199px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" SelectionMode="Multiple" AutoPostBack="True">
<asp:ListItem Value="大话西游"></asp:ListItem>
<asp:ListItem Value="终结者"></asp:ListItem>
<asp:ListItem Value="无间道"></asp:ListItem>
<asp:ListItem Value="明日边缘"></asp:ListItem>
<asp:ListItem Value="遗落战境"></asp:ListItem>
<asp:ListItem Value="夏洛特烦恼"></asp:ListItem>
<asp:ListItem Value="无人区"></asp:ListItem>
<asp:ListItem Value="疯狂的石头"></asp:ListItem>
</asp:ListBox><br />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</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_16
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int count = ListBox1.Items.Count;
string result = "";
for (int i = 0; i < count; i++)
{
if (ListBox1.Items[i].Selected == true)
{
result += ListBox1.Items[i].Text + " ";
}
}
Label2.Text = Label1.Text +" : " + result;
}
}
}