乏mily

导航

<select>标签使用方法

前台页面:

    <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;
        }

 

posted on 2013-10-31 14:41  乏mily  阅读(449)  评论(0编辑  收藏  举报