【问题标题】:How to bind a drop-down control to a data source in ASP.NET如何将下拉控件绑定到 ASP.NET 中的数据源
【发布时间】:2013-02-18 18:24:06
【问题描述】:

我是 C# 新手。

我有一个创建 HR 系统的项目,我创建了一个页面来添加员工,但主管要求我创建一个下拉列表,在添加新员工时显示部门。

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

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 = "";
    }

这是我页面的屏幕截图: http://store2.up-00.com/Nov12/YXb11858.png

这是最后的代码,没有错误,但下拉列表没有项目:(

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 = "";
        TextBox3.Text = "";
    }
    protected void bindDepartments()
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT ID,department_name FROM Department ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();


        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

        adapter.Fill(table);

        DropDownList1.DataSource = table;
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataTextField = "department_name";
        DropDownList1.DataBind();

    }

}

【问题讨论】:

    标签: c# asp.net data-binding


    【解决方案1】:

    由于您的代码可用于检索 Employees 信息。从数据库中,您将检索到 Departments 信息。从您的部门表中。

    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();
    
    }
    

    【讨论】:

    • 非常感谢你,当我看到你的答案时,我无法告诉你我有多高兴,但是当我添加像这张图片 store2.up-00.com/Nov12/JrJ12794.png 这样的下拉列表时,我选择了部门表,但是值和名称是什么?
    • @nourah - Value 属性是将显示在下拉列表中的文本。 Name 属性指示可用于将其插入数据库的值。最常见的做法是显示 DepartmentName,将 DepartmentId 作为值,假设您的表包含这两个字段。
    • 员工表中的 dpartment id 那么我该如何使用它呢? :(
    • @nourah - 这将带我们进入一个基本的数据库概念,Employess 表中的 DepartmentID 指的是 Departments 表,称为数据库关系。要插入新员工,您必须选择一个部门来获取它的 ID,以便在 Employee 表的 DepartmentId 字段中插入。 - msdn.microsoft.com/en-us/library/ms175464(v=sql.105).aspx
    • 我已经建立了员工表和部门表之间的关系,但是在这里我应该将下拉列表的值设置为部门_ID,它在员工表中作为外键,所以我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多