后台代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Sample5_1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet ds = new DataSet();//
ds.Tables.Add("stu");
ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
ds.Tables["stu"].Columns.Add("stuName", typeof(string));
ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
ds.Tables["stu"].Rows.Add(new object[] { 1, "张一", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 3, "李三", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
ds.Tables["stu"].Rows.Add(new object[] { 5, "周五", 100 });
this.ListBox1.DataSource = ds.Tables["stu"];
this.ListBox1.DataValueField = "stuNo";
this.ListBox1.DataTextField = "stuName";
this.ListBox1.DataBind();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Label1.Text = "你选择的学生是:学号 " + this.ListBox1.SelectedValue.ToString() + " 姓名 " + this.ListBox1.SelectedItem.Text.ToString();
}
}
}