0

i have the following two objects

public partial class ProgramObj
{
  public int id;
  public PersonObj myPerson;
}

public class PersonObj
{
   public int id;
   public string full_name;
}

I am assigning a list of ProgramObj's to a repeater from a SqlDataReader

program_list.DataSource = reader;
program_list.DataBind();

What I want to do, is access the full_name property of the PersonObj in each ProgramObj I've tried multiple things, the only thing that gets me an output value is

<%# DataBinder.Eval(Container.DataItem, "id") %>

which gets me the id of the ProgramObj, but I would like to get the name of the PersonObj, I thought

<%# DataBinder.Eval(Container.DataItem, "myPerson.full_name") %>

would work, but it doesn't appear to get me anywhere.

I also tried an ItemDataBound with

PersonObj myPerson = (PersonObj)e.Item.DataItem;
lblUserName.Text = myPerson.Full_Name_RFL;

and

<%# DataBinder.Eval(Container.DataItem, "myPerson") %>

but i get an error that it cannot cast an object of type DataRecordInternal to PersonObj. thoughts?

Josh
  • 831
  • 1
  • 15
  • 31

2 Answers2

1

Your ItemDataBound should work if you do this:

PersonObj myPerson = ((ProgramObj)e.Item.DataItem).myPerson;
lblUserName.Text = myPerson.full_name;

Becausse the repeater is bound to a list of ProgramObj object the DataItem will be the ProgramObj. so you then need to get the myPerson property if you want the PersonObj object.

Sam Greenhalgh
  • 5,952
  • 21
  • 37
1

See the What's the deal with Databinder.Eval and Container.DataItem? article on asp.net. Also, see this GridView bound with with Properties of nested class question on here.

Community
  • 1
  • 1
JamieSee
  • 12,696
  • 2
  • 31
  • 47