locked
Custom DataBinding RRS feed

  • Question

  • User851098746 posted

    Hi All,

    If any one tries to google "custom databinding", he/she will get several results on how to implement your custom data binding using attributes.

    I have no problems with attributes, but I thought that this task should be simpler, and this is why I posting this question here.

    I basically want to implement my own custom Drop Down List. I want this DDL to have a DataSource property and a DataBind() method.

    The dataSource should accept any kind of collections, list, ... etc, just like the normal DataSource in .NET built-in server controls.

    Inside the DataBind() method I want to loop through all items in the DataSource. For every item I will use the DataBinder class to retrieve the properties that I want to add to my custom DDL. I will specifically use DataBinder.Eval(object, string) method (Please tell me if there's a better way).

    My primary problem is: I don't know how to convert the DataSource value into something that I can iterate over. The DataSource data type is (object). Please, is there a direct way to get the items of this DataSource?


    Thanks in advance

    Friday, August 14, 2009 6:33 PM

Answers

  • User-2106054853 posted

     

    Thanks Allen,

    The datasource that I assign in the client code implements the generic type: List<T>

    Yet, my intention is to make it possible to assign any kind of IEnumerable datasource, but at the DataBind() method, I cannot know what is the data type of the data source that was assigned in the client code. So I don't know what data type shoudl I convert the datasource to so I can iterate over it.


    You don't have to know the type. You can use reflection:

      public class MyData
        {
            public string StrProperty { get; set; }
            public bool BoolProperty { get; set; }
            public int IntProperty { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                object datasource = new List<object>() {"hello world", new MyData()
                { BoolProperty=false, StrProperty="test", IntProperty=11 }};
                IEnumerable array = datasource as IEnumerable;
                foreach (var item in array)
                {

                    Console.WriteLine(string.Format("========{0}========", item.ToString()));
                    //use reflection to get all properties of item.
                    //To test only extract strimg, bool, int properties.
                    foreach (PropertyInfo pi in item.GetType().GetProperties())
                    {
                        if (pi.PropertyType == typeof(string) ||
                            pi.PropertyType == typeof(int) ||
                            pi.PropertyType == typeof(bool))
                        {
                            Console.WriteLine(string.Format("{0}: {1}"
                                , pi.Name, pi.GetValue(item, null).ToString()));

                        }
                    }

                }
                Console.ReadLine();
            }

        }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2009 2:33 AM

All replies

  • User-2106054853 posted

    My primary problem is: I don't know how to convert the DataSource value into something that I can iterate over. The DataSource data type is (object). Please, is there a direct way to get the items of this DataSource?


     

    If the DataSource is IEnumerable you can use "foreach" keyword to loop through it. If not, you probably need to use reflection to get all its properties value, use them as the datasource. You can also define your own interface. If the DataSource implements this interface, do specific stuffs. Of course, all depend on your requirement.


     

    Monday, August 17, 2009 12:07 AM
  • User851098746 posted

    Thanks Allen,

    The datasource that I assign in the client code implements the generic type: List<T>

    Yet, my intention is to make it possible to assign any kind of IEnumerable datasource, but at the DataBind() method, I cannot know what is the data type of the data source that was assigned in the client code. So I don't know what data type shoudl I convert the datasource to so I can iterate over it.


    Monday, August 17, 2009 12:15 PM
  • User-2106054853 posted

     

    Thanks Allen,

    The datasource that I assign in the client code implements the generic type: List<T>

    Yet, my intention is to make it possible to assign any kind of IEnumerable datasource, but at the DataBind() method, I cannot know what is the data type of the data source that was assigned in the client code. So I don't know what data type shoudl I convert the datasource to so I can iterate over it.


    You don't have to know the type. You can use reflection:

      public class MyData
        {
            public string StrProperty { get; set; }
            public bool BoolProperty { get; set; }
            public int IntProperty { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                object datasource = new List<object>() {"hello world", new MyData()
                { BoolProperty=false, StrProperty="test", IntProperty=11 }};
                IEnumerable array = datasource as IEnumerable;
                foreach (var item in array)
                {

                    Console.WriteLine(string.Format("========{0}========", item.ToString()));
                    //use reflection to get all properties of item.
                    //To test only extract strimg, bool, int properties.
                    foreach (PropertyInfo pi in item.GetType().GetProperties())
                    {
                        if (pi.PropertyType == typeof(string) ||
                            pi.PropertyType == typeof(int) ||
                            pi.PropertyType == typeof(bool))
                        {
                            Console.WriteLine(string.Format("{0}: {1}"
                                , pi.Name, pi.GetValue(item, null).ToString()));

                        }
                    }

                }
                Console.ReadLine();
            }

        }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2009 2:33 AM
  • User851098746 posted

    Thank you very much Allen. It's worked fine.

    I've made some small modifications on your code to match what I needed at the beginning and I'm listing it belwo for the benefit of other users.

    Thanks again.


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;

    namespace ConsoleApplication1
    {

    public class MyData
    {
    public string StrProperty { get; set; }
    public bool BoolProperty { get; set; }
    public int IntProperty { get; set; }
    }

    public class MyDataList : List<MyData>
    {

    }

    public class MyControl
    {
    public object DataSourceObject;

    public void DataBind()
    {
    IEnumerable array = DataSourceObject as IEnumerable;

    foreach (var item in array)
    {
    Console.WriteLine(string.Format("========{0}========", item.ToString()));

    //use reflection to get all properties of item.
    //To test only extract strimg, bool, int properties.
    foreach (PropertyInfo pi in item.GetType().GetProperties())
    {
    if (pi.PropertyType == typeof(string) ||
    pi.PropertyType == typeof(int) ||
    pi.PropertyType == typeof(bool))
    {
    Console.WriteLine(string.Format("{0}: {1}"
    , pi.Name, pi.GetValue(item, null).ToString()));

    }
    }
    }

    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    MyDataList datasource = new MyDataList()
    {
    new MyData { BoolProperty = false, StrProperty="test", IntProperty=11 },
    new MyData { BoolProperty = true, StrProperty="test number 2", IntProperty=25 }
    };


    MyControl ctrl = new MyControl();
    ctrl.DataSourceObject = datasource;
    ctrl.DataBind();

    Console.ReadLine();
    }
    }


    }


    Wednesday, August 19, 2009 11:57 AM
  • User851098746 posted

    Thank you very much Allen. It's worked fine.

    I've made some small modifications on your code to match what I needed at the beginning and I'm listing it belwo for the benefit of other users.

    Thanks again.


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
    
        public class MyData
        {
            public string StrProperty { get; set; }
            public bool BoolProperty { get; set; }
            public int IntProperty { get; set; }
        }
    
        public class MyDataList : List<MyData>
        {
    
        }
    
        public class MyControl
        {
            public object DataSourceObject;
    
            public void DataBind()
            {
                IEnumerable array = DataSourceObject as IEnumerable;
    
                foreach (var item in array)
                {
                    Console.WriteLine(string.Format("========{0}========", item.ToString()));
    
                    //use reflection to get all properties of item.
                    //To test only extract strimg, bool, int properties.
                    foreach (PropertyInfo pi in item.GetType().GetProperties())
                    {
                        if (pi.PropertyType == typeof(string) ||
                            pi.PropertyType == typeof(int) ||
                            pi.PropertyType == typeof(bool))
                        {
                            Console.WriteLine(string.Format("{0}: {1}"
                                , pi.Name, pi.GetValue(item, null).ToString()));
    
                        }
                    }
                }
    
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                MyDataList datasource = new MyDataList()
                {
                    new MyData { BoolProperty = false, StrProperty="test", IntProperty=11 },
                    new MyData { BoolProperty = true, StrProperty="test number 2", IntProperty=25 }
                };
    
    
                MyControl ctrl = new MyControl();
                ctrl.DataSourceObject = datasource;
                ctrl.DataBind();
    
                Console.ReadLine();
            }
        }
    
    }
    




    Wednesday, August 19, 2009 11:58 AM