2013-03-04 12 views
5

Jestem nowy w języku C#.Jak powiązać rozwijaną kontrolę ze źródłem danych w ASP.NET

Mam projekt stworzenia systemu HR i stworzyłem stronę do dodania pracowników, ale przełożony poprosił mnie o utworzenie rozwijanej listy, która wyświetla dział dodając nowego pracownika.

Nie wiem, jak zacząć i co powinienem zrobić jako pierwszy. Dodałem już listę rozwijaną z narzędzi, ale nie wiem, jak wybrać źródło danych oraz jego nazwę i wartość. Czy powinienem wybrać stół działu lub tabelę pracowników?

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

I to jest zrzut ekranu z mojej strony: http://store2.up-00.com/Nov12/YXb11858.png

to ostateczny kod nie ma błędu, ale lista rozwijana nie mają pozycję :(

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

    } 

} 

Odpowiedz

17

jak twoje Kod działa w celu pobrania Employees Info. z bazy danych, pobierzesz informacje Departments z tabeli w departamentach:

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

dziękuję bardzo, nie mogę powiedzieć, ile jestem szczęśliwy, kiedy widzę odpowiedź, ale jest coś, kiedy dodaję listę rozwijaną, taką jak ta http://store2.up-00.com/Nov12/JrJ12794 .png wybieram tabelę działu, ale wartość i nazwę, która będzie? – nourah

+0

@nourah - właściwość Value jest tekstem, który pojawi się na liście rozwijanej. Właściwość Name wskazuje wartość, której można użyć do wstawienia jej do bazy danych. Najczęstszym podejściem jest pokazanie nazwy departamentu, ustawienie wartości DepartmentId jako wartości, Zakładając, że twoja tabela zawiera dwa pola. – MuhammadHani

+0

Identyfikator dpartment w tabeli pracowników, więc jak mogę go użyć? :( – nourah

Powiązane problemy