Files
mono/mcs/class/System.Web/System.Web.UI/TemplateControl.cs
T
Sebastien Pouliot 7836b64c9d 2005-09-19 Sebastien Pouliot <[email protected]>
* ControlBuilder.cs: Added BuildObject override for 2.0 profile.
	* TagPrefixAttribute.cs: Added LinkDemand for AspNetHostingPermission
	with Minimal level. Fixed checks on ctor.
	* TemplateBuilder.cs: Added LinkDemand and InheritanceDemand for 
	AspNetHostingPermission with Minimal level.
	* TemplateContainerAttribute.cs: Added LinkDemand for 
	AspNetHostingPermission with Minimal level.
	* TemplateControl.cs: Added LinkDemand and InheritanceDemand for 
	AspNetHostingPermission with Minimal level. Fixed checks/exceptions. 
	Stubbed IFilterResolutionService for CAS testing.
	* TemplateControlParser.cs: Added LinkDemand and InheritanceDemand for
	AspNetHostingPermission with Minimal level.
	* TemplateParser.cs: Added LinkDemand and InheritanceDemand for 
	AspNetHostingPermission with Minimal level.
	* ThemeableAttribute.cs: Added LinkDemand for AspNetHostingPermission
	with Minimal level. Removed IDispose interface.
	* ToolboxDataAttribute.cs: Added LinkDemand for 
	AspNetHostingPermission with Minimal level. Fixed IsDefaultAttribute 
	to work on both 1.x and 2.0 profiles.
	* Triplet.cs: Added LinkDemand and (only for 1.x) InheritanceDemand 
	for AspNetHostingPermission with Minimal level.
	* UserControl.cs: Added LinkDemand and InheritanceDemand for 
	AspNetHostingPermission with Minimal level. Stubbed 
	IFilterResolutionService for CAS testing.
	* UserControlControlBuilder.cs: Added LinkDemand and InheritanceDemand
	for AspNetHostingPermission with Minimal level.
	* ValidationPropertyAttribute.cs: Added LinkDemand for 
	AspNetHostingPermission with Minimal level.
	* ValidatorCollection.cs: Added LinkDemand for AspNetHostingPermission 
	with Minimal level.
	* WebResourceAttribute.cs: Removed extra ctor and added setter to 
	PerformSubstitution.
	* WebServiceParser.cs: Added LinkDemand and InheritanceDemand for 
	AspNetHostingPermission with Minimal level.


svn path=/trunk/mcs/; revision=50230
2005-09-19 13:55:35 +00:00

357 lines
9.2 KiB
C#

