从源基类下拉列表绑定错误

时间:2013-02-20 08:11:59

标签: c# oop drop-down-menu bind

我有一个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的属性,但它们是m​​yobject基类的公共属性,我必须使用myobject

我收到错误说X和Y不是列表的属性。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您应该将XY定义为读写属性。以下是工作代码示例。

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; }
}