![定义写在.aspx《---》绑定写在.cs](https://i-blog.csdnimg.cn/blog_migrate/b7714f5407b632e197b67e9de51a5ebc.png)
DropDownList控件的定义
学生列表:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Width="146px">
</asp:DropDownList>
向DropDownList控件绑定数据
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_2
{
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();
}
}
}
}