Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
学生列表:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Width="146px">
</asp:DropDownList>
</form>
</body>
</html>
绑定数据到DropDownList控件
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 });
//绑定数据到DropDownList控件
this.DropDownList1.DataSource = ds.Tables["stu"];
this.DropDownList1.DataValueField = "stuNo";
this.DropDownList1.DataTextField = "stuName";
this.DropDownList1.DataBind();
}
}
运行结果: