【发布时间】:2017-01-14 23:00:21
【问题描述】:
我有一个已绑定项目的 ListBox,但是当我稍后尝试从 ListBox 中检索对象时,出现编译器错误...有人知道这是什么吗?
protected void Page_Load(object sender, EventArgs e)
{
List<Project> projects;
DeleteListBox.ItemType = "Project";
DeleteListBox.DataValueField = "projName";
using(DBMethods db = new DBMethods())
{
//Projects is not null during testing
projects = db.getProjects() as List<Project>;
DeleteListBox.DataSource = projects;
DeleteListBox.DataBind();
}
}
现在稍后我尝试从 ListBox 中检索对象,但我在整行代码下看到一条红色波浪线:
protected void PermDelete_Click(object sender, EventArgs e)
{
using(DBMethods db = new DBMethods())
{
//Compiler error here
var toDelete = DeleteListBox.SelectedItem as Project;
}
}
如何将我选择的列表项转换为 Project 对象而不会出现编译器错误?无法通过引用转换将类型“SystemW.Web.UI.WebControls.ListItem”转换为“Project”...
【问题讨论】: