How are DataTextField and DataValueFields evaluated

1.2k views Asked by At

For a data bound control it is common scenario where we provide data text field and data value field ( in simple controls like Dropdownlist) but more fields in controls like Gridview. Generally the datasource is of type IEnumerable.

  • How does the control internally process these values or rather how do they get the value from the data source without knowing what kind of datasource they are dealing with.

  • Can someone explain with code how the controls evaluate these fields from the data source.

2

There are 2 answers

0
Deeptechtons On BEST ANSWER

I never knew i could find this information so easily and LLyod was in fact wrong on using reflection to find data from a datasource. None of the data controls use it when i inspected through Reflector ;(

link that solved the problem

http://msdn.microsoft.com/en-us/library/ms366540.aspx

how you do it is below

protected override void PerformDataBinding(IEnumerable retrievedData)
        {
            base.PerformDataBinding(retrievedData);

            // Verify data exists.
            if (retrievedData != null)
            {

                string dataStr = String.Empty;

                foreach (object dataItem in retrievedData)
                {
                    if (DataTextField.Length > 0)
                    {
                        dataStr = DataBinder.GetPropertyValue(dataItem,
                            DataTextField, null);
                    }
                    else
                    {
                        PropertyDescriptorCollection props =
                                TypeDescriptor.GetProperties(dataItem);
                        if (props.Count >= 1)
                        {
                            if (null != props[0].GetValue(dataItem))
                            {
                                dataStr = props[0].GetValue(dataItem).ToString();
                            }
                        }
                    }
                }

            }
        }

If the above code seem Greek and Latin , you will have to have a course on asp.net controls development to understand what is being done.

3
VinayC On
  1. Typically, the data-bound control (or concerned components such as DataControlField in GridView) will handle DataBinding event.

  2. Within event handler, the data item that is being currently bound (e.g. DataRowView or entity instance) is retrieved. This is done via DataBinder.GetDataItem passing the actual control or control's NamingContainer. For example, if you are implementing a lower level control such as DataControlField for higher level data-bound control such as GridView then it would handle data-binding of a cell control and hence it will use cell's naming container to pass to DataBinder.GetDataItem method which uses current data binding context to get the same.

  3. Once the data item object is retrieved, one need to evaluate the given data-binding expression against it to get the actual value and apply any formatting as per different properties set to the control/component. The most simple way is to use DataBinder.Eval overload. However, one may use the more efficient ways - for example, say DataField string is going to be only property name then you may look and cache the property descriptor and then use the same against different data items.

I will suggest you to use tool such as Reflector to inspect relevant control's code to get the better idea.