Bladeren bron

2002-12-13 Gonzalo Paniagua Javier <[email protected]>

	* System.Web.Compilation/AspGenerator.cs: added support for
	AutoEventWireup attribute in @Page and @Control.

	* System.Web.Compilation/CompilationResult.cs:
	* System.Web.Compilation/GlobalAsaxCompiler.cs:
	* System.Web.Compilation/PageCompiler.cs:
	* System.Web.Compilation/UserControlCompiler.cs: store the options.

	* System.Web.UI/Control.cs: added AutoEventWireup internal property.

	* System.Web.UI/Page.cs: removed page events wire up from here.

	* System.Web.UI/TemplateControl.cs: new method WireupAutomaticEvents to
	hook up page and user controls events.

	* System.Web.UI/TemplateControlParser.cs: process the options that are
	applicable once we have the instance of the control.

	* System.Web.UI/TemplateParser.cs: also stores the options.

	* System.Web.UI/UserControl.cs: hook up events before initializing the
	control.

svn path=/trunk/mcs/; revision=9623
Gonzalo Paniagua Javier 23 jaren geleden
bovenliggende
commit
31c202887f

+ 14 - 0
mcs/class/System.Web/System.Web.Compilation/AspGenerator.cs

@@ -525,6 +525,20 @@ class AspGenerator
 		if (att ["CompilerOptions"] != null)
 			Options ["CompilerOptions"] = (string) att ["CompilerOptions"];
 
+		if (att ["AutoEventWireup"] != null) {
+			if (options ["AutoEventWireup"] != null)
+				throw new ApplicationException ("Already have an AutoEventWireup attribute");
+			
+			bool autoevent = true;
+			string v = att ["AutoEventWireup"] as string;
+			try {
+				autoevent = Convert.ToBoolean (v);
+			} catch (Exception) {
+				throw new ApplicationException ("'" + v + "' is not a valid value for AutoEventWireup");
+			}
+			options ["AutoEventWireup"] = autoevent;
+		}
+
 		//FIXME: add support for more attributes.
 	}
 

+ 10 - 0
mcs/class/System.Web/System.Web.Compilation/ChangeLog

@@ -1,3 +1,13 @@
+2002-12-13  Gonzalo Paniagua Javier <[email protected]>
+
+	* AspGenerator.cs: added support for AutoEventWireup attribute in
+	@Page and @Control.
+
+	* CompilationResult.cs:
+	* GlobalAsaxCompiler.cs:
+	* PageCompiler.cs:
+	* UserControlCompiler.cs: store the options.
+
 2002-12-12  Gonzalo Paniagua Javier <[email protected]>
 
 	* AspElements.cs: new method Tag.GetElements

+ 11 - 8
mcs/class/System.Web/System.Web.Compilation/CompilationResult.cs

@@ -7,6 +7,7 @@
 // (C) 2002 Ximian, Inc (http://www.ximian.com)
 //
 using System;
