2 // System.Web.UI.TemplateControl.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
12 using System.Web.Compilation;
13 using System.Web.Util;
15 namespace System.Web.UI {
17 public abstract class TemplateControl : Control, INamingContainer
19 static object abortTransaction = new object ();
20 static object commitTransaction = new object ();
21 static object error = new object ();
24 protected TemplateControl ()
33 protected virtual int AutoHandlers {
38 protected virtual bool SupportAutoEvents {
46 protected virtual void Construct ()
51 protected virtual LiteralControl CreateResourceBasedLiteralControl (int offset,
58 protected virtual void FrameworkInitialize ()
62 Type GetTypeFromControlPath (string virtualPath)
64 if (virtualPath == null)
65 throw new ArgumentNullException ("virtualPath");
67 if (virtualPath [0] == '/')
68 throw new ArgumentException ("Path cannot be rooted", "virtualPath");
70 virtualPath = PathUtil.Combine (TemplateSourceDirectory, virtualPath);
72 return UserControlCompiler.CompileUserControlType (new UserControlParser (virtualPath));
75 public Control LoadControl (string virtualPath)
77 object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
78 if (control is UserControl)
79 ((UserControl) control).InitializeAsUserControl (Page);
81 return (Control) control;
84 public ITemplate LoadTemplate (string virtualPath)
86 Type t = GetTypeFromControlPath (virtualPath);
87 return new SimpleTemplate (t);
90 protected virtual void OnAbortTransaction (EventArgs e)
92 EventHandler eh = Events [error] as EventHandler;
97 protected virtual void OnCommitTransaction (EventArgs e)
99 EventHandler eh = Events [commitTransaction] as EventHandler;
104 protected virtual void OnError (EventArgs e)
106 EventHandler eh = Events [abortTransaction] as EventHandler;
112 public Control ParseControl (string content)
118 public static object ReadStringResource (Type t)
124 protected void SetStringResourcePointer (object stringResourcePointer,
125 int maxResourceOffset)
130 protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
131 int size, bool fAsciiOnly)
139 public event EventHandler AbortTransaction {
140 add { Events.AddHandler (abortTransaction, value); }
141 remove { Events.RemoveHandler (abortTransaction, value); }
144 public event EventHandler CommitTransaction {
145 add { Events.AddHandler (commitTransaction, value); }
146 remove { Events.RemoveHandler (commitTransaction, value); }
149 public event EventHandler Error {
150 add { Events.AddHandler (error, value); }
151 remove { Events.RemoveHandler (error, value); }
156 class SimpleTemplate : ITemplate
160 public SimpleTemplate (Type type)
165 public void InstantiateIn (Control control)
167 Control template = Activator.CreateInstance (type) as Control;
168 template.SetBindingContainer (false);
169 control.Controls.Add (template);