我有一个ddl和我这样的绑定值;
List<ClassB> myobject =new List<ClassB>();
myddl.DataSource = myobject;
myddl.DataTextField = "X";
myddl.DataValueField = "Y";
myddl.DataBind();
public class ClassB: ClassA
{
}
public class ClassA
{
public string X;
public string Y;
}
X和Y不是myobject的属性,但它们是myobject基类的公共属性,我必须使用myobject
我收到错误说X和Y不是列表的属性。
有人有什么想法吗?
答案 0 :(得分:0)
您应该将X
和Y
定义为读写属性。以下是工作代码示例。
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<ClassB> myobject = new List<ClassB>();
myobject.Add(new ClassB() { X = "1", Y = "2" });
myobject.Add(new ClassB() { X = "3", Y = "4" });
myddl.DataSource = myobject;
myddl.DataTextField = "X";
myddl.DataValueField = "Y";
myddl.DataBind();
}
}
public class ClassB : ClassA
{
}
public class ClassA
{
public string X { get; set; }
public string Y { get; set; }
}