前台页面:
<form id="form1" runat="server"> <select runat="server" id="TestList"> </select> </form>
后台代码:
public readonly string conStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { DataTable dt = GetTable(); if (dt.Rows.Count>0) { TestList.DataSource = dt; TestList.DataTextField = "UserName"; TestList.DataValueField = "id"; TestList.DataBind(); TestList.Items.Insert(0, new ListItem("请选择设备...", "0")); } } public DataTable GetTable() { DataTable dt = new DataTable(); string sql = "select * from tb_user"; using (SqlConnection conn=new SqlConnection(conStr)) { SqlDataAdapter adapter = new SqlDataAdapter(sql,conn); adapter.Fill(dt); } return dt; }