namespace System.Web.DynamicData {
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Data.Linq;
using System.Diagnostics;
using System.Globalization;
using System.Linq.Expressions;
using System.Web;
using System.Web.DynamicData.Util;
using System.Web.Resources;
using System.Web.UI;
using System.Web.UI.WebControls;
#if ORYX_VNEXT
using System.Data.Metadata.Edm;
using System.Linq;
using Microsoft.Web.Data.UI.WebControls;
using Microsoft.Web.Data.UI.WebControls.Expressions;
using LinqDataSource = Microsoft.Web.Data.UI.WebControls.LinqDataSource;
using LinqDataSourceStatusEventArgs = Microsoft.Web.Data.UI.WebControls.LinqDataSourceStatusEventArgs;
#endif
///
/// Extension methods used by DynamicData
///
public static class DynamicDataExtensions {
#if ORYX_VNEXT
public static string GetUniqueIDRelativeTo(this Control control, Control target) {
if (target == null) {
throw new ArgumentNullException("control");
}
if (!control.IsDescendentOf(target.NamingContainer)) {
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "This control is not a descendent of the NamingContainer of '{0}'.", target.ID));
}
if (target.NamingContainer == target.Page) {
return control.UniqueID;
} else {
return control.UniqueID.Substring(target.NamingContainer.UniqueID.Length + 1); // add 1 for the ID seperator (which is a char)
}
}
internal static bool IsDescendentOf(this Control control, Control ancestor) {
Control current = control;
while (current != ancestor && current.Parent != null) {
current = current.Parent;
}
return (current == ancestor);
}
internal static IEnumerable GetDependentProperties(this NavigationProperty navigationProperty) {
AssociationType relationshipType = (AssociationType)navigationProperty.RelationshipType;
ReferentialConstraint constraint = relationshipType.ReferentialConstraints.FirstOrDefault(c => c.ToRole == navigationProperty.FromEndMember);
if (constraint != null) {
return constraint.ToProperties;
}
return Enumerable.Empty();
}
internal static EntityType GetEntityType(this RelationshipEndMember member) {
return (EntityType)member.TypeUsage.EdmType.MetadataProperties.Single(prop => prop.Name == "ElementType").Value;
}
#endif
public static void SetMetaTable(this INamingContainer control, MetaTable table) {
SetMetaTableInternal(control, table, null/* defaultValues*/, new HttpContextWrapper(HttpContext.Current));
}
public static void SetMetaTable(this INamingContainer control, MetaTable table, IDictionary defaultValues) {
if (defaultValues == null) {
throw new ArgumentNullException("defaultValues");
}
SetMetaTableInternal(control, table, defaultValues, new HttpContextWrapper(HttpContext.Current));
}
public static void SetMetaTable(this INamingContainer control, MetaTable table, object defaultValues) {
if (defaultValues == null) {
throw new ArgumentNullException("defaultValues");
}
SetMetaTableInternal(control, table, Misc.ConvertObjectToDictionary(defaultValues), new HttpContextWrapper(HttpContext.Current));
}
public static IDictionary GetDefaultValues(this IDataSource dataSource) {
return GetDefaultValues(dataSource, new HttpContextWrapper(HttpContext.Current));
}
public static IDictionary GetDefaultValues(this INamingContainer control) {
return GetDefaultValues(control, new HttpContextWrapper(HttpContext.Current));
}
public static MetaTable GetMetaTable(this IDataSource dataSource) {
return GetMetaTable(dataSource, new HttpContextWrapper(HttpContext.Current));
}
public static bool TryGetMetaTable(this IDataSource dataSource, out MetaTable table) {
return TryGetMetaTable(dataSource, new HttpContextWrapper(HttpContext.Current), out table);
}
public static MetaTable GetMetaTable(this INamingContainer control) {
return GetMetaTable(control, new HttpContextWrapper(HttpContext.Current));
}
public static bool TryGetMetaTable(this INamingContainer control, out MetaTable table) {
return TryGetMetaTable(control, new HttpContextWrapper(HttpContext.Current), out table);
}
internal static void ApplyFieldGenerator(INamingContainer control, MetaTable table) {
GridView gridView = control as GridView;
if (gridView != null && gridView.AutoGenerateColumns && gridView.ColumnsGenerator == null) {
gridView.ColumnsGenerator = new DefaultAutoFieldGenerator(table);
}
else {
DetailsView detailsView = control as DetailsView;
if (detailsView != null && detailsView.AutoGenerateRows && detailsView.RowsGenerator == null) {
detailsView.RowsGenerator = new DefaultAutoFieldGenerator(table);
}
}
}
internal static DefaultValueMapping GetDefaultValueMapping(object control, HttpContextBase context) {
IDictionary