Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / DynamicControlParameter.cs / 1305376 / DynamicControlParameter.cs
using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Web.DynamicData.Util; using System.Web.Resources; using System.Web.UI; using System.Web.UI.WebControls; namespace System.Web.DynamicData { ////// DynamicControlParameter is similar to ControlParameter, but understainds higher level concepts. e.g. in a /// master-details scenario using a GridView and DetailsView, you only need to point the DetailsView's datasource /// to the GridView (using a DynamicControlParameter), and it does the right thing. This works even for /// multi-part primary keys /// public class DynamicControlParameter : Parameter, IWhereParametersProvider { ////// public DynamicControlParameter() { } ////// public DynamicControlParameter(string controlId) { ControlId = controlId; } ////// The ID of the control from which the parameter gets its data /// public string ControlId { get; set; } ////// See IWhereParametersProvider.GetWhereParameters /// public virtual IEnumerableGetWhereParameters(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); } private IEnumerable GetPropertyControlWhereParameters(Control control, IControlParameterTarget paramTarget, MetaColumn column) { ControlParameter controlParameter = new ControlParameter() { Name = column.Name, ControlID = control.UniqueID, PropertyName = paramTarget.GetPropertyNameExpression(column.Name) }; DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, column); yield return controlParameter; } private IEnumerable GetPrimaryKeyControlWhereParameters(Control control, IControlParameterTarget paramTarget) { MetaTable parentTable = paramTarget.Table; if (parentTable != null) { // For each PK column in the table, we need to create a ControlParameter foreach (var keyColumn in parentTable.PrimaryKeyColumns) { var controlParameter = new ControlParameter() { Name = keyColumn.Name, ControlID = control.UniqueID, PropertyName = paramTarget.GetPropertyNameExpression(keyColumn.Name) }; DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, keyColumn); yield return controlParameter; } } } private IEnumerable GetForeignKeyControlWhereParameters(Control control, IControlParameterTarget paramTarget, MetaForeignKeyColumn column) { MetaTable parentTable = paramTarget.Table; if (parentTable != null) { string namePrefix = String.Empty; // Make sure the data types match if (column.ColumnType != parentTable.EntityType) { throw new Exception(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicControlParameter_DynamicDataSourceColumnNotCompatibleWithTable, column.DisplayName, parentTable.Name)); } // For each underlying FK, we need to create a ControlParameter Debug.Assert(column.ForeignKeyNames.Count == parentTable.PrimaryKeyColumns.Count); int index = 0; foreach (var fkName in column.ForeignKeyNames) { MetaColumn parentTablePKColumn = parentTable.PrimaryKeyColumns[index++]; var controlParameter = new ControlParameter() { Name = fkName, ControlID = control.UniqueID, PropertyName = paramTarget.GetPropertyNameExpression(parentTablePKColumn.Name) }; DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, parentTablePKColumn); yield return controlParameter; } } } /// /// same as base /// /// /// ///protected override object Evaluate(HttpContext context, Control control) { // If this gets called, it means we never had a chance to expand the parameter. Give an error // telling the user to use a DynamicDataManager throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, DynamicDataResources.DynamicParameter_NeedExpansion, typeof(DynamicControlParameter).Name)); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Web.DynamicData.Util; using System.Web.Resources; using System.Web.UI; using System.Web.UI.WebControls; namespace System.Web.DynamicData { /// /// DynamicControlParameter is similar to ControlParameter, but understainds higher level concepts. e.g. in a /// master-details scenario using a GridView and DetailsView, you only need to point the DetailsView's datasource /// to the GridView (using a DynamicControlParameter), and it does the right thing. This works even for /// multi-part primary keys /// public class DynamicControlParameter : Parameter, IWhereParametersProvider { ////// public DynamicControlParameter() { } ////// public DynamicControlParameter(string controlId) { ControlId = controlId; } ////// The ID of the control from which the parameter gets its data /// public string ControlId { get; set; } ////// See IWhereParametersProvider.GetWhereParameters /// public virtual IEnumerableGetWhereParameters(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); } private IEnumerable GetPropertyControlWhereParameters(Control control, IControlParameterTarget paramTarget, MetaColumn column) { ControlParameter controlParameter = new ControlParameter() { Name = column.Name, ControlID = control.UniqueID, PropertyName = paramTarget.GetPropertyNameExpression(column.Name) }; DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, column); yield return controlParameter; } private IEnumerable GetPrimaryKeyControlWhereParameters(Control control, IControlParameterTarget paramTarget) { MetaTable parentTable = paramTarget.Table; if (parentTable != null) { // For each PK column in the table, we need to create a ControlParameter foreach (var keyColumn in parentTable.PrimaryKeyColumns) { var controlParameter = new ControlParameter() { Name = keyColumn.Name, ControlID = control.UniqueID, PropertyName = paramTarget.GetPropertyNameExpression(keyColumn.Name) }; DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, keyColumn); yield return controlParameter; } } } private IEnumerable GetForeignKeyControlWhereParameters(Control control, IControlParameterTarget paramTarget, MetaForeignKeyColumn column) { MetaTable parentTable = paramTarget.Table; if (parentTable != null) { string namePrefix = String.Empty; // Make sure the data types match if (column.ColumnType != parentTable.EntityType) { throw new Exception(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicControlParameter_DynamicDataSourceColumnNotCompatibleWithTable, column.DisplayName, parentTable.Name)); } // For each underlying FK, we need to create a ControlParameter Debug.Assert(column.ForeignKeyNames.Count == parentTable.PrimaryKeyColumns.Count); int index = 0; foreach (var fkName in column.ForeignKeyNames) { MetaColumn parentTablePKColumn = parentTable.PrimaryKeyColumns[index++]; var controlParameter = new ControlParameter() { Name = fkName, ControlID = control.UniqueID, PropertyName = paramTarget.GetPropertyNameExpression(parentTablePKColumn.Name) }; DataSourceUtil.SetParameterTypeCodeAndDbType(controlParameter, parentTablePKColumn); yield return controlParameter; } } } /// /// same as base /// /// /// ///protected override object Evaluate(HttpContext context, Control control) { // If this gets called, it means we never had a chance to expand the parameter. Give an error // telling the user to use a DynamicDataManager throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, DynamicDataResources.DynamicParameter_NeedExpansion, typeof(DynamicControlParameter).Name)); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Logging.cs
- X509Utils.cs
- SafeFileMapViewHandle.cs
- WorkerRequest.cs
- XmlSchemaNotation.cs
- TileModeValidation.cs
- StringPropertyBuilder.cs
- SubstitutionResponseElement.cs
- XmlParserContext.cs
- ColumnResult.cs
- DataGridViewCellPaintingEventArgs.cs
- SmtpDateTime.cs
- HostingEnvironmentException.cs
- QueryRelOp.cs
- ProjectedSlot.cs
- XamlPointCollectionSerializer.cs
- ToolStripItem.cs
- ColumnBinding.cs
- Rijndael.cs
- AccessViolationException.cs
- TreeNodeStyleCollectionEditor.cs
- _StreamFramer.cs
- DescriptionAttribute.cs
- SynchronizedInputAdaptor.cs
- UserNameSecurityTokenProvider.cs
- PropertyChangeTracker.cs
- XamlDesignerSerializationManager.cs
- WmpBitmapEncoder.cs
- MessageBox.cs
- COMException.cs
- BasicHttpMessageSecurity.cs
- DbConnectionClosed.cs
- AnonymousIdentificationSection.cs
- PackageStore.cs
- ExpressionEvaluator.cs
- ClrProviderManifest.cs
- DataObjectMethodAttribute.cs
- SafeFileHandle.cs
- XmlDesignerDataSourceView.cs
- ProcessDesigner.cs
- ConnectionPointCookie.cs
- AssemblyNameProxy.cs
- KerberosRequestorSecurityToken.cs
- KeyGestureConverter.cs
- Attribute.cs
- PersistChildrenAttribute.cs
- Debug.cs
- DomainUpDown.cs
- DataGridViewCheckBoxColumn.cs
- SettingsProviderCollection.cs
- TabControlEvent.cs
- IPipelineRuntime.cs
- GeneralTransform3D.cs
- HMACSHA256.cs
- Common.cs
- ElementHostAutomationPeer.cs
- XslCompiledTransform.cs
- ColorContext.cs
- TreeNodeStyleCollection.cs
- ExceptionRoutedEventArgs.cs
- DictionaryContent.cs
- RegexCode.cs
- DispatcherExceptionEventArgs.cs
- Transform3D.cs
- DoubleAnimationBase.cs
- ConditionalAttribute.cs
- IconHelper.cs
- TextBreakpoint.cs
- SyndicationSerializer.cs
- XmlRawWriter.cs
- LayoutTableCell.cs
- TemplateXamlParser.cs
- Soap.cs
- InputProcessorProfilesLoader.cs
- MasterPageCodeDomTreeGenerator.cs
- MethodCallTranslator.cs
- MemberDescriptor.cs
- StatusStrip.cs
- SoapEnumAttribute.cs
- QilList.cs
- Merger.cs
- DataGridColumnEventArgs.cs
- CannotUnloadAppDomainException.cs
- oledbconnectionstring.cs
- XmlTextReaderImpl.cs
- RelationalExpressions.cs
- Walker.cs
- EvidenceTypeDescriptor.cs
- BasicExpandProvider.cs
- MsmqDiagnostics.cs
- SqlFileStream.cs
- FilterableAttribute.cs
- SafeMILHandle.cs
- _FtpDataStream.cs
- OpenTypeLayout.cs
- DetailsViewCommandEventArgs.cs
- SpecialTypeDataContract.cs
- BamlRecords.cs
- InputBuffer.cs
- WebPartZoneCollection.cs