C# 如何将下拉控件绑定到 ASP.NET 中的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15205380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind a drop-down control to a data source in ASP.NET
提问by nourah
I am new to C#.
我是 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.
我有一个创建 HR 系统的项目,我创建了一个页面来添加员工,但主管要求我创建一个下拉列表,在添加新员工时显示部门。
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 = "";
}
And this is a screen shot of my page: http://store2.up-00.com/Nov12/YXb11858.png
这是我的页面的屏幕截图: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();
}
}
采纳答案by MuhammadHani
As your code works for retrieving Employees
Info. from database, you will retrieve the Departments
info. from your Departments table.
由于您的代码适用于检索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();
}