2 // DynamicDataExtensions.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Marek Habersack <mhabersack@novell.com>
8 // Copyright (C) 2008-2009 Novell Inc. http://novell.com
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.Security.Permissions;
37 using System.Security.Principal;
38 using System.Web.Caching;
40 using System.Web.UI.WebControls;
41 using System.Web.DynamicData.ModelProviders;
43 namespace System.Web.DynamicData
45 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 public static class DynamicDataExtensions
48 public static object ConvertEditedValue (this IFieldFormattingOptions formattingOptions, string value)
50 // Not a surprise anymore...
51 if (formattingOptions == null)
52 throw new NullReferenceException ();
54 if (String.IsNullOrEmpty (value)) {
55 if (formattingOptions.ConvertEmptyStringToNull)
58 string nullDisplayText = formattingOptions.NullDisplayText;
59 if (!String.IsNullOrEmpty (nullDisplayText) && String.Compare (value, nullDisplayText, StringComparison.Ordinal) == 0)
67 public static void EnablePersistedSelection (this BaseDataBoundControl dataBoundControl)
69 throw new NotImplementedException ();
73 public static void ExpandDynamicWhereParameters (this IDynamicDataSource dataSource)
75 // http://forums.asp.net/p/1396453/3005197.aspx#3005197
76 throw new NotImplementedException ();
80 public static IDynamicDataSource FindDataSourceControl (this Control current)
82 var control = current as BaseDataBoundControl;
86 string dataSourceID = control.DataSourceID;
87 if (!String.IsNullOrEmpty (dataSourceID))
88 return control.DataSource as IDynamicDataSource;
90 Control namingContainer = control.NamingContainer;
91 IDynamicDataSource dds;
92 while (namingContainer != null) {
93 dds = namingContainer.FindControl (dataSourceID) as IDynamicDataSource;
97 namingContainer = namingContainer.NamingContainer;
104 public static Control FindFieldTemplate (this Control control, string columnName)
106 throw new NotImplementedException ();
109 public static MetaTable FindMetaTable (this Control current)
111 // .NET doesn't perform the check, we will
113 throw new NullReferenceException ();
115 while (current != null) {
116 DataBoundControl dbc = current as DataBoundControl;
118 IDynamicDataSource dds = dbc.DataSourceObject as IDynamicDataSource;
120 return dds.GetTable ();
123 current = current.NamingContainer;
130 public static string FormatEditValue (this IFieldFormattingOptions formattingOptions, object fieldValue)
132 throw new NotImplementedException ();
136 public static string FormatValue (this IFieldFormattingOptions formattingOptions, object fieldValue)
138 throw new NotImplementedException ();
141 static string GetDataSourceId (IDynamicDataSource dataSource)
143 Control c = dataSource as Control;
150 public static MetaTable GetTable (this IDynamicDataSource dataSource)
152 if (dataSource == null)
155 string entitySetName = dataSource.EntitySetName;
156 if (String.IsNullOrEmpty (entitySetName)) {
157 // LAMESPEC: MSDN says we should throw in this case, but .NET calls
158 // DynamicDataRouteHandler.GetRequestMetaTable(HttpContext
159 // httpContext) instead (eventually)
160 MetaTable ret = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
162 throw new InvalidOperationException ("The control '" + GetDataSourceId (dataSource) +
163 "' does not have a TableName property and a table name cannot be inferred from the URL.");
166 Type contextType = dataSource.ContextType;
167 if (contextType == null)
168 throw new InvalidOperationException ("The ContextType property of control '" + GetDataSourceId (dataSource) + "' must specify a data context");
170 return MetaModel.GetModel (contextType).GetTable (entitySetName);
174 public static void LoadWithForeignKeys (this LinqDataSource dataSource, Type rowType)
176 throw new NotImplementedException ();