其时你上面所写的代码就已经是绑定了,只不过不是很完全。当然,绑定也可以完全从后台来写,eg:
sda.Fill(dataset1);//相应从数据库中读并填充到dataset中你已经会了,这里略过
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextFild="你的字段名1";
this.DropDownList1.DataValueFile="你的字段名2";
this.DropDownList1.DataBind();另一种方法就是获得sqldatareader对象,然后:
while(sdr.Read())
{
this.DropDownList1.Items.Add(new ListItem(sdr.GetValue(0).ToString(),Sdr.GetValue(1).ToString()));
}
以上两种方法都是可以用来绑定dropdownlist的

解决方案 »

  1.   

    首先在web.config配置连接数据库:
     
    <appSettings>
    <add key="ds" value="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Hospital;connect timeout=120" />
      </appSettings>
    在.cs文件中来绑定DropDownListusing System.Data.SqlClient;
    using System.Configuration;
    protected System.Data.SqlClient.SqlConnection cn;
    protected System.Data.SqlClient.SqlCommand cm;
    protected System.Data.SqlClient.SqlDataReader dr;
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    bindlistdepart();
    }
    }
    private void  bindlistdepart()
    {
    cn= new SqlConnection(ConfigurationSettings.AppSettings["ds"]);
    string strSQL="select * from department order by depart_id";
    cm = new SqlCommand(strSQL,cn);
    cn.Open();
    dr = cm.ExecuteReader();
    DropDownList1.DataSource=dr;
    DropDownList1.DataTextField="depart_name";
    DropDownList1.DataValueField="depart_id";
    DropDownList1.DataBind();
    cn.Close();
    }