Custom DataBinding

Last post 08-19-2009 11:58 AM by net fan. 5 replies.

Sort Posts:

  • Custom DataBinding

    08-14-2009, 6:33 PM
    • Member
      546 point Member
    • net fan
    • Member since 11-03-2004, 3:52 AM
    • Amman, Jordan
    • Posts 181

    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

    WADIRUM Software is a Jordan-based specialized software provider for tourism-related industries.
  • Re: Custom DataBinding

    08-17-2009, 12:07 AM

    net fan:

    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.


     

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Custom DataBinding

    08-17-2009, 12:15 PM
    • Member
      546 point Member
    • net fan
    • Member since 11-03-2004, 3:52 AM
    • Amman, Jordan
    • Posts 181

    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.


    WADIRUM Software is a Jordan-based specialized software provider for tourism-related industries.
  • Re: Custom DataBinding

    08-18-2009, 2:33 AM
    Answer

     

    net fan:

    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();
            }

        }

     

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Custom DataBinding

    08-19-2009, 11:57 AM
    • Member
      546 point Member
    • net fan
    • Member since 11-03-2004, 3:52 AM
    • Amman, Jordan
    • Posts 181

    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();
    }
    }


    }


    WADIRUM Software is a Jordan-based specialized software provider for tourism-related industries.
  • Re: Custom DataBinding

    08-19-2009, 11:58 AM
    • Member
      546 point Member
    • net fan
    • Member since 11-03-2004, 3:52 AM
    • Amman, Jordan
    • Posts 181

    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();
            }
        }
    
    }
    




    WADIRUM Software is a Jordan-based specialized software provider for tourism-related industries.
Page 1 of 1 (6 items)