Files
acceptance-tests
data
debian
docs
external
ikvm-native
llvm
m4
man
mcs
build
class
Accessibility
Commons.Xml.Relaxng
Cscompmgd
CustomMarshalers
Facades
I18N
IBM.Data.DB2
ICSharpCode.SharpZipLib
Microsoft.Build
Microsoft.Build.Engine
Microsoft.Build.Framework
Microsoft.Build.Tasks
Microsoft.Build.Utilities
Microsoft.CSharp
Microsoft.NuGet.Build.Tasks
Microsoft.VisualC
Microsoft.Web.Infrastructure
MicrosoftAjaxLibrary
Mono.Btls.Interface
Mono.C5
Mono.CSharp
Mono.Cairo
Mono.Cecil
Mono.Cecil.Mdb
Mono.CodeContracts
Mono.CompilerServices.SymbolWriter
Mono.Data.Sqlite
Mono.Data.Tds
Mono.Debugger.Soft
Mono.Http
Mono.Management
Mono.Messaging
Mono.Messaging.RabbitMQ
Mono.Options
Mono.Parallel
Mono.Posix
Mono.Profiler.Log
Mono.Runtime.Tests
Mono.Security
Mono.Security.Win32
Mono.Simd
Mono.Tasklets
Mono.WebBrowser
Mono.XBuild.Tasks
Novell.Directory.Ldap
PEAPI
RabbitMQ.Client
SMDiagnostics
System
System.ComponentModel.Composition.4.5
System.ComponentModel.DataAnnotations
System.Configuration
System.Configuration.Install
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Linq
System.Data.OracleClient
System.Data.Services
System.Data.Services.Client
System.Deployment
System.Design
System.DirectoryServices
System.DirectoryServices.Protocols
System.Drawing
System.Drawing.Design
System.Dynamic
System.EnterpriseServices
System.IO.Compression
System.IO.Compression.FileSystem
System.IdentityModel
System.IdentityModel.Selectors
System.Json
System.Json.Microsoft
System.Management
System.Messaging
System.Net
System.Net.Http
System.Net.Http.Formatting
System.Net.Http.WebRequest
System.Net.Http.WinHttpHandler
System.Numerics
System.Numerics.Vectors
System.Reactive.Core
System.Reactive.Debugger
System.Reactive.Experimental
System.Reactive.Interfaces
System.Reactive.Linq
System.Reactive.Observable.Aliases
System.Reactive.PlatformServices
System.Reactive.Providers
System.Reactive.Runtime.Remoting
System.Reactive.Windows.Forms
System.Reactive.Windows.Threading
System.Reflection.Context
System.Runtime.Caching
System.Runtime.CompilerServices.Unsafe
System.Runtime.DurableInstancing
System.Runtime.Remoting
System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Soap
System.Security
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.Web
System.ServiceProcess
System.Threading.Tasks.Dataflow
System.Transactions
System.Web
System.Web.Abstractions
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Extensions
System.Web.Extensions.Design
System.Web.Http
System.Web.Http.SelfHost
System.Web.Http.WebHost
System.Web.Mobile
System.Web.Mvc3
System.Web.Razor
System.Web.RegularExpressions
System.Web.Routing
System.Web.Services
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
System.Windows
System.Windows.Forms
System.Windows.Forms.DataVisualization
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.XML
System.Xaml
System.Xml.Linq
System.Xml.Serialization
SystemWebTestShim
WebMatrix.Data
WindowsBase
aot-compiler
corlib
dlr
doc
legacy
lib
monodoc
notes
reference-assemblies
referencesource
SMDiagnostics
System
System.Activities
System.Activities.Core.Presentation
System.Activities.DurableInstancing
System.Activities.Presentation
System.ComponentModel.DataAnnotations
System.Configuration
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Entity.Design
System.Data.Linq
System.Data.SqlXml
System.IdentityModel
System.IdentityModel.Selectors
System.Net
System.Numerics
System.Runtime.Caching
System.Runtime.DurableInstancing
System.Runtime.Serialization
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Activities
System.ServiceModel.Channels
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.WasHosting
System.ServiceModel.Web
System.Web
System.Web.ApplicationServices
System.Web.DataVisualization
System.Web.DynamicData
DynamicData
ModelProviders
Util
ContainerType.cs
ContextConfiguration.cs
ControlFilterExpression.cs
DataBoundControlParameterTarget.cs
DataControlReference.cs
DataControlReferenceCollection.cs
DataSourceUtil.cs
DefaultAutoFieldGenerator.cs
DynamicControl.cs
DynamicControlParameter.cs
DynamicDataExtensions.cs
DynamicDataManager.cs
DynamicDataRoute.cs
DynamicDataRouteHandler.cs
DynamicEntity.cs
DynamicField.cs
DynamicFilter.cs
DynamicFilterExpression.cs
DynamicHyperLink.cs
DynamicQueryStringParameter.cs
DynamicRouteExpression.cs
DynamicValidator.cs
EntityTemplate.cs
EntityTemplateFactory.cs
EntityTemplateUserControl.cs
FieldTemplateFactory.cs
FieldTemplateUserControl.cs
FilterFactory.cs
FilterRepeater.cs
FilterUserControlBase.cs
IControlParameterTarget.cs
IFieldFormattingOptions.cs
IFieldTemplate.cs
IFieldTemplateFactory.cs
IFieldTemplateHost.cs
IFilterExpressionProvider.cs
IMetaChildrenColumn.cs
IMetaColumn.cs
IMetaForeignKeyColumn.cs
IMetaModel.cs
IMetaTable.cs
IWhereParametersProvider.cs
MetaChildrenColumn.cs
MetaColumn.cs
MetaForeignKeyColumn.cs
MetaModel.cs
MetaTable.cs
PageAction.cs
QueryStringHandler.cs
QueryableFilterRepeater.cs
QueryableFilterUserControl.cs
RepeaterDataBoundAdapter.cs
SimpleFieldTemplateFactory.cs
SimpleFieldTemplateUserControl.cs
TableNameAttribute.cs
Properties
Resources
UI
System.Web.Entity
System.Web.Entity.Design
System.Web.Extensions
System.Web.Mobile
System.Web.Routing
System.Web.Services
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.WorkflowServices
System.Xaml.Hosting
System.Xml
System.Xml.Linq
XamlBuildTask
mscorlib
LICENSE.txt
PATENTS.TXT
README.Mono.md
README.md
test-helpers
LICENSE
Makefile
Open.snk
README
ecma.pub
mono.pub
mono.snk
msfinal.pub
reactive.pub
silverlight.pub
winfx.pub
winfx3.pub
docs
errors
ilasm
jay
mcs
packages
tests
tools
AUTHORS
COPYING
INSTALL.txt
Makefile
MonoIcon.png
README
ScalableMonoIcon.svg
mkinstalldirs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/mcs/class/referencesource/System.Web.DynamicData/DynamicData/DynamicDataManager.cs

