C# 如何从对象列表中获取字符串值并将其添加到下拉列表中?
我想要一份包含三个部分的员工列表,员工id、姓氏和名字,并将他们添加到显示姓氏和名字的下拉列表中 到目前为止,我已经为员工创建了一个类:C# 如何从对象列表中获取字符串值并将其添加到下拉列表中?,c#,asp.net,C#,Asp.net,我想要一份包含三个部分的员工列表,员工id、姓氏和名字,并将他们添加到显示姓氏和名字的下拉列表中 到目前为止,我已经为员工创建了一个类: public class Employee { public int emp_Id; public string lastName; public string firstName; public Employee(int id, string last, string first) { this
public class Employee
{
public int emp_Id;
public string lastName;
public string firstName;
public Employee(int id, string last, string first)
{
this.emp_Id = id;
this.lastName = last;
this.firstName = first;
}
}
并创建了一个列表来填充:
private List<Employee> employeeList = new List<Employee>();
到那时为止,一切都完全按照我所希望的那样工作,但我不知道如何用列表中包含的姓氏和名字值填充dropdownlist
代码已编辑为可读性为什么不创建一个名为FullName的属性来获取“FirstName+“”+LastName”?这将为您提供一个字段来处理,而不是两个。现有属性的示例:
<asp:DropDownList id="bla" runat="server" />
bla.DataSource = employeeList;
bla.DataTextField = "firstName";
bla.DataValueField = "emp_Id"
bla.DataBind();
bla.DataSource=employeeList;
bla.DataTextField=“firstName”;
bla.DataValueField=“emp_Id”
bla.DataBind();
我推荐这个:
<asp:DropDownList id="bla" runat="server" />
bla.DataSource = employeeList;
bla.DataTextField = "fullName";
bla.DataValueField = "emp_Id"
bla.DataBind();
public class Employee
{
public int emp_Id;
public string lastName;
public string firstName;
public string fullName get{ return firstName + " " + lastName;}
public Employee(int id, string last, string first)
{
this.emp_Id = id;
this.lastName = last;
this.firstName = first;
}
}
bla.DataSource=employeeList;
bla.DataTextField=“fullName”;
bla.DataValueField=“emp_Id”
bla.DataBind();
公营雇员
{
公共int emp_Id;
公共字符串lastName;
公共字符串名;
公共字符串fullName get{return firstName+“”+lastName;}
公共雇员(int-id,最后一个字符串,第一个字符串)
{
this.emp_Id=Id;
this.lastName=last;
this.firstName=first;
}
}
参见下面的代码:
DropDownList ddl = new DropDownList();
ddl.DataSource = employeeList;
ddl.DataTextField = "fullName";
ddl.DataValueField = "emp_Id";
我还将修改您的类以包含全名字段:
public class Employee
{
public int emp_Id { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string fullName
{
get
{
return String.Format("{0} {1}", this.firstName, this.LastName);
}
}
public Employee(int id, string last, string first)
{
this.emp_Id = id;
this.lastName = last;
this.firstName = first;
}
}
您可以向类中添加一个额外的属性来保存这3个值,并在绑定DropDownList时将其用作DataTextField: 类别代码
public class Employee
{
public int emp_Id;
public string lastName;
public string firstName;
public string Text
{
get { return this.ToString(); }
}
public Employee(int id, string last, string first)
{
this.emp_Id = id;
this.lastName = last;
this.firstName = first;
}
public override string ToString()
{
return lastName + " " + firstName + " " + emp_Id;
}
}
HTML:
List employees=newlist();
ddl.DataSource=员工;
ddl.DataValueField=“emp_Id”;
ddl.DataTextField=“Text”;
ddl.DataBind();
祝你好运 如果您不想或无法修改员工,您也可以尝试以下方法:
var data = employee.Select (x =>
new KeyValuePair<int, string>(
x.emp_Id,
string.Format("{0}, {1}", x.lastName, x.firstName)
));
ddl.DataSource = data.ToList();
ddl.DataValueField = "Key";
ddl.DataTextField = "Value";
ddl.DataBind();
var data=employee.Select(x=>
新的KeyValuePair(
x、 emp_Id,
格式(“{0},{1}”,x.lastName,x.firstName)
));
ddl.DataSource=data.ToList();
ddl.DataValueField=“Key”;
ddl.DataTextField=“值”;
ddl.DataBind();
如果您有不同的页面,其中包含不同的员工下拉列表,有时是Lastname优先,有时是Firstname优先,可能中间有冒号,也可能没有冒号,这也可能很有用…我建议在sql查询中对员工进行排序,除非有特殊原因不这样做。感谢您的澄清编辑。这正是我要找的。完美的
List<Employee> employees = new List<Employee>();
ddl.DataSource = employees;
ddl.DataValueField = "emp_Id";
ddl.DataTextField = "Text";
ddl.DataBind();
var data = employee.Select (x =>
new KeyValuePair<int, string>(
x.emp_Id,
string.Format("{0}, {1}", x.lastName, x.firstName)
));
ddl.DataSource = data.ToList();
ddl.DataValueField = "Key";
ddl.DataTextField = "Value";
ddl.DataBind();