1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
//
// System.Web.UI.TemplateControl.cs
//
// Authors:
// Duncan Mak (duncan@ximian.com)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Andreas Nahr (ClassDevelopment@A-SoftTech.com)
//
// (C) 2002 Ximian, Inc. (http://www.ximian.com)
//
using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.Util;
namespace System.Web.UI {
public abstract class TemplateControl : Control, INamingContainer
{
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.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)
{
object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
if (control is UserControl)
((UserControl) control).InitializeAsUserControl (Page);
return (Control) control;
}
public ITemplate LoadTemplate (string virtualPath)
{
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)
{
return null;
}
[MonoTODO]
[EditorBrowsable (EditorBrowsableState.Never)]
public static object ReadStringResource (Type 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);
}
}
}
}
|