This is a migrated thread and some comments may be shown as answers.

Programmatically create ItemTemplate using Reflection and POCO

1 Answer 25 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arni Gunnar Ragnarsson
Top achievements
Rank 2
Arni Gunnar Ragnarsson asked on 17 Oct 2009, 11:16 AM
Hi.

I have a large system we are developing constantly which displays around 100 lists throughout the system. We have set up a configuration system for the lists, and 1 user control to handle them all.

All our datasources are custom classes.

When I need to combine two different properties of the class into 1 column, things get a bit tricky.

Example of a custom class:
public class Master { 
    public int ID { getset; } 
    public string Name { getset; } 
    public Child FirstChild { getset; } 
 
public class Child { 
    public int ID { getset; } 
    public string Name { getset; } 

Using reflection I can access the values of the Master class easily. This is the databind event of the custom ItemTemplate class:
void control_DataBinding(object sender, EventArgs e) { 
    LiteralControl control = (LiteralControl)sender; 
    GridDataItem container = (GridDataItem)control.NamingContainer; 
    control.Text = dataItem.GetType().GetProperty("Name").GetValue(dataItem, null).ToString(); 




But when I need values from the Child class, things get tricky. I have tried all methods thinkable to me, and am stuck with this.

Can anyone show me how I can access the values from the Child class?

1 Answer, 1 is accepted

Sort by
0
BaiH
Top achievements
Rank 1
answered on 22 Oct 2009, 06:52 AM
Did you try using DataBinder.Eval? Here is an example:

var container = (GridDataItem) control.NamingContainer; 
var value = (string)DataBinder.Eval(container.DataItem, "FirstChild.Name"); 

--BH
Tags
Grid
Asked by
Arni Gunnar Ragnarsson
Top achievements
Rank 2
Answers by
BaiH
Top achievements
Rank 1
Share this question
or