//
// System.Web.UI.TemplateControl.cs
//
// Authors:
// Duncan Mak ([email protected])
// Gonzalo Paniagua Javier ([email protected])
// Andreas Nahr ([email protected])
//
// (C) 2002 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Security.Permissions;
using System.Web.Compilation;
using System.Web.Util;
namespace System.Web.UI {
// CAS
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
#if NET_2_0
public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService {
#else
public abstract class TemplateControl : Control, INamingContainer {
#endif
static object abortTransaction = new object ();
static object commitTransaction = new object ();
static object error = new object ();
static string [] methodNames = { "Page_Init",
"Page_Load",
"Page_DataBind",
"Page_PreRender",
"Page_Disposed",
"Page_Error",
"Page_Unload",
"Page_AbortTransaction",
"Page_CommitTransaction" };
const BindingFlags bflags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
#region Constructor
protected TemplateControl ()
{
Construct ();
}
#endregion
#region Properties
[EditorBrowsable (EditorBrowsableState.Never)]
protected virtual int AutoHandlers {
get { return 0; }
set { }
}
[EditorBrowsable (EditorBrowsableState.Never)]
protected virtual bool SupportAutoEvents {
get { return true; }
}
#endregion
#region Methods
protected virtual void Construct ()
{
}
[MonoTODO]
protected LiteralControl CreateResourceBasedLiteralControl (int offset,
int size,
bool fAsciiOnly)
{
return null;
}
internal void WireupAutomaticEvents ()
{
if (!SupportAutoEvents || !AutoEventWireup)
return;
Type type = GetType ();
foreach (string methodName in methodNames) {
MethodInfo method = type.GetMethod (methodName, bflags);
if (method == null)
continue;
if (method.DeclaringType != type) {
if (!method.IsPublic && !method.IsFamilyOrAssembly &&
!method.IsFamilyAndAssembly && !method.IsFamily)
continue;
}
if (method.ReturnType != typeof (void))
continue;
ParameterInfo [] parms = method.GetParameters ();
int length = parms.Length;
bool noParams = (length == 0);
if (!noParams && (length != 2 ||
parms [0].ParameterType != typeof (object) ||
parms [1].ParameterType != typeof (EventArgs)))
continue;
int pos = methodName.IndexOf ("_");
string eventName = methodName.Substring (pos + 1);
EventInfo evt = type.GetEvent (eventName);
if (evt == null) {
/* This should never happen */
continue;
}
if (noParams) {
NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
evt.AddEventHandler (this, npi.FakeDelegate);
} else {
evt.AddEventHandler (this, Delegate.CreateDelegate (
typeof (EventHandler), this, methodName));
}
}
}
[EditorBrowsable (EditorBrowsableState.Never)]
protected virtual void FrameworkInitialize ()
{
}
Type GetTypeFromControlPath (string virtualPath)
{
if (virtualPath == null)
throw new ArgumentNullException ("virtualPath");
string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
string realpath = Context.Request.MapPath (vpath);
return UserControlParser.GetCompiledType (vpath, realpath, Context);
}
public Control LoadControl (string virtualPath)
{
#if NET_2_0
if (virtualPath == null)
throw new ArgumentNullException ("virtualPath");
#else
if (virtualPath == null)
throw new HttpException ("virtualPath is null");
#endif
object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
if (control is UserControl)
((UserControl) control).InitializeAsUserControl (Page);
return (Control) control;
}
public ITemplate LoadTemplate (string virtualPath)
{
#if NET_2_0
if (virtualPath == null)
throw new ArgumentNullException ("virtualPath");
#else
if (virtualPath == null)
throw new HttpException ("virtualPath is null");
#endif
Type t = GetTypeFromControlPath (virtualPath);
return new SimpleTemplate (t);
}
protected virtual void OnAbortTransaction (EventArgs e)
{
EventHandler eh = Events [abortTransaction] as EventHandler;
if (eh != null)
eh (this, e);
}
protected virtual void OnCommitTransaction (EventArgs e)
{
EventHandler eh = Events [commitTransaction] as EventHandler;
if (eh != null)
eh (this, e);
}
protected virtual void OnError (EventArgs e)
{
EventHandler eh = Events [error] as EventHandler;
if (eh != null)
eh (this, e);
}
[MonoTODO]
public Control ParseControl (string content)
{
if (content == null)
throw new ArgumentNullException ("content");
return null;
}
[MonoTODO]
[EditorBrowsable (EditorBrowsableState.Never)]
public static object ReadStringResource (Type t)
{
if (t == null)
throw new ArgumentNullException ("t");
return null;
}
[MonoTODO]
[EditorBrowsable (EditorBrowsableState.Never)]
protected void SetStringResourcePointer (object stringResourcePointer,
int maxResourceOffset)
{
}
[MonoTODO]
[EditorBrowsable (EditorBrowsableState.Never)]
protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
int size, bool fAsciiOnly)
{
}
#endregion
#region Events
[WebSysDescription ("Raised when the user aborts a transaction.")]
public event EventHandler AbortTransaction {
add { Events.AddHandler (abortTransaction, value); }
remove { Events.RemoveHandler (abortTransaction, value); }
}
[WebSysDescription ("Raised when the user initiates a transaction.")]
public event EventHandler CommitTransaction {
add { Events.AddHandler (commitTransaction, value); }
remove { Events.RemoveHandler (commitTransaction, value); }
}
[WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
public event EventHandler Error {
add { Events.AddHandler (error, value); }
remove { Events.RemoveHandler (error, value); }
}
#endregion
class SimpleTemplate : ITemplate
{
Type type;
public SimpleTemplate (Type type)
{
this.type = type;
}
public void InstantiateIn (Control control)
{
Control template = Activator.CreateInstance (type) as Control;
template.SetBindingContainer (false);
control.Controls.Add (template);
}
}
#if NET_2_0
Stack dataItemCtx;
internal void PushDataItemContext (object o)
{
if (dataItemCtx == null)
dataItemCtx = new Stack ();
dataItemCtx.Push (o);
}
internal void PopDataItemContext ()
{
if (dataItemCtx == null)
throw new InvalidOperationException ();
dataItemCtx.Pop ();
}
internal object CurrentDataItem {
get {
if (dataItemCtx == null || dataItemCtx.Count == 0)
throw new InvalidOperationException ("No data item");
return dataItemCtx.Peek ();
}
}
protected object Eval (string expression)
{
return DataBinder.Eval (CurrentDataItem, expression);
}
protected object Eval (string expression, string format)
{
return DataBinder.Eval (CurrentDataItem, expression, format);
}
protected object XPath (string xpathexpression)
{
return XPathBinder.Eval (CurrentDataItem, xpathexpression);
}
protected object XPath (string xpathexpression, string format)
{
return XPathBinder.Eval (CurrentDataItem, xpathexpression, format);
}
protected IEnumerable XPathSelect (string xpathexpression)
{
return XPathBinder.Select (CurrentDataItem, xpathexpression);
}
// IFilterResolutionService
[MonoTODO]
int IFilterResolutionService.CompareFilters (string filter1, string filter2)
{
throw new NotImplementedException ();
}
[MonoTODO]
bool IFilterResolutionService.EvaluateFilter (string filterName)
{
throw new NotImplementedException ();
}
#endif
}
}