Programmatically create ItemTemplate using Reflection and POCO

2 posts, 0 answers
  1. Arni Gunnar Ragnarsson
    Arni Gunnar Ragnarsson avatar
    13 posts
    Member since:
    Jul 2007

    Posted 17 Oct 2009 Link to this post


    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?
  2. BaiH
    BaiH avatar
    83 posts
    Member since:
    Aug 2008

    Posted 22 Oct 2009 Link to this post

    Did you try using DataBinder.Eval? Here is an example:

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

Back to Top