如何在ASP.NET中将下拉式控件绑定到数据源 [英] How to bind a drop-down control to a data source in ASP.NET

查看:98
本文介绍了如何在ASP.NET中将下拉式控件绑定到数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手。

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

I have a project to create an HR system and I created a page to add employees but the supervisor asked me to create a drop down list that displays the department when adding a new employee.

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

I don't know how to start and what I should do first. I already added a drop down list from the tools but I don't know how to choose the data source and the name and value of it. Should I choose the department table or the employee table?

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

And this is a screen shot of my page: http://store2.up-00.com/Nov12/YXb11858.png

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

this is the final code there is no error but the dropdown list have no item :(

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

    }

}


推荐答案

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

As your code works for retrieving Employees Info. from database, you will retrieve the Departments info. from your Departments table.

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

}

这篇关于如何在ASP.NET中将下拉式控件绑定到数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