全部,假设您有一个对象列表,其中类名为Person
。
public class Person
{
public string name{get;set;}
public string id{get;set;}
public int age{get;set;}
public Major major{get;set;}
}
public class Major{
public string MajorName{get;set;}
public string MajorId{get;set;}
}
假设你有一个ListBox
which id is personListBox
。并且它正在尝试与List
of绑定Person
。
List<Person> list= new List<Person>();
list.add(...);
...
list.add(...);
personListBox.DataSource=list;
personListBox.DataTextField = "name";
personListBox.DataValueField = "id";
personListBox.DataBind();
但我的问题是如何将所选项目转换为Person
?我想象的代码如下所示。
protected void personListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//Person item = (Person)lbBizCategory.SelectedItem;
//string majorName = item.major.MajorName;
}
不幸的是,它不起作用。有什么办法可以做到吗?谢谢。