Search code examples
asp.netdata-bindingradiobuttonlist

ASP:RadioButtonList Databind Custom Object


Hey guys, I'm having a bit of trouble with my ASP:RadioButtonList, searched Google and SO, no luck, can you help me out?

I'm having trouble databinding. I've got a custom class that looks like this:

public class myClass{
  public myInnerClass{
    public int myID;
    public String myTextField;
    /* other fields*/
  }
  public List<myInnerClass> myList;
}

And I'm trying to bind a Generic List of it's inner class to a radiolist:

protected void Page_Load(object sender, EventArgs e){
  myClass data = anotherClass.getData();
  uxRadioList1.DataSource = data.myList;
  uxRadioList1.DataTextField = "myTextField";
  uxRadioList1.DataValueField = "myID";
  uxRadioList1.DataBind();
}

But it just won't go. When I don't specify the DataTextField and DataValueField field it binds, but it displays 'myClass+myInnerClass' . How do I do this properly?


Solution

  • I think you can only bind to public properties, but not to fields. Try changing the fields of myInnerClass to properties:

    public class myClass{
      public myInnerClass{
        public int myID { get; set; }
        public String myTextField { get; set; }
        /* other fields*/
      }
      public List<myInnerClass> myList;
    }