2013-03-04 90 views
5

我是C#的新手。如何將下拉控件綁定到ASP.NET中的數據源

我有一個創建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(); 

    } 

} 

回答

17

隨着你代碼適用於從數據庫中檢索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(); 

} 
+0

謝謝你soooo多我不能告訴你當我看到你的回答時我有多開心,但是當我添加像這樣的下拉列表時,有事情http://store2.up-00.com/Nov12/JrJ12794 .png我選擇部門表,但價值和名稱會是什麼? – nourah 2013-03-04 16:05:19

+0

@nourah - Value屬性是將在下拉列表中顯示的文本。 Name屬性表示您可以用來將其插入到數據庫中的值。最常見的方法是顯示DepartmentName,將DepartmentId作爲值,假設您的表包含兩個字段。 – MuhammadHani 2013-03-04 16:08:49

+0

員工表中的部門ID,我如何使用它? :( – nourah 2013-03-04 16:09:16

相關問題