Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / DynamicHyperLink.cs / 1305376 / DynamicHyperLink.cs
using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Globalization; using System.Security.Permissions; using System.Web.Compilation; using System.Web.Resources; using System.Web.Routing; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.DynamicData.Util; namespace System.Web.DynamicData { ////// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "HyperLink", Justification="It's an extension of the HyperLink class")] [DefaultProperty("Action")] [ToolboxBitmap(typeof(DynamicHyperLink), "DynamicHyperLink.bmp")] public class DynamicHyperLink : HyperLink, IAttributeAccessor { private HttpContextBase _context; private bool _dataBound; private object _dataItem; private DictionaryA control that displays links to table actions based on routing rules. It will not generate links for actions that are not /// allowed by the routing rules. It can work in 3 modes: explicit, databinding to MetaTable, or databinding to a data row. ///Databinding to MetaTable allows for creating links to actions for a collection of MetaTable objects (such as in the Default.aspx /// page in the project templates) ///Databinding to a data row allows for creating links to actions for data rows retrieved from a database. These are usually used with /// Edit and Details actions. ///Explicit mode allows for links to non-item-specific actions (like List and Insert) and is achieved by properly setting /// ContextTypeName, Table, and Action properties. This is done in the PreRender phase if the NavigateUrl property is null (i.e. it has not /// been set explicitly or did not get set in one of the databinding scenarios.) ///Extra route parameters can be provided by declaring expando attributes on the controls markup. ///_extraRouteParams = new Dictionary (); /// /// The name of the action /// [TypeConverter(typeof(ActionConverter))] [DefaultValue("")] [Category("Navigation")] [ResourceDescription("DynamicHyperLink_Action")] public string Action { get { object o = ViewState["Action"]; return (o == null ? String.Empty: (string)o); } set { ViewState["Action"] = value; } } internal new HttpContextBase Context { get { return _context ?? new HttpContextWrapper(base.Context); } set { _context = value; } } ////// The name of the context type /// [DefaultValue("")] [Category("Navigation")] [ResourceDescription("DynamicHyperLink_ContextTypeName")] public string ContextTypeName { get { object o = ViewState["ContextTypeName"]; return ((o == null) ? String.Empty : (string)o); } set { ViewState["ContextTypeName"] = value; } } ////// The name of the column whose value will be used to populate the Text /// property if it is not already set in data binding scenarios. /// [DefaultValue("")] [Category("Navigation")] [ResourceDescription("DynamicHyperLink_DataField")] public string DataField { get { object o = ViewState["DataField"]; return ((o == null) ? String.Empty : (string)o); } set { ViewState["DataField"] = value; } } // for unit testing purposes internal object Page_DataItem { get { return _dataItem ?? Page.GetDataItem(); } set { _dataItem = value; } } ////// The name of the table /// [DefaultValue("")] [Category("Navigation")] [ResourceDescription("DynamicHyperLink_TableName")] public string TableName { get { object o = ViewState["TableName"]; return ((o == null) ? String.Empty : (string)o); } set { ViewState["TableName"] = value; } } [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", MessageId = "0#")] protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); if (DesignMode) { return; } if (!String.IsNullOrEmpty(NavigateUrl)) { // stop processing if there already is a URL return; } if (!String.IsNullOrEmpty(TableName) || !String.IsNullOrEmpty(ContextTypeName)) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicHyperLink_CannotSetTableAndContextWhenDatabinding, this.ID)); } object dataItem = Page_DataItem; if (dataItem == null) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicHyperLink_CannotBindToNull, this.ID)); } MetaTable table = dataItem as MetaTable; if (table != null) { BindToMetaTable(table); } else { BindToDataItem(dataItem); } _dataBound = true; } private void BindToMetaTable(MetaTable table) { string action = GetActionOrDefaultTo(PageAction.List); NavigateUrl = table.GetActionPath(action, GetRouteValues()); if (String.IsNullOrEmpty(Text)) { Text = table.DisplayName; } } private void BindToDataItem(object dataItem) { dataItem = Misc.GetRealDataItem(dataItem); Debug.Assert(dataItem != null, "DataItem is null"); // Try to get the MetaTable from the type and if we can't find it then ---- up. MetaTable table = Misc.GetTableFromTypeHierarchy(dataItem.GetType()); if (table == null) { throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, DynamicDataResources.MetaModel_EntityTypeDoesNotBelongToModel, dataItem.GetType().FullName)); } string action = GetActionOrDefaultTo(PageAction.Details); NavigateUrl = table.GetActionPath(action, GetRouteValues(table, dataItem)); if (String.IsNullOrEmpty(Text)) { if (!String.IsNullOrEmpty(DataField)) { Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString(); } else { Text = table.GetDisplayString(dataItem); } } } [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", MessageId = "0#")] protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (DesignMode) { if (!String.IsNullOrEmpty(NavigateUrl)) { NavigateUrl = "DesignTimeUrl"; } return; } // check both _dataBound and NavigateUrl cause NavigateUrl might be empty if routing/scaffolding // does not allow a particular action if (!_dataBound && String.IsNullOrEmpty(NavigateUrl)) { MetaTable table; try { table = GetTable(); } catch (Exception exception) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicHyperLink_CannotDetermineTable, this.ID), exception); } if (table == null) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicHyperLink_CannotDetermineTable, this.ID)); } var action = GetActionOrDefaultTo(PageAction.List); NavigateUrl = table.GetActionPath(action, GetRouteValues()); } } private RouteValueDictionary GetRouteValues() { var routeValues = new RouteValueDictionary(); foreach (var entry in _extraRouteParams) { string key = entry.Key; routeValues[key] = entry.Value; } return routeValues; } private RouteValueDictionary GetRouteValues(MetaTable table, object row) { RouteValueDictionary routeValues = GetRouteValues(); foreach (var pk in table.GetPrimaryKeyDictionary(row)) { routeValues[pk.Key] = pk.Value; } return routeValues; } private string GetActionOrDefaultTo(string defaultAction) { return String.IsNullOrEmpty(Action) ? defaultAction : Action; } // internal for unit testing internal virtual MetaTable GetTable() { MetaTable table; if (!String.IsNullOrEmpty(TableName)) { table = GetTableFromTableName(); } else { table = DynamicDataRouteHandler.GetRequestMetaTable(Context); } return table; } private MetaTable GetTableFromTableName() { var tableName = TableName; var contextTypeName = ContextTypeName; Debug.Assert(!String.IsNullOrEmpty(tableName)); if (!String.IsNullOrEmpty(contextTypeName)) { // context type allows to disambiguate table names Type contextType = BuildManager.GetType(contextTypeName, /* throwOnError */ true, /* ignoreCase */ true); MetaModel model = MetaModel.GetModel(contextType); MetaTable table = model.GetTable(tableName, contextType); return table; } else { var table = DynamicDataRouteHandler.GetRequestMetaTable(Context); if (table == null) { return null; } return table.Model.GetTable(tableName); } } #region IAttributeAccessor Members string IAttributeAccessor.GetAttribute(string key) { return (string)_extraRouteParams[key]; } void IAttributeAccessor.SetAttribute(string key, string value) { _extraRouteParams[key] = value; } #endregion } } // 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
- IfJoinedCondition.cs
- NotifyCollectionChangedEventArgs.cs
- SelectionPattern.cs
- HuffCodec.cs
- HttpCapabilitiesSectionHandler.cs
- DefaultTraceListener.cs
- AppSettings.cs
- AccessorTable.cs
- OrderingInfo.cs
- DataGridPageChangedEventArgs.cs
- Quaternion.cs
- PrintingPermissionAttribute.cs
- AuthorizationRule.cs
- MessageEncoderFactory.cs
- BrowserCapabilitiesCompiler.cs
- IBuiltInEvidence.cs
- AppearanceEditorPart.cs
- RelOps.cs
- CommonDialog.cs
- FrameworkContentElement.cs
- RuntimeConfigurationRecord.cs
- CheckableControlBaseAdapter.cs
- ComponentRenameEvent.cs
- TrustLevel.cs
- DataColumnPropertyDescriptor.cs
- DataFormats.cs
- SiteMapPathDesigner.cs
- UmAlQuraCalendar.cs
- RuntimeHelpers.cs
- CompositeFontInfo.cs
- RequestCacheEntry.cs
- DbInsertCommandTree.cs
- BaseHashHelper.cs
- AlphaSortedEnumConverter.cs
- EventMap.cs
- GPStream.cs
- ParserContext.cs
- SystemUdpStatistics.cs
- PartialCachingAttribute.cs
- AsyncCompletedEventArgs.cs
- ComponentSerializationService.cs
- WorkItem.cs
- DataTable.cs
- ToolboxDataAttribute.cs
- Vector3dCollection.cs
- elementinformation.cs
- FixedDSBuilder.cs
- ObservableCollection.cs
- AppDomainShutdownMonitor.cs
- VectorAnimationBase.cs
- BoolExpr.cs
- WebReferenceOptions.cs
- SoapObjectInfo.cs
- Effect.cs
- ISAPIApplicationHost.cs
- DurableOperationAttribute.cs
- QilReplaceVisitor.cs
- HiddenFieldPageStatePersister.cs
- BinaryHeap.cs
- ACL.cs
- TimeManager.cs
- XmlJsonWriter.cs
- RangeBase.cs
- HttpPostClientProtocol.cs
- ExceptionUtility.cs
- SqlConnectionManager.cs
- XmlSchemaValidationException.cs
- EndpointBehaviorElement.cs
- Soap.cs
- SecurityTokenProvider.cs
- RawStylusSystemGestureInputReport.cs
- ReflectEventDescriptor.cs
- CodeTypeDeclarationCollection.cs
- AutomationPeer.cs
- AnnotationResource.cs
- TableColumnCollectionInternal.cs
- BaseCollection.cs
- SerTrace.cs
- PerfCounterSection.cs
- localization.cs
- LicenseException.cs
- TraversalRequest.cs
- RpcResponse.cs
- AsyncCallback.cs
- GifBitmapEncoder.cs
- ExpressionBuilder.cs
- PaperSize.cs
- IISMapPath.cs
- ArraySet.cs
- _Rfc2616CacheValidators.cs
- SSmlParser.cs
- MobileTemplatedControlDesigner.cs
- DbConnectionStringCommon.cs
- ComPersistableTypeElement.cs
- DataGridColumnCollection.cs
- SynchronizedCollection.cs
- TextClipboardData.cs
- TCPClient.cs
- MSHTMLHost.cs
- QueryCoreOp.cs