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();
}
}
謝謝你soooo多我不能告訴你當我看到你的回答時我有多開心,但是當我添加像這樣的下拉列表時,有事情http://store2.up-00.com/Nov12/JrJ12794 .png我選擇部門表,但價值和名稱會是什麼? – nourah 2013-03-04 16:05:19
@nourah - Value屬性是將在下拉列表中顯示的文本。 Name屬性表示您可以用來將其插入到數據庫中的值。最常見的方法是顯示DepartmentName,將DepartmentId作爲值,假設您的表包含兩個字段。 – MuhammadHani 2013-03-04 16:08:49
員工表中的部門ID,我如何使用它? :( – nourah 2013-03-04 16:09:16