webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work027.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>ListBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode ="Single" Width="300px" Rows="8"></asp:ListBox><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<hr />
<h4>多选时,请按住Ctrl键</h4>
<asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple" Width="300px" Rows="8"></asp:ListBox><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="显示选择的值" OnClick="Button1_Click" />
</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 work027
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindMethod1();
BindMethod2();
}
}
//从数据库中取数据,返回
private System.Data.DataTable GetBindData()
{
System.Data.SqlClient.SqlConnectionStringBuilder bu = new System.Data.SqlClient.SqlConnectionStringBuilder();
bu.DataSource = "(local)";
bu.InitialCatalog = "test";
bu.UserID = "sa";
bu.Password = "123456";
string conString = bu.ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString);
string sql = "select user_name,real_name from userinfo order by user_id asc";
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(sql, con);
System.Data.DataTable dt = new System.Data.DataTable();
adapter.Fill(dt);
return dt;
}
//绑定
private void BindMethod1()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt = GetBindData();
ListBox1.DataSource = dt;
ListBox1.DataTextField = "real_name";
ListBox1.DataValueField = "user_name";
ListBox1.DataBind();
}
//绑定
private void BindMethod2()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt = GetBindData();
ListBox2.DataSource = dt;
ListBox2.DataTextField = "real_name";
ListBox2.DataValueField = "user_name";
ListBox2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
//单选
Label1.Text = "";
Label1.Text = string.Format("Text={0},Value={1},Index={2}",ListBox1.SelectedItem.Text,ListBox1.SelectedItem.Value,ListBox1.SelectedIndex);
//多选
string selText = string.Empty;
foreach (ListItem item in ListBox2.Items)
{
if (item.Selected == true)
{
selText += string.Format("Text={0},Value={1} <br>",item.Text,item.Value);
}
}
if (!string.IsNullOrEmpty(selText))
{
Label2.Text = selText;
}
}
}
}
sql
create database test;
create table UserInfo(
user_id int identity(1,1) not null,
user_name varchar(20) not null unique,
real_name nvarchar(8) not null,
age tinyint not null,
sex bit not null,
mobile varchar(14),
phone varchar(14),
email varchar(50) not null,
primary key(user_id)
);
insert into UserInfo values('zhangfei','张飞',36,1,'13011110001','8845996','zf@qq.com');
insert into UserInfo values('guanyu','关羽',38,1,'13011110002','8845995','gy@qq.com');
insert into UserInfo values('liubei','刘备',42,1,'13011110003','8845994','lb@qq.com');
insert into UserInfo values('zhaoyun','赵云',32,1,'13011110004','8845993','zy@qq.com');
insert into UserInfo values('huangzhong','黄忠',50,1,'13011110005','8845992','hz@qq.com');
insert into UserInfo values('caocao','曹操',48,1,'13011110006','8845991','cc@qq.com');
insert into UserInfo values('sunquan','孙权',33,1,'13011110007','8845990','sq@qq.com');
insert into UserInfo values('diaochan','貂蝉',20,0,'13011110008','8845880','dc@qq.com');
insert into UserInfo values('daqiao','大乔',21,0,'13011110009','8845881','dq@qq.com');
insert into UserInfo values('xiaoqiao','小乔',20,0,'13011110010','8845882','xq@qq.com');