瀏覽代碼

2009-12-22 Marek Habersack <[email protected]>

	* HtmlForm.cs: RenderChildren is slightly more efficient now.

2009-12-22  Marek Habersack  <[email protected]>

	* Page.cs: form javascript declaration block is rendered only if
	necessary. Fixes bug #566541

	* ClientScriptManager.cs: added internal property ScriptsPresent
	used to determine if it is necessary to render form javascript
	declaration block.
	Hidden fields block is output without indenting the controls and
	without rendering trailing empty line.
	Made Write{Begin,End}ScriptBlock slightly faster.

svn path=/trunk/mcs/; revision=148840
Marek Habersack 16 年之前
父節點
當前提交
3d083fe3fe

+ 4 - 0
mcs/class/System.Web/System.Web.UI.HtmlControls/ChangeLog

@@ -1,3 +1,7 @@
+2009-12-22  Marek Habersack  <[email protected]>
+
+	* HtmlForm.cs: RenderChildren is slightly more efficient now.
+
 2009-11-13  Marek Habersack  <[email protected]>
 
 	* HtmlForm.cs: reverted part of the previous patch - action is not

+ 9 - 5
mcs/class/System.Web/System.Web.UI.HtmlControls/HtmlForm.cs

@@ -406,15 +406,19 @@ namespace System.Web.UI.HtmlControls
 #endif		
 		override void RenderChildren (HtmlTextWriter w)
 		{
-			if (!inited) {
-				Page.RegisterViewStateHandler ();
+			Page page = Page;
+			
+			if (!inited && page != null) {
+				page.RegisterViewStateHandler ();
 #if NET_2_0
-				Page.RegisterForm (this);
+				page.RegisterForm (this);
 #endif
 			}
-			Page.OnFormRender (w, ClientID);
+			if (page != null)
+				page.OnFormRender (w, ClientID);
 			base.RenderChildren (w);
-			Page.OnFormPostRender (w, ClientID);
+			if (page != null)
+				page.OnFormPostRender (w, ClientID);
 		}
 
 #if NET_2_0

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

@@ -1,3 +1,15 @@
+2009-12-22  Marek Habersack  <[email protected]>
+
+	* Page.cs: form javascript declaration block is rendered only if
+	necessary. Fixes bug #566541
+
+	* ClientScriptManager.cs: added internal property ScriptsPresent
+	used to determine if it is necessary to render form javascript
+	declaration block.
+	Hidden fields block is output without indenting the controls and
+	without rendering trailing empty line.
+	Made Write{Begin,End}ScriptBlock slightly faster.
+
 2009-12-18  Marek Habersack  <[email protected]>
 
 	* ObjectStateFormatter.cs: ObjectFormatter.WriteObject checks

+ 33 - 13
mcs/class/System.Web/System.Web.UI/ClientScriptManager.cs

@@ -66,6 +66,21 @@ namespace System.Web.UI
 		bool _hasRegisteredForEventValidationOnCallback;
 		bool _pageInRender;
 		bool _initCallBackRegistered;
+		bool _webFormClientScriptRendered;
+		bool _webFormClientScriptRequired;
+		
+		internal bool ScriptsPresent {
+			get {
+				return _webFormClientScriptRequired ||
+					_initCallBackRegistered ||
+					_hasRegisteredForEventValidationOnCallback ||
+					clientScriptBlocks != null ||
+					startupScriptBlocks != null ||
+					submitStatements != null ||
+					registeredArrayDeclares != null ||
+					expandoAttributes != null;
+			}
+		}
 #endif
 		
 		internal ClientScriptManager (Page page)
@@ -181,9 +196,6 @@ namespace System.Web.UI
 			_webFormClientScriptRequired = true;
 		}
 
