internal static IEnumerable <Parameter> GetControlParameters(Control control, IDictionary <string, MetaColumn> nameColumnMapping)
        {
            IControlParameterTarget target = null;

            target = DynamicDataManager.GetControlParameterTarget(control);
            Debug.Assert(target != null);

            foreach (var entry in nameColumnMapping)
            {
                string     parameterName = entry.Key;
                MetaColumn column        = entry.Value;

                ControlParameter controlParameter = new ControlParameter()
                {
                    Name      = parameterName,
                    ControlID = control.UniqueID
                };
                if (target != null)
                {
                    // this means the relationship consists of more than one key and we need to expand the property name
                    controlParameter.PropertyName = target.GetPropertyNameExpression(column.Name);
                }
                DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, column);
                yield return(controlParameter);
            }
        }
        /// <summary>
        /// See IWhereParametersProvider.GetWhereParameters
        /// </summary>
        public virtual IEnumerable <Parameter> GetWhereParameters(IDynamicDataSource dataSource)
        {
            Debug.Assert(dataSource != null);

            // Find the control that the ControlParameter uses
            Control control = Misc.FindControl((Control)dataSource, ControlId);

            if (control == null)
            {
                throw new InvalidOperationException(String.Format(
                                                        CultureInfo.CurrentCulture, DynamicDataResources.DynamicControlParameter_DynamicDataSourceControlNotFound, ControlId));
            }

            // If the control is itself a parameter provider, delegate to it
            var whereParametersProvider = control as IWhereParametersProvider;

            if (whereParametersProvider != null)
            {
                return(whereParametersProvider.GetWhereParameters(dataSource));
            }

            IControlParameterTarget paramTarget = DynamicDataManager.GetControlParameterTarget(control);

            if (paramTarget == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                  DynamicDataResources.DynamicControlParameter_DynamicDataSourceControlCannotBeUsedAsParent, ControlId));
            }

            string     columnName = Name;
            MetaColumn column     = null;
            MetaTable  table      = MetaTableHelper.GetTableWithFullFallback(dataSource, HttpContext.Current.ToWrapper());

            if (!String.IsNullOrEmpty(columnName))
            {
                column = table.GetColumn(columnName);
            }
            else
            {
                // There was no Name attribute telling us what field to filter, but maybe
                // the control given us data has that info
                column = paramTarget.FilteredColumn;
            }

            if (column == null)
            {
                // If there is no specific column, we're setting the primary key

                if (paramTarget.Table != table)
                {
                    throw new Exception(String.Format(CultureInfo.CurrentCulture,
                                                      DynamicDataResources.DynamicControlParameter_InvalidPK,
                                                      ControlId, paramTarget.Table, table.Name));
                }

                return(GetPrimaryKeyControlWhereParameters(control, paramTarget));
            }
            else if (column is MetaForeignKeyColumn)
            {
                return(GetForeignKeyControlWhereParameters(control, paramTarget, (MetaForeignKeyColumn)column));
            }
            return(GetPropertyControlWhereParameters(control, paramTarget, column));
        }