下拉框数据绑定两种方式
1、利用cs包,DataAccess.cs
#region
string 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();
版权声明:本文为博主原创文章,未经博主允许不得转载。