-		bool _webFormClientScriptRendered;
-		bool _webFormClientScriptRequired;
-
 		internal void WriteWebFormClientScript (HtmlTextWriter writer) {
 			if (!_webFormClientScriptRendered && _webFormClientScriptRequired) {
 				writer.WriteLine ();
@@ -309,6 +321,7 @@ namespace System.Web.UI
 				registeredArrayDeclares.Add (arrayName, new ArrayList());
 	
 			((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
+			page.RequiresFormScriptDeclaration ();
 		}
 
 		void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, bool addScriptTags)
@@ -627,25 +640,22 @@ namespace System.Web.UI
 #if NET_2_0
 		internal const string SCRIPT_BLOCK_START = "//<![CDATA[";
 		internal const string SCRIPT_BLOCK_END = "//]]>";
+		internal const string SCRIPT_ELEMENT_START = @"<script type=""text/javascript"">" + SCRIPT_BLOCK_START;
 #else
 		internal const string SCRIPT_BLOCK_START = "<!--";
 		internal const string SCRIPT_BLOCK_END ="// -->";
+		internal const string SCRIPT_ELEMENT_START = @"<script language=""javascript"" type=""text/javascript"">" + SCRIPT_BLOCK_START;
 #endif
+		internal const string SCRIPT_ELEMENT_END = SCRIPT_BLOCK_END + "</script>";
 		
 		internal static void WriteBeginScriptBlock (HtmlTextWriter writer)
 		{
-			writer.WriteLine ("<script"+
-#if !NET_2_0
-				" language=\"javascript\""+
-#endif
-				" type=\"text/javascript\">");
-			writer.WriteLine (SCRIPT_BLOCK_START);
+			writer.WriteLine (SCRIPT_ELEMENT_START);
 		}
 
 		internal static void WriteEndScriptBlock (HtmlTextWriter writer)
 		{
-			writer.WriteLine (SCRIPT_BLOCK_END);
-			writer.WriteLine ("</script>");
+			writer.WriteLine (SCRIPT_ELEMENT_END);
 		}
 		
 		internal void WriteHiddenFields (HtmlTextWriter writer)
@@ -654,18 +664,28 @@ namespace System.Web.UI
 				return;
 
 #if NET_2_0
+			writer.WriteLine ();
 			writer.RenderBeginTag (HtmlTextWriterTag.Div);
+			int oldIndent = writer.Indent;
+			writer.Indent = 0;
+			bool first = true;
 #endif
 			foreach (string key in hiddenFields.Keys) {
 				string value = hiddenFields [key] as string;
+				if (first)
+					first = false;
+				else
+					writer.WriteLine ();
 #if NET_2_0
-				writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", key, HttpUtility.HtmlAttributeEncode (value));
+				writer.Write ("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", key, HttpUtility.HtmlAttributeEncode (value));
 #else
-				writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, HttpUtility.HtmlAttributeEncode (value));
+				writer.Write ("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, HttpUtility.HtmlAttributeEncode (value));
 #endif
 			}
 #if NET_2_0
+			writer.Indent = oldIndent;
 			writer.RenderEndTag (); // DIV
+			writer.WriteLine ();
 #endif
 			hiddenFields = null;
 		}

+ 18 - 4
mcs/class/System.Web/System.Web.UI/Page.cs

@@ -99,6 +99,8 @@ public partial class Page : TemplateControl, IHttpHandler
 	NameValueCollection secondPostData;
 	bool requiresPostBackScript;
 	bool postBackScriptRendered;
+	bool requiresFormScriptDeclaration;
+	bool formScriptDeclarationRendered;
 	bool handleViewState;
 	string viewStateUserKey;
 	NameValueCollection _requestValueCollection;
@@ -945,6 +947,11 @@ public partial class Page : TemplateControl, IHttpHandler
 		return scriptManager.GetPostBackEventReference (control, argument);
 	}
 
+	internal void RequiresFormScriptDeclaration ()
+	{
+		requiresFormScriptDeclaration = true;
+	}
+	
 	internal void RequiresPostBackScript ()
 	{
 #if NET_2_0
@@ -954,6 +961,7 @@ public partial class Page : TemplateControl, IHttpHandler
 		ClientScript.RegisterHiddenField (postEventArgumentID, String.Empty);
 #endif
 		requiresPostBackScript = true;
+		RequiresFormScriptDeclaration ();
 	}
 
 	[EditorBrowsable (EditorBrowsableState.Never)]
@@ -1147,7 +1155,7 @@ public partial class Page : TemplateControl, IHttpHandler
 		
 		ClientScriptManager.WriteBeginScriptBlock (writer);
 
-#if ONLY_1_1
+#if NET_1_1
 		RenderClientScriptFormDeclaration (writer, formUniqueID);
 #endif
 #if NET_2_0
@@ -1167,6 +1175,9 @@ public partial class Page : TemplateControl, IHttpHandler
 
 	void RenderClientScriptFormDeclaration (HtmlTextWriter writer, string formUniqueID)
 	{
+		if (formScriptDeclarationRendered)
+			return;
+		
 #if NET_2_0
 		if (PageAdapter != null) {
  			writer.WriteLine ("\tvar {0} = {1};\n", theForm, PageAdapter.GetPostBackFormReference(formUniqueID));
@@ -1183,6 +1194,7 @@ public partial class Page : TemplateControl, IHttpHandler
 		writer.WriteLine ("\twindow.TARGET_J2EE = true;");
 		writer.WriteLine ("\twindow.IsMultiForm = {0};", IsMultiForm ? "true" : "false");
 #endif
+		formScriptDeclarationRendered = true;
 	}
 
 	internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
@@ -1194,9 +1206,11 @@ public partial class Page : TemplateControl, IHttpHandler
 		writer.WriteLine ();
 
 #if NET_2_0
-		ClientScriptManager.WriteBeginScriptBlock (writer);
-		RenderClientScriptFormDeclaration (writer, formUniqueID);
-		ClientScriptManager.WriteEndScriptBlock (writer);
+		if (requiresFormScriptDeclaration || (scriptManager != null && scriptManager.ScriptsPresent) || PageAdapter != null) {
+			ClientScriptManager.WriteBeginScriptBlock (writer);
+			RenderClientScriptFormDeclaration (writer, formUniqueID);
+			ClientScriptManager.WriteEndScriptBlock (writer);
+		}
 #endif
 
 		if (handleViewState)