下拉框数据绑定两种方式

来源:互联网 发布:北京云计算招聘岗位 编辑:程序博客网 时间:2024/05/20 23:59

1、利用cs包,DataAccess.cs

#regionstring m_str = @"select sname from service group by sname";DataTable m_dt = DataAccess.DBHelper.GetList(m_str);DropDownList2.DataValueField = "sname";DropDownList2.DataTextField = "sname";绑定数据源this.DropDownList2.DataSource = m_dt;DropDownList2.DataBind();DropDownList2.Items.Insert(0, new ListItem("请选择", ""));#endregion

而在web.config中

<appSettings>    <!--数据库连接字符串-->    <add key="DBConnString" value="Data Source=localhost;database=manager;uid=sa;pwd=sa;" />  </appSettings>

2、

SqlConnection conn = new SqlConnection("Data Source = localhost; database = manager; uid=sa; pwd=sa;");        conn.Open();        //任务类型绑定        #region        SqlDataAdapter sdt = new SqlDataAdapter("select sname from service group by sname", conn);        DataSet dt = new DataSet();        sdt.Fill(dt);        DropDownList2.DataValueField = "sname";        DropDownList2.DataTextField = "sname";        this.DropDownList2.DataSource = dt;        DropDownList2.DataBind();        DropDownList2.Items.Insert(0, new ListItem("请选择", ""));        #endregion        conn.Close();
0 0
原创粉丝点击