-1

我正在使用 C# 在 asp.net 中创建一个应用程序,其中包含下拉列表。现在我不想编写相同的代码来从数据库中获取相同的数据。我正在尝试这段代码,但它不起作用

 protected void Page_Load(object sender, EventArgs e)
    {

        DataTable DT = sel_obj.select_Dept_Name();
        departmentDrop.DataSource = DT;
        departmentDrop.DataMember = "Department_Name";
        departmentDrop.DataBind();
    }
 public DataTable select_Dept_Name()
    {
        module c = new module();
        c.DB_Connection();

        if (c.con.State == ConnectionState.Open)
        {
            c.con.Close();
            c.con.Open();
        }

        DataSet DS = new DataSet();
        string QRY = "";
        QRY = "SELECT Department_Name FROM Department_Master";
        SqlDataAdapter DA = new SqlDataAdapter(QRY, c.con);
        DA.Fill(DS);
        DataTable DT = DS.Tables[0];
        return DT;
    }
4

1 回答 1

1

您需要调用“DataBind()”函数。您还需要确保您的表包含要与下拉列表绑定的数据。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        DataTable DT = sel_obj.select_Dept_Name();
        departmentDrop.DataSource = DT ;
        departmentDrop.DataTextField = "Department_Name";
        departmentDrop.DataValueField = "Department_Name";
        departmentDrop.DataBind();
        }
    }
于 2013-07-23T09:16:04.260 回答