+using System.Collections;
 
 namespace System.Web.Compilation
 {
@@ -16,6 +17,7 @@ namespace System.Web.Compilation
 		string output;
 		string outputFile;
 		object data;
+		Hashtable options;
 		
 		public CompilationResult ()
 		{
@@ -35,30 +37,31 @@ namespace System.Web.Compilation
 			data = other.data;
 		}
 		
-		public int ExitCode
-		{
+		public int ExitCode {
 			get { return exitCode; }
 			set { exitCode = exitCode; }
 		}
 		
-		public string CompilerOutput
-		{
+		public string CompilerOutput {
 			get { return output; }
 			set { output = value; }
 		}
 
-		public string OutputFile
-		{
+		public string OutputFile {
 			get { return outputFile; }
 			set { outputFile = value; }
 		}
 
-		public object Data
-		{
+		public object Data {
 			get { return data; }
 			set { data = value; }
 		}
 
+		public Hashtable Options {
+			get { return options; }
+			set { options = value; }
+		}
+
 		public override string ToString ()
 		{
 			return String.Format ("CompilationResult: {0} {1} {2} {3}", exitCode, output, outputFile, data);

+ 1 - 0
mcs/class/System.Web/System.Web.Compilation/GlobalAsaxCompiler.cs

@@ -31,6 +31,7 @@ namespace System.Web.Compilation
 
 			CachingCompiler compiler = new CachingCompiler (this);
 			CompilationResult result = new CompilationResult ();
+			result.Options = options;
 			if (compiler.Compile (result) == false)
 				throw new CompilationException (result);
 				

+ 4 - 1
mcs/class/System.Web/System.Web.Compilation/PageCompiler.cs

@@ -32,6 +32,7 @@ namespace System.Web.Compilation
 
 			CachingCompiler compiler = new CachingCompiler (this);
 			CompilationResult result = new CompilationResult ();
+			result.Options = options;
 			if (compiler.Compile (result) == false)
 				throw new CompilationException (result);
 				
@@ -64,8 +65,10 @@ namespace System.Web.Compilation
 		{
 			CompilationCacheItem item = CachingCompiler.GetCached (pageParser.InputFile);
 			if (item != null && item.Result != null) {
-				if (item.Result != null)
+				if (item.Result != null) {
+					pageParser.Options = item.Result.Options;
 					return item.Result.Data as Type;
+				}
 
 				throw new CompilationException (item.Result);
 			}

+ 4 - 1
mcs/class/System.Web/System.Web.Compilation/UserControlCompiler.cs

@@ -32,6 +32,7 @@ namespace System.Web.Compilation
 
 			CachingCompiler compiler = new CachingCompiler (this);
 			CompilationResult result = new CompilationResult ();
+			result.Options = options;
 			if (compiler.Compile (result) == false)
 				throw new CompilationException (result);
 				
@@ -78,8 +79,10 @@ namespace System.Web.Compilation
 		{
 			CompilationCacheItem item = CachingCompiler.GetCached (userControlParser.InputFile);
 			if (item != null && item.Result != null) {
-				if (item.Result != null)
+				if (item.Result != null) {
+					userControlParser.Options = item.Result.Options;
 					return item.Result.Data as Type;
+				}
 
 				throw new CompilationException (item.Result);
 			}

+ 17 - 0
mcs/class/System.Web/System.Web.UI/ChangeLog

@@ -1,3 +1,20 @@
+2002-12-13  Gonzalo Paniagua Javier <[email protected]>
+
+	* Control.cs: added AutoEventWireup internal property.
+
+	* Page.cs: removed page events wire up from here.
+
+	* TemplateControl.cs: new method WireupAutomaticEvents to hook up page
+	and user controls events.
+
+	* TemplateControlParser.cs: process the options that are applicable
+	once we have the instance of the control.
+
+	* TemplateParser.cs: also stores the options.
+
+	* UserControl.cs: hook up events before initializing the control.
+
+
 2002-12-12  Gonzalo Paniagua Javier <[email protected]>
 
 	* Control.cs: new method to set bindingContainer value.

+ 7 - 0
mcs/class/System.Web/System.Web.UI/Control.cs

@@ -116,6 +116,7 @@ namespace System.Web.UI
 		private bool autoID = true;
 		private bool creatingControls = false;
 		private bool bindingContainer = true;
+		private bool autoEventWireup = true;
         	
         	    private DataBindingCollection dataBindings = null;
 
@@ -320,6 +321,7 @@ namespace System.Web.UI
                         	return _viewState;
                         }
                 }
+
                 protected virtual bool ViewStateIgnoresCase
                 {
                         get {
@@ -327,6 +329,11 @@ namespace System.Web.UI
                         }
                 }
 
+		internal bool AutoEventWireup {
+			get { return autoEventWireup; }
+			set { autoEventWireup = value; }
+		}
+
 		internal void SetBindingContainer (bool isBC)
 		{
 			bindingContainer = isBC;

+ 2 - 85
mcs/class/System.Web/System.Web.UI/Page.cs

@@ -12,7 +12,6 @@ using System;
 using System.Collections;
 using System.Collections.Specialized;
 using System.IO;
-using System.Reflection;
 using System.Security.Principal;
 using System.Text;
 using System.Web;
@@ -354,47 +353,6 @@ public class Page : TemplateControl, IHttpHandler
 		throw new NotImplementedException ();
 	}
 	
-	MethodInfo [] autoEventsMethods = null;
-
-	private void InvokeEventMethod (string m_name, object sender, EventArgs e)
-	{
-		if (autoEventsMethods == null) {
-			BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
-					  BindingFlags.DeclaredOnly | BindingFlags.Instance;
-			
-			MethodInfo [] m1 = GetType ().GetMethods (bf);
-			bf = BindingFlags.Public | BindingFlags.NonPublic |
-			     BindingFlags.DeclaredOnly | BindingFlags.Static;
-
-			MethodInfo [] m2 = GetType ().GetMethods (bf);
-			autoEventsMethods = new MethodInfo [m1.Length + m2.Length];
-			m1.CopyTo (autoEventsMethods, 0);
-			m2.CopyTo (autoEventsMethods, m1.Length);
-		}
-
-		foreach (MethodInfo m in autoEventsMethods) {
-			if (m.ReturnType != typeof (void))
-				continue;
-
-			if (m.Name != m_name)
-				continue;
-
-			ParameterInfo [] pi = m.GetParameters ();
-			if (pi.Length != 2)
-				continue;
-
-			if (pi [0].ParameterType != typeof (object) ||
-			    pi [1].ParameterType != typeof (EventArgs))
-				continue;
-
-			object [] parms = new object [2];
-			parms [0] = sender;
-			parms [1] = e;
-			m.Invoke (this, parms);
-			break;
-		}
-	}
-
 	private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
 	{
 		writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTTARGET\" value=\"\" />");
@@ -444,37 +402,6 @@ public class Page : TemplateControl, IHttpHandler
 		postBackScriptRendered = false;
 	}
 
-
-	private void _Page_Init (object sender, EventArgs e)
-	{
-		InvokeEventMethod ("Page_Init", sender, e);
-	}
-
-	private void _Page_Load (object sender, EventArgs e)
-	{
-		InvokeEventMethod ("Page_Load", sender, e);
-	}
-
-	private void _Page_DataBind (object sender, EventArgs e)
-	{
-		InvokeEventMethod ("Page_DataBind", sender, e);
-	}
-
-	private void _Page_PreRender (object sender, EventArgs e)
-	{
-		InvokeEventMethod ("Page_PreRender", sender, e);
-	}
-
-	private void _Page_Dispose (object sender, EventArgs e)
-	{
-		InvokeEventMethod ("Page_Dispose", sender, e);
-	}
-
-	private void _Page_Error (object sender, EventArgs e)
-	{
-		InvokeEventMethod ("Page_Error", sender, e);
-	}
-
 	private void ProcessPostData (NameValueCollection data, bool second)
 	{
 		if (data == null)
@@ -508,23 +435,13 @@ public class Page : TemplateControl, IHttpHandler
 		}
 	}
 
-	private bool init_done;
 	public void ProcessRequest (HttpContext context)
 	{
 		_context = context;
 		WebTrace.PushContext ("Page.ProcessRequest ()");
 		WebTrace.WriteLine ("Entering");
-		if (!init_done){
-			init_done = true;
-			// These should depend on AutoEventWireUp in Page directive. Defaults to true.
-			Init += new EventHandler (_Page_Init);
-			Load += new EventHandler (_Page_Load);
-			DataBinding += new EventHandler (_Page_DataBind);
-			PreRender += new EventHandler (_Page_PreRender);
-			Disposed += new EventHandler (_Page_Dispose);
-			Error += new EventHandler (_Page_Error);
-			WebTrace.WriteLine ("Finished init");
-		}
+		WireupAutomaticEvents ();
+		WebTrace.WriteLine ("Finished hookup");
 		//-- Control execution lifecycle in the docs
 		WebTrace.WriteLine ("Controls.Clear");
 		Controls.Clear ();

+ 48 - 0
mcs/class/System.Web/System.Web.UI/TemplateControl.cs

@@ -9,6 +9,8 @@
 //
 
 using System;
+using System.Collections;
+using System.Reflection;
 using System.Web.Compilation;
 using System.Web.Util;
 
@@ -19,6 +21,18 @@ namespace System.Web.UI {
 		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_Dispose",
+						 "Page_Error" };
+
+		const BindingFlags bflags = BindingFlags.Public |
+					    BindingFlags.NonPublic |
+					    BindingFlags.DeclaredOnly |
+					    BindingFlags.Static |
+					    BindingFlags.Instance;
 
 		#region Constructor
 		protected TemplateControl ()
@@ -55,6 +69,40 @@ namespace System.Web.UI {
 			return null;
 		}
 
+		internal void WireupAutomaticEvents ()
+		{
+			if (!SupportAutoEvents || !AutoEventWireup)
+				return;
+
+			Type type = GetType ();
+			foreach (MethodInfo method in type.GetMethods (bflags)) {
+				int pos = Array.IndexOf (methodNames, method.Name);
+				if (pos == -1)
+					continue;
+
+				string name = methodNames [pos];
+				pos = name.IndexOf ("_");
+				if (pos == -1 || pos + 1 == name.Length)
+					continue;
+
+				if (method.ReturnType != typeof (void))
+					continue;
+
+				ParameterInfo [] parms = method.GetParameters ();
+				if (parms.Length != 2 ||
+				    parms [0].ParameterType != typeof (object) ||
+				    parms [1].ParameterType != typeof (EventArgs))
+				    continue;
+
+				string eventName = name.Substring (pos + 1);
+				EventInfo evt = type.GetEvent (eventName);
+				if (evt == null)
+					continue;
+
+				evt.AddEventHandler (this, Delegate.CreateDelegate (typeof (EventHandler), method));
+			}
+		}
+
 		protected virtual void FrameworkInitialize ()
 		{
 		}

+ 18 - 1
mcs/class/System.Web/System.Web.UI/TemplateControlParser.cs

@@ -7,6 +7,7 @@
 // (C) 2002 Ximian, Inc (http://www.ximian.com)
 //
 using System;
+using System.Collections;
 using System.IO;
 using System.Web.Compilation;
 
@@ -21,7 +22,23 @@ namespace System.Web.UI
 			if (type == null)
 				return null;
 
-			return Activator.CreateInstance (type);
+			object ctrl = Activator.CreateInstance (type);
+			if (ctrl == null)
+				return null;
+
+			HandleOptions (ctrl);
+			return ctrl;
+		}
+
+		protected override void HandleOptions (object obj)
+		{
+			Control ctrl = obj as Control;
+			Hashtable options = Options;
+			if (options == null)
+				return;
+
+			if (options ["AutoEventWireup"] != null)
+				ctrl.AutoEventWireup = (bool) options ["AutoEventWireup"];
 		}
 	}
 }

+ 13 - 2
mcs/class/System.Web/System.Web.UI/TemplateParser.cs

@@ -8,17 +8,23 @@
 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
 //
 using System;
+using System.Collections;
 using System.Web;
 
 namespace System.Web.UI
 {
 	public abstract class TemplateParser : BaseParser
 	{
-		private string inputFile;
-		private string text;
+		string inputFile;
+		string text;
+		Hashtable options;
 
 		protected abstract Type CompileIntoType ();
 
+		protected virtual void HandleOptions (object obj)
+		{
+		}
+
 		protected abstract Type DefaultBaseType { get; }
 
 		protected abstract string DefaultDirectiveName { get; }
@@ -39,6 +45,11 @@ namespace System.Web.UI
 		{
 			get { return DefaultBaseType; }
 		}
+		
+		internal Hashtable Options {
+			get { return options; }
+			set { options = value; }
+		}
 	}
 }
 

+ 1 - 0
mcs/class/System.Web/System.Web.UI/UserControl.cs

@@ -131,6 +131,7 @@ namespace System.Web.UI
 			if (initialized)
 				return;
 			initialized = true;
+			WireupAutomaticEvents ();
 			FrameworkInitialize ();
 		}