C# 如何在ASP.NET中将下拉控件绑定到数据源

C# 如何在ASP.NET中将下拉控件绑定到数据源,c#,asp.net,data-binding,C#,Asp.net,Data Binding,我是C#的新手 我有一个创建人力资源系统的项目,我创建了一个页面来添加员工,但主管让我创建一个下拉列表,在添加新员工时显示部门 我不知道如何开始,我应该先做什么。我已经从工具中添加了一个下拉列表,但我不知道如何选择数据源及其名称和值。我应该选择部门表还是员工表 public partial class _Default : Page { private String strcon = ConfigurationManager.ConnectionStrings["hr"].Connec

我是C#的新手

我有一个创建人力资源系统的项目,我创建了一个页面来添加员工,但主管让我创建一个下拉列表,在添加新员工时显示部门

我不知道如何开始,我应该先做什么。我已经从工具中添加了一个下拉列表,但我不知道如何选择数据源及其名称和值。我应该选择部门表还是员工表

public partial class _Default : Page
{

    private String strcon = ConfigurationManager.ConnectionStrings["hr"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)

            bindgrideview();
    }
    protected void bindgrideview()
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT F_name , L_name , salary FROM Employee ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();


        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

        adapter.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string F_name = TextBox1.Text;
        string L_name = TextBox2.Text;
        int status = 1;
        string salarystr = TextBox3.Text.ToString();
        int salary = Int32.Parse(salarystr);
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "ADDEMP";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        ADDCmd.CommandType = CommandType.StoredProcedure;
        ADDCmd.Parameters.AddWithValue("@F_name", F_name);
        ADDCmd.Parameters.AddWithValue("@L_name", L_name);
        ADDCmd.Parameters.AddWithValue("@status", status);
        ADDCmd.Parameters.AddWithValue("@salary", salary);

        ADDCmd.ExecuteNonQuery();
        bindgrideview();
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
这是我页面的屏幕截图:

这是最终代码。没有错误,但下拉列表中没有项:(


由于您的代码用于从数据库中检索
员工
信息,因此您将从部门表中检索
部门
信息

protected void bindDepartments()
{
    SqlConnection strcon1 = new SqlConnection(strcon);
    strcon1.Open();
    string ADDStr = "SELECT DepartmentId,DepartmentName FROM Departments ";
    SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
    DataTable table = new DataTable();


    SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

    adapter.Fill(table);

    ddlDepartments.DataSource = table;
    ddlDepartments.DataValueField = "DepartmentId"; //The Value of the DropDownList, to get it you should call ddlDepartments.SelectedValue;
    ddlDepartments.DataTextField = "DepartmentName"; //The Name shown of the DropDownList.
    ddlDepartments.DataBind();

}

非常感谢你,当我看到你的答案时,我无法告诉你我有多高兴,但当我添加如图所示的下拉列表时,我选择了部门表,但值和名称将是什么?@nourah-值属性是将显示在下拉列表中的文本。名称属性指示了可用于输入的值将其插入数据库。最常见的方法是显示部门名称,将部门id作为值,假设您的表包含两个字段。员工表中的D部门id如何使用(@nourah-这将引导我们了解一个基本的数据库概念,Employees表中的DepartmentID指的是Departments表,称为数据库关系。要插入新员工,您必须选择一个部门来获取其Id,以便插入Employee表中的DepartmentID字段。-我已经建立了员工之间的关系表和department表,但在这里,我应该将下拉列表=department\u ID的值作为外键保存在employee表中,那么我该如何做呢?
protected void bindDepartments()
{
    SqlConnection strcon1 = new SqlConnection(strcon);
    strcon1.Open();
    string ADDStr = "SELECT DepartmentId,DepartmentName FROM Departments ";
    SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
    DataTable table = new DataTable();


    SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

    adapter.Fill(table);

    ddlDepartments.DataSource = table;
    ddlDepartments.DataValueField = "DepartmentId"; //The Value of the DropDownList, to get it you should call ddlDepartments.SelectedValue;
    ddlDepartments.DataTextField = "DepartmentName"; //The Name shown of the DropDownList.
    ddlDepartments.DataBind();

}