262 lines
10 KiB
C#
Raw Normal View History

namespace System.Web.DynamicData {
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.Security.Permissions;
using System.Web.Resources;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.DynamicData.Util;
using System.Data.Objects;
using IDataBoundControlInterface = System.Web.UI.WebControls.IDataBoundControl;
/// <summary>
/// Adds behavior to certain control to make them work with Dynamic Data
/// </summary>
[NonVisualControl()]
[ParseChildren(true)]
[PersistChildren(false)]
[ToolboxBitmap(typeof(DynamicDataManager), "DynamicDataManager.bmp")]
[Designer("System.Web.DynamicData.Design.DynamicDataManagerDesigner, " + AssemblyRef.SystemWebDynamicDataDesign)]
public class DynamicDataManager : Control {
private DataControlReferenceCollection _dataControls;
// Key is used as the set of registered data source controls. Value is ignored.
private Dictionary<IDynamicDataSource, object> _dataSources = new Dictionary<IDynamicDataSource, object>();
/// <summary>
/// Causes foreign entities to be loaded as well setting the proper DataLoadOptions.
/// Only works with Linq To Sql.
/// </summary>
[
Category("Behavior"),
DefaultValue(false),
ResourceDescription("DynamicDataManager_AutoLoadForeignKeys")
]
public bool AutoLoadForeignKeys {
get;
set;
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public override string ClientID {
get {
return base.ClientID;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public override ClientIDMode ClientIDMode {
get {
return base.ClientIDMode;
}
set {
throw new NotImplementedException();
}
}
[
Category("Behavior"),
DefaultValue(null),
PersistenceMode(PersistenceMode.InnerProperty),
MergableProperty(false),
]
public DataControlReferenceCollection DataControls {
get {
if (_dataControls == null) {
_dataControls = new DataControlReferenceCollection(this);
}
return _dataControls;
}
}
/// <summary>
/// See base class documentation
/// </summary>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public override bool Visible {
get {
return base.Visible;
}
set {
throw new NotImplementedException();
}
}
/// <summary>
/// See base class documentation
/// </summary>
[SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
protected override void OnInit(EventArgs e) {
base.OnInit(e);
// Initialize the collection
DataControls.Initialize();
// Subscribe to the Page's Init to register the controls set in the DataControls collection
Page.Init += OnPageInit;
}
private void OnPageInit(object sender, EventArgs e) {
foreach (DataControlReference controlReference in DataControls) {
Control targetControl = Misc.FindControl(this, controlReference.ControlID);
if (targetControl == null) {
throw new InvalidOperationException(
String.Format(CultureInfo.CurrentCulture,
DynamicDataResources.DynamicDataManager_ControlNotFound,
controlReference.ControlID));
}
RegisterControl(targetControl);
}
}
/// <summary>
/// See base class documentation
/// </summary>
[SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
// Go through all the registered data sources
foreach (IDynamicDataSource dataSource in _dataSources.Keys) {
// Expand any dynamic where parameters that they may use
dataSource.ExpandDynamicWhereParameters();
}
}
/// <summary>
/// Register a data control to give it Dynamic Data behavior
/// </summary>
/// <param name="control"></param>
public void RegisterControl(Control control) {
RegisterControl(control, false);
}
/// <summary>
/// Register a data control to give it Dynamic Data behavior
/// </summary>
/// <param name="setSelectionFromUrl">When true, if a primary key is found in the route values
/// (typically on the query string), it will get be set as the selected item. This only applies
/// to list controls.</param>
public void RegisterControl(Control control, bool setSelectionFromUrl) {
//
if (DesignMode) {
return;
}
IDataBoundControlInterface dataBoundControl = DataControlHelper.GetDataBoundControl(control, true /*failIfNotFound*/);
// If we can't get an associated IDynamicDataSource, don't do anything
IDynamicDataSource dataSource = dataBoundControl.DataSourceObject as IDynamicDataSource;
if (dataSource == null) {
return;
}
// If we can't get a MetaTable from the data source, don't do anything
MetaTable table = MetaTableHelper.GetTableWithFullFallback(dataSource, Context.ToWrapper());
// Save the datasource so we can process its parameters in OnLoad. The value we set is irrelevant
_dataSources[dataSource] = null;
((INamingContainer)control).SetMetaTable(table);
BaseDataBoundControl baseDataBoundControl = control as BaseDataBoundControl;
if (baseDataBoundControl != null) {
EnablePersistedSelection(baseDataBoundControl, table);
}
RegisterControlInternal(dataBoundControl, dataSource, table, setSelectionFromUrl, Page.IsPostBack);
}
internal static void EnablePersistedSelection(BaseDataBoundControl baseDataBoundControl, IMetaTable table) {
Debug.Assert(baseDataBoundControl != null, "NULL!");
// Make the persisted selection sync up with the selected index if possible
if (!table.IsReadOnly) {
DynamicDataExtensions.EnablePersistedSelectionInternal(baseDataBoundControl);
}
}
internal void RegisterControlInternal(IDataBoundControlInterface dataBoundControl, IDynamicDataSource dataSource, IMetaTable table, bool setSelectionFromUrl, bool isPostBack) {
// Set the auto field generator (for controls that support it - GridView and DetailsView)
IFieldControl fieldControl = dataBoundControl as IFieldControl;
if (fieldControl != null) {
fieldControl.FieldsGenerator = new DefaultAutoFieldGenerator(table);
}
var linqDataSource = dataSource as LinqDataSource;
var entityDataSource = dataSource as EntityDataSource;
// If the context type is not set, we need to set it
if (dataSource.ContextType == null) {
dataSource.ContextType = table.DataContextType;
// If it's a LinqDataSurce, register for ContextCreating so the context gets created using the correct ctor
// Ideally, this would work with other datasource, but we just don't have the right abstraction
if (linqDataSource != null) {
linqDataSource.ContextCreating += delegate(object sender, LinqDataSourceContextEventArgs e) {
e.ObjectInstance = table.CreateContext();
};
}
if (entityDataSource != null) {
entityDataSource.ContextCreating += delegate(object sender, EntityDataSourceContextCreatingEventArgs e) {
e.Context = (ObjectContext)table.CreateContext();
};
}
}
// If the datasource doesn't have an EntitySetName (aka TableName), set it from the meta table
if (String.IsNullOrEmpty(dataSource.EntitySetName)) {
dataSource.EntitySetName = table.DataContextPropertyName;
}
// If there is no Where clause, turn on auto generate
if (String.IsNullOrEmpty(dataSource.Where)) {
dataSource.AutoGenerateWhereClause = true;
}
// If it's a LinqDataSource and the flag is set, pre load the foreign keys
if (AutoLoadForeignKeys && linqDataSource != null) {
linqDataSource.LoadWithForeignKeys(table.EntityType);
}
if (!isPostBack) {
if (table.HasPrimaryKey) {
dataBoundControl.DataKeyNames = table.PrimaryKeyNames;
// Set the virtual selection from the URL if needed
var dataKeySelector = dataBoundControl as IPersistedSelector;
if (dataKeySelector != null && setSelectionFromUrl) {
DataKey dataKey = table.GetDataKeyFromRoute();
if (dataKey != null) {
dataKeySelector.DataKey = dataKey;
}
}
}
}
}
internal static IControlParameterTarget GetControlParameterTarget(Control control) {
return (control as IControlParameterTarget) ?? new DataBoundControlParameterTarget(control);
}
}
}