5 // Igor Zelmanovich <igorz@mainsoft.com>
7 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
33 using System.ComponentModel;
34 using System.Security.Permissions;
35 using System.Collections.Specialized;
36 using System.Collections;
37 using System.Web.Handlers;
38 using System.Reflection;
39 using System.Web.Configuration;
40 using System.Web.UI.HtmlControls;
42 using System.Globalization;
43 using System.Threading;
44 using System.Web.Script.Serialization;
45 using System.Web.Script.Services;
47 using System.Collections.ObjectModel;
49 namespace System.Web.UI
51 [ParseChildrenAttribute (true)]
52 [DefaultPropertyAttribute ("Scripts")]
53 [DesignerAttribute ("System.Web.UI.Design.ScriptManagerDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
54 [NonVisualControlAttribute]
55 [PersistChildrenAttribute (false)]
56 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
57 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
58 public class ScriptManager : Control, IPostBackDataHandler, IScriptManager
60 // the keywords are used in fomatting async response
61 const string updatePanel = "updatePanel";
62 const string hiddenField = "hiddenField";
63 const string arrayDeclaration = "arrayDeclaration";
64 const string scriptBlock = "scriptBlock";
65 const string scriptStartupBlock = "scriptStartupBlock";
66 const string expando = "expando";
67 const string onSubmit = "onSubmit";
68 const string asyncPostBackControlIDs = "asyncPostBackControlIDs";
69 const string postBackControlIDs = "postBackControlIDs";
70 const string updatePanelIDs = "updatePanelIDs";
71 const string asyncPostBackTimeout = "asyncPostBackTimeout";
72 const string childUpdatePanelIDs = "childUpdatePanelIDs";
73 const string panelsToRefreshIDs = "panelsToRefreshIDs";
74 const string formAction = "formAction";
75 const string dataItem = "dataItem";
76 const string dataItemJson = "dataItemJson";
77 const string scriptDispose = "scriptDispose";
78 const string pageRedirect = "pageRedirect";
79 const string error = "error";
80 const string pageTitle = "pageTitle";
81 const string focus = "focus";
82 const string scriptContentNoTags = "ScriptContentNoTags";
83 const string scriptContentWithTags = "ScriptContentWithTags";
84 const string scriptPath = "ScriptPath";
86 static readonly object ScriptManagerKey = typeof (IScriptManager);
88 int _asyncPostBackTimeout = 90;
89 List<Control> _asyncPostBackControls;
90 List<Control> _postBackControls;
91 List<UpdatePanel> _childUpdatePanels;
92 List<UpdatePanel> _panelsToRefresh;
93 List<UpdatePanel> _updatePanels;
94 ScriptReferenceCollection _scripts;
95 ServiceReferenceCollection _services;
96 bool _isInAsyncPostBack;
97 bool _isInPartialRendering;
98 string _asyncPostBackSourceElementID;
99 ScriptMode _scriptMode = ScriptMode.Auto;
100 bool _enableScriptGlobalization;
101 bool _enableScriptLocalization;
103 List<RegisteredScript> _clientScriptBlocks;
104 List<RegisteredScript> _startupScriptBlocks;
105 List<RegisteredScript> _onSubmitStatements;
106 List<RegisteredArrayDeclaration> _arrayDeclarations;
107 List<RegisteredExpandoAttribute> _expandoAttributes;
108 List<RegisteredHiddenField> _hiddenFields;
109 List<IScriptControl> _registeredScriptControls;
110 Dictionary<IExtenderControl, Control> _registeredExtenderControls;
111 bool? _supportsPartialRendering;
112 bool _enablePartialRendering = true;
114 string _panelToRefreshID;
115 Dictionary<Control, DataItemEntry> _dataItems;
116 bool _enablePageMethods;
117 string _controlIDToFocus;
118 bool _allowCustomErrorsRedirect = true;
119 string _asyncPostBackErrorMessage;
120 List<RegisteredDisposeScript> _disposeScripts;
121 List<ScriptReferenceEntry> _scriptToRegister;
122 bool _loadScriptsBeforeUI = true;
123 AuthenticationServiceManager _authenticationService;
124 ProfileServiceManager _profileService;
125 List<ScriptManagerProxy> _proxies;
127 [DefaultValue (true)]
128 [Category ("Behavior")]
129 public bool AllowCustomErrorsRedirect {
131 return _allowCustomErrorsRedirect;
134 _allowCustomErrorsRedirect = value;
138 [Category ("Behavior")]
140 public string AsyncPostBackErrorMessage {
142 if (String.IsNullOrEmpty (_asyncPostBackErrorMessage))
144 return _asyncPostBackErrorMessage;
147 _asyncPostBackErrorMessage = value;
152 public string AsyncPostBackSourceElementID {
154 if (_asyncPostBackSourceElementID == null)
156 return _asyncPostBackSourceElementID;
161 [Category ("Behavior")]
162 public int AsyncPostBackTimeout {
164 return _asyncPostBackTimeout;
167 _asyncPostBackTimeout = value;
171 [Category ("Behavior")]
172 [MergableProperty (false)]
174 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
175 [PersistenceMode (PersistenceMode.InnerProperty)]
176 public AuthenticationServiceManager AuthenticationService {
178 if (_authenticationService == null)
179 _authenticationService = new AuthenticationServiceManager ();
180 return _authenticationService;
184 [Category ("Behavior")]
185 [DefaultValue (false)]
186 public bool EnablePageMethods {
188 return _enablePageMethods;
191 _enablePageMethods = value;
195 [DefaultValue (true)]
196 [Category ("Behavior")]
197 public bool EnablePartialRendering {
199 return _enablePartialRendering;
203 throw new InvalidOperationException ();
204 _enablePartialRendering = value;
208 [DefaultValue (false)]
209 [Category ("Behavior")]
210 public bool EnableScriptGlobalization {
212 return _enableScriptGlobalization;
215 _enableScriptGlobalization = value;
219 [Category ("Behavior")]
220 [DefaultValue (false)]
221 public bool EnableScriptLocalization {
223 return _enableScriptLocalization;
226 _enableScriptLocalization = value;
231 public bool IsDebuggingEnabled {
233 if (IsDeploymentRetail)
236 CompilationSection compilation = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
237 if (!compilation.Debug && (ScriptMode == ScriptMode.Auto || ScriptMode == ScriptMode.Inherit))
240 if (ScriptMode == ScriptMode.Release)
247 bool IsDeploymentRetail {
252 DeploymentSection deployment = (DeploymentSection) WebConfigurationManager.GetSection ("system.web/deployment");
253 return deployment.Retail;
259 public bool IsInAsyncPostBack {
261 return _isInAsyncPostBack;
265 internal bool IsInPartialRendering {
267 return _isInPartialRendering;
270 _isInPartialRendering = value;
274 [Category ("Behavior")]
275 [DefaultValue (true)]
276 public bool LoadScriptsBeforeUI {
278 return _loadScriptsBeforeUI;
281 _loadScriptsBeforeUI = value;
285 [PersistenceMode (PersistenceMode.InnerProperty)]
287 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
288 [Category ("Behavior")]
289 [MergableProperty (false)]
290 public ProfileServiceManager ProfileService {
292 if (_profileService == null)
293 _profileService = new ProfileServiceManager ();
294 return _profileService;
298 [Category ("Behavior")]
300 [MonoLimitation ("The 'Auto' value is the same as 'Debug'.")]
302 public ScriptMode ScriptMode {
307 if (value == ScriptMode.Inherit)
308 value = ScriptMode.Auto;
314 [Category ("Behavior")]
315 public string ScriptPath {
317 if (_scriptPath == null)
326 [PersistenceMode (PersistenceMode.InnerProperty)]
328 [Category ("Behavior")]
329 [MergableProperty (false)]
330 public ScriptReferenceCollection Scripts {
332 if (_scripts == null)
333 _scripts = new ScriptReferenceCollection ();
339 [PersistenceMode (PersistenceMode.InnerProperty)]
341 [MergableProperty (false)]
342 [Category ("Behavior")]
343 public ServiceReferenceCollection Services {
345 if (_services == null)
346 _services = new ServiceReferenceCollection ();
352 [DefaultValue (true)]
354 public bool SupportsPartialRendering {
356 if (!_supportsPartialRendering.HasValue)
357 _supportsPartialRendering = CheckSupportsPartialRendering ();
358 return _supportsPartialRendering.Value;
362 throw new InvalidOperationException ();
363 if (!EnablePartialRendering && value)
364 throw new InvalidOperationException ("The SupportsPartialRendering property cannot be set when EnablePartialRendering is false.");
366 _supportsPartialRendering = value;
370 bool CheckSupportsPartialRendering () {
371 if (!EnablePartialRendering)
373 // TODO: consider browser capabilities
377 [EditorBrowsable (EditorBrowsableState.Never)]
379 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
380 public override bool Visible {
385 throw new NotImplementedException ();
389 [Category ("Action")]
390 public event EventHandler<AsyncPostBackErrorEventArgs> AsyncPostBackError;
392 [Category ("Action")]
393 public event EventHandler<ScriptReferenceEventArgs> ResolveScriptReference;
395 public static ScriptManager GetCurrent (Page page) {
397 throw new ArgumentNullException ("page");
398 return (ScriptManager) page.Items [ScriptManagerKey];
401 static void SetCurrent (Page page, ScriptManager instance) {
402 page.Items [ScriptManagerKey] = instance;
405 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection) {
406 _isInAsyncPostBack = true;
407 string arg = postCollection [postDataKey];
408 if (!String.IsNullOrEmpty (arg)) {
409 string [] args = arg.Split ('|');
410 _panelToRefreshID = args [0];
411 _asyncPostBackSourceElementID = args [1];
417 protected internal virtual void OnAsyncPostBackError (AsyncPostBackErrorEventArgs e) {
418 if (AsyncPostBackError != null)
419 AsyncPostBackError (this, e);
422 protected override void OnInit (EventArgs e) {
425 if (GetCurrent (Page) != null)
426 throw new InvalidOperationException ("Only one instance of a ScriptManager can be added to the page.");
428 SetCurrent (Page, this);
429 Page.Error += new EventHandler (OnPageError);
433 void OnPageError (object sender, EventArgs e) {
434 if (IsInAsyncPostBack)
435 OnAsyncPostBackError (new AsyncPostBackErrorEventArgs (Context.Error));
438 protected override void OnPreRender (EventArgs e) {
439 base.OnPreRender (e);
441 Page.PreRenderComplete += new EventHandler (OnPreRenderComplete);
443 if (IsInAsyncPostBack) {
444 Page.SetRenderMethodDelegate (RenderPageCallback);
447 if (EnableScriptGlobalization) {
448 CultureInfo culture = Thread.CurrentThread.CurrentCulture;
449 string script = String.Format ("var __cultureInfo = '{0}';", JavaScriptSerializer.DefaultSerializer.Serialize (new CultureInfoSerializer (culture)));
450 RegisterClientScriptBlock (this, typeof (ScriptManager), "ScriptGlobalization", script, true);
453 // Register dispose script
454 if (_disposeScripts != null && _disposeScripts.Count > 0) {
455 StringBuilder sb = new StringBuilder ();
457 for (int i = 0; i < _disposeScripts.Count; i++) {
458 RegisteredDisposeScript entry = _disposeScripts [i];
460 sb.Append ("Sys.WebForms.PageRequestManager.getInstance($get(\"" + Page.Form.ClientID + "\"))._registerDisposeScript(\"");
462 sb.Append ("Sys.WebForms.PageRequestManager.getInstance()._registerDisposeScript(\"");
463 sb.Append (entry.UpdatePanel.ClientID);
465 sb.Append (JavaScriptSerializer.DefaultSerializer.Serialize (entry.Script)); //JavaScriptSerializer.Serialize used escape script literal
466 sb.AppendLine (");");
468 RegisterStartupScript (this, typeof (ExtenderControl), "disposeScripts;", sb.ToString (), true);
472 // to cause webform client script being included
473 Page.ClientScript.GetPostBackEventReference (new PostBackOptions (this, null, null, false, false, false, true, true, null));
475 Page.ClientScript.GetPostBackEventReference (this, null);
480 void OnPreRenderComplete (object sender, EventArgs e) {
482 ScriptReference ajaxScript = new ScriptReference ("MicrosoftAjax.js", String.Empty);
483 ajaxScript.NotifyScriptLoaded = false;
484 OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxScript));
486 ScriptReference ajaxWebFormsScript = new ScriptReference ("MicrosoftAjaxWebForms.js", String.Empty);
487 ajaxWebFormsScript.NotifyScriptLoaded = false;
488 OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxWebFormsScript));
490 ScriptReference ajaxExtensionScript = null;
491 ScriptReference ajaxWebFormsExtensionScript = null;
493 ajaxExtensionScript = new ScriptReference ("MicrosoftAjaxExtension.js", String.Empty);
494 OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxExtensionScript));
496 ajaxWebFormsExtensionScript = new ScriptReference ("MicrosoftAjaxWebFormsExtension.js", String.Empty);
497 OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxWebFormsExtensionScript));
500 foreach (ScriptReferenceEntry script in GetScriptReferences ()) {
501 OnResolveScriptReference (new ScriptReferenceEventArgs (script.ScriptReference));
502 if (_scriptToRegister == null)
503 _scriptToRegister = new List<ScriptReferenceEntry> ();
504 _scriptToRegister.Add (script);
507 if (!IsInAsyncPostBack) {
508 // Register Ajax framework script.
509 RegisterScriptReference (this, ajaxScript, true);
511 RegisterScriptReference (this, ajaxExtensionScript, true);
512 RegisterClientScriptBlock (this, typeof (ScriptManager), "Sys.Application", "\nSys.Application._initialize(document.getElementById('" + Page.Form.ClientID + "'));\n", true);
515 StringBuilder sb = new StringBuilder ();
516 sb.AppendLine ("if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');");
518 ScriptingProfileServiceSection profileService = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
519 if (profileService.Enabled)
520 sb.AppendLine ("Sys.Services._ProfileService.DefaultWebServicePath = '" + ResolveClientUrl ("~" + System.Web.Script.Services.ProfileService.DefaultWebServicePath) + "';");
521 string profileServicePath = GetProfileServicePath ();
522 if (!String.IsNullOrEmpty (profileServicePath))
523 sb.AppendLine ("Sys.Services.ProfileService.set_path('" + profileServicePath + "');");
525 ScriptingAuthenticationServiceSection authenticationService = (ScriptingAuthenticationServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/authenticationService");
526 if (authenticationService.Enabled) {
527 sb.AppendLine ("Sys.Services._AuthenticationService.DefaultWebServicePath = '" + ResolveClientUrl ("~/Authentication_JSON_AppService.axd") + "';");
528 if (Page.User.Identity.IsAuthenticated)
529 sb.AppendLine ("Sys.Services.AuthenticationService._setAuthenticated(true);");
531 string authenticationServicePath = GetAuthenticationServicePath ();
532 if (!String.IsNullOrEmpty (authenticationServicePath))
533 sb.AppendLine ("Sys.Services.AuthenticationService.set_path('" + authenticationServicePath + "');");
535 RegisterClientScriptBlock (this, typeof (ScriptManager), "Framework", sb.ToString (), true);
537 RegisterScriptReference (this, ajaxWebFormsScript, true);
540 RegisterScriptReference (this, ajaxWebFormsExtensionScript, true);
544 if (_scriptToRegister != null)
545 for (int i = 0; i < _scriptToRegister.Count; i++)
546 RegisterScriptReference (_scriptToRegister [i].Control, _scriptToRegister [i].ScriptReference, _scriptToRegister [i].LoadScriptsBeforeUI);
548 if (!IsInAsyncPostBack) {
550 if (_services != null && _services.Count > 0) {
551 for (int i = 0; i < _services.Count; i++) {
552 RegisterServiceReference (this, _services [i]);
556 if (_proxies != null && _proxies.Count > 0) {
557 for (int i = 0; i < _proxies.Count; i++) {
558 ScriptManagerProxy proxy = _proxies [i];
559 for (int j = 0; j < proxy.Services.Count; j++) {
560 RegisterServiceReference (proxy, proxy.Services [j]);
565 if (EnablePageMethods) {
566 LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (Page.GetType (), Page.Request.FilePath);
567 RegisterClientScriptBlock (this, typeof (ScriptManager), "PageMethods", logicalTypeInfo.Proxy, true);
570 // Register startup script
572 RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", "Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).initialize();\n", true);
574 RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", "Sys.Application.initialize();\n", true);
578 string GetProfileServicePath () {
579 if (_profileService != null && !String.IsNullOrEmpty (_profileService.Path))
580 return ResolveClientUrl (_profileService.Path);
582 if (_proxies != null && _proxies.Count > 0)
583 for (int i = 0; i < _proxies.Count; i++)
584 if (!String.IsNullOrEmpty (_proxies [i].ProfileService.Path))
585 return _proxies [i].ResolveClientUrl (_proxies [i].ProfileService.Path);
589 string GetAuthenticationServicePath () {
590 if (_authenticationService != null && !String.IsNullOrEmpty (_authenticationService.Path))
591 return ResolveClientUrl (_authenticationService.Path);
593 if (_proxies != null && _proxies.Count > 0)
594 for (int i = 0; i < _proxies.Count; i++)
595 if (!String.IsNullOrEmpty (_proxies [i].AuthenticationService.Path))
596 return _proxies [i].ResolveClientUrl (_proxies [i].AuthenticationService.Path);
600 public ReadOnlyCollection<RegisteredArrayDeclaration> GetRegisteredArrayDeclarations () {
601 if (_arrayDeclarations == null)
602 _arrayDeclarations = new List<RegisteredArrayDeclaration> ();
603 return new ReadOnlyCollection<RegisteredArrayDeclaration> (_arrayDeclarations);
606 public ReadOnlyCollection<RegisteredScript> GetRegisteredClientScriptBlocks () {
607 if (_clientScriptBlocks == null)
608 _clientScriptBlocks = new List<RegisteredScript> ();
609 return new ReadOnlyCollection<RegisteredScript> (_clientScriptBlocks);
612 public ReadOnlyCollection<RegisteredDisposeScript> GetRegisteredDisposeScripts () {
613 if (_disposeScripts == null)
614 _disposeScripts = new List<RegisteredDisposeScript> ();
615 return new ReadOnlyCollection<RegisteredDisposeScript> (_disposeScripts);
618 public ReadOnlyCollection<RegisteredExpandoAttribute> GetRegisteredExpandoAttributes () {
619 if (_expandoAttributes == null)
620 _expandoAttributes = new List<RegisteredExpandoAttribute> ();
621 return new ReadOnlyCollection<RegisteredExpandoAttribute> (_expandoAttributes);
624 public ReadOnlyCollection<RegisteredHiddenField> GetRegisteredHiddenFields () {
625 if (_hiddenFields == null)
626 _hiddenFields = new List<RegisteredHiddenField> ();
627 return new ReadOnlyCollection<RegisteredHiddenField> (_hiddenFields);
630 public ReadOnlyCollection<RegisteredScript> GetRegisteredOnSubmitStatements () {
631 if (_onSubmitStatements == null)
632 _onSubmitStatements = new List<RegisteredScript> ();
633 return new ReadOnlyCollection<RegisteredScript> (_onSubmitStatements);
636 public ReadOnlyCollection<RegisteredScript> GetRegisteredStartupScripts () {
637 if (_startupScriptBlocks == null)
638 _startupScriptBlocks = new List<RegisteredScript> ();
639 return new ReadOnlyCollection<RegisteredScript> (_startupScriptBlocks);
643 bool _isMultiForm = false;
644 bool _isMultiFormInited = false;
648 if (!_isMultiFormInited) {
649 string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
650 _isMultiForm = isMultiForm != null ? Boolean.Parse (isMultiForm) : false;
652 _isMultiFormInited = true;
659 get { return false; }
663 static bool HasBeenRendered (Control control) {
667 if (control is UpdatePanel && ((UpdatePanel) control).RequiresUpdate)
670 return HasBeenRendered (control.Parent);
673 IEnumerable<ScriptReferenceEntry> GetScriptReferences () {
674 if (_scripts != null && _scripts.Count > 0) {
675 for (int i = 0; i < _scripts.Count; i++) {
676 yield return new ScriptReferenceEntry (this, _scripts [i], LoadScriptsBeforeUI);
680 if (_proxies != null && _proxies.Count > 0) {
681 for (int i = 0; i < _proxies.Count; i++) {
682 ScriptManagerProxy proxy = _proxies [i];
683 for (int j = 0; j < proxy.Scripts.Count; j++)
684 yield return new ScriptReferenceEntry (proxy, proxy.Scripts [j], LoadScriptsBeforeUI);
688 if (_registeredScriptControls != null && _registeredScriptControls.Count > 0) {
689 for (int i = 0; i < _registeredScriptControls.Count; i++) {
690 IEnumerable<ScriptReference> scripts = _registeredScriptControls [i].GetScriptReferences ();
692 foreach (ScriptReference s in scripts)
693 yield return new ScriptReferenceEntry ((Control) _registeredScriptControls [i], s, LoadScriptsBeforeUI);
697 if (_registeredExtenderControls != null && _registeredExtenderControls.Count > 0) {
698 foreach (IExtenderControl ex in _registeredExtenderControls.Keys) {
699 IEnumerable<ScriptReference> scripts = ex.GetScriptReferences ();
701 foreach (ScriptReference s in scripts)
702 yield return new ScriptReferenceEntry ((Control) ex, s, LoadScriptsBeforeUI);
707 protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e) {
708 if (ResolveScriptReference != null)
709 ResolveScriptReference (this, e);
712 protected virtual void RaisePostDataChangedEvent () {
713 UpdatePanel up = Page.FindControl (_panelToRefreshID) as UpdatePanel;
714 if (up != null && up.ChildrenAsTriggers)
718 public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue) {
719 RegisterArrayDeclaration ((Control) page, arrayName, arrayValue);
722 public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue) {
723 Page page = control.Page;
724 ScriptManager sm = GetCurrent (page);
726 if (sm._arrayDeclarations == null)
727 sm._arrayDeclarations = new List<RegisteredArrayDeclaration> ();
729 sm._arrayDeclarations.Add (new RegisteredArrayDeclaration (control, arrayName, arrayValue));
731 if (!sm.IsInAsyncPostBack)
732 page.ClientScript.RegisterArrayDeclaration (arrayName, arrayValue);
735 public void RegisterAsyncPostBackControl (Control control) {
739 if (_asyncPostBackControls == null)
740 _asyncPostBackControls = new List<Control> ();
742 if (_asyncPostBackControls.Contains (control))
745 _asyncPostBackControls.Add (control);
748 public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags) {
749 RegisterClientScriptBlock ((Control) page, type, key, script, addScriptTags);
752 public static void RegisterClientScriptBlock (Control control, Type type, string key, string script, bool addScriptTags) {
753 Page page = control.Page;
754 ScriptManager sm = GetCurrent (page);
756 RegisterScript (ref sm._clientScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientScriptBlock);
758 if (!sm.IsInAsyncPostBack)
759 page.ClientScript.RegisterClientScriptBlock (type, key, script, addScriptTags);
762 public static void RegisterClientScriptInclude (Page page, Type type, string key, string url) {
763 RegisterClientScriptInclude ((Control) page, type, key, url);
766 public static void RegisterClientScriptInclude (Control control, Type type, string key, string url) {
767 Page page = control.Page;
768 ScriptManager sm = GetCurrent (page);
770 RegisterScript (ref sm._clientScriptBlocks, control, type, key, null, url, false, RegisteredScriptType.ClientScriptInclude);
772 if (!sm.IsInAsyncPostBack)
773 page.ClientScript.RegisterClientScriptInclude (type, key, url);
776 public static void RegisterClientScriptResource (Page page, Type type, string resourceName) {
777 RegisterClientScriptResource ((Control) page, type, resourceName);
780 public static void RegisterClientScriptResource (Control control, Type type, string resourceName) {
781 RegisterClientScriptInclude (control, type, resourceName, ScriptResourceHandler.GetResourceUrl (type.Assembly, resourceName, true));
784 void RegisterScriptReference (Control control, ScriptReference script, bool loadScriptsBeforeUI) {
786 bool isDebugMode = IsDeploymentRetail ? false : (script.ScriptModeInternal == ScriptMode.Inherit ? IsDebuggingEnabled : (script.ScriptModeInternal == ScriptMode.Debug));
788 if (!String.IsNullOrEmpty (script.Path)) {
789 url = GetScriptName (control.ResolveClientUrl (script.Path), isDebugMode, EnableScriptLocalization ? script.ResourceUICultures : null);
791 else if (!String.IsNullOrEmpty (script.Name)) {
793 if (String.IsNullOrEmpty (script.Assembly))
794 assembly = typeof (ScriptManager).Assembly;
796 assembly = Assembly.Load (script.Assembly);
797 string name = GetScriptName (script.Name, isDebugMode, null);
798 if (script.IgnoreScriptPath || String.IsNullOrEmpty (ScriptPath))
799 url = ScriptResourceHandler.GetResourceUrl (assembly, name, script.NotifyScriptLoaded);
801 AssemblyName an = assembly.GetName ();
802 url = ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (ScriptPath), an.Name, '/', an.Version, '/', name));
806 throw new InvalidOperationException ("Name and Path cannot both be empty.");
809 if (loadScriptsBeforeUI)
810 RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
812 RegisterStartupScript (control, typeof (ScriptManager), url, String.Format ("<script src=\"{0}\" type=\"text/javascript\"></script>", url), false);
815 static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures) {
816 if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
819 if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase))
820 throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path. The path must end in '.js'.", releaseName));
822 StringBuilder sb = new StringBuilder (releaseName);
825 sb.Append (".debug");
826 string culture = Thread.CurrentThread.CurrentUICulture.Name;
827 if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0)
828 sb.AppendFormat (".{0}", culture);
831 return sb.ToString ();
834 void RegisterServiceReference (Control control, ServiceReference serviceReference) {
835 if (serviceReference.InlineScript) {
836 string url = control.ResolveUrl (serviceReference.Path);
837 LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (WebServiceParser.GetCompiledType (url, Context), url);
838 RegisterClientScriptBlock (control, typeof (ScriptManager), url, logicalTypeInfo.Proxy, true);
842 string pathInfo = "/js.invoke";
844 string pathInfo = "/js";
846 string url = String.Concat (control.ResolveClientUrl (serviceReference.Path), pathInfo);
847 RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
851 public void RegisterDataItem (Control control, string dataItem) {
852 RegisterDataItem (control, dataItem, false);
855 public void RegisterDataItem (Control control, string dataItem, bool isJsonSerialized) {
856 if (!IsInAsyncPostBack)
857 throw new InvalidOperationException ("RegisterDataItem can only be called during an async postback.");
859 throw new ArgumentNullException ("control");
861 if (_dataItems == null)
862 _dataItems = new Dictionary<Control, DataItemEntry> ();
864 if (_dataItems.ContainsKey (control))
865 throw new ArgumentException (String.Format ("'{0}' already has a data item registered.", control.ID), "control");
867 _dataItems.Add (control, new DataItemEntry (dataItem, isJsonSerialized));
870 public void RegisterDispose (Control control, string disposeScript) {
872 throw new ArgumentNullException ("control");
873 if (disposeScript == null)
874 throw new ArgumentNullException ("disposeScript");
876 UpdatePanel updatePanel = GetUpdatePanel (control);
877 if (updatePanel == null)
880 if (_disposeScripts == null)
881 _disposeScripts = new List<RegisteredDisposeScript> ();
882 _disposeScripts.Add (new RegisteredDisposeScript (control, disposeScript, updatePanel));
885 static UpdatePanel GetUpdatePanel (Control control) {
889 UpdatePanel parent = control.Parent as UpdatePanel;
893 return GetUpdatePanel (control.Parent);
896 public static void RegisterExpandoAttribute (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
897 Page page = control.Page;
898 ScriptManager sm = GetCurrent (page);
900 if (sm._expandoAttributes == null)
901 sm._expandoAttributes = new List<RegisteredExpandoAttribute> ();
903 sm._expandoAttributes.Add (new RegisteredExpandoAttribute (control, controlId, attributeName, attributeValue, encode));
905 if (!sm.IsInAsyncPostBack)
906 page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
909 public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue) {
910 RegisterHiddenField ((Control) page, hiddenFieldName, hiddenFieldInitialValue);
913 public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
914 Page page = control.Page;
915 ScriptManager sm = GetCurrent (page);
917 if (sm._hiddenFields == null)
918 sm._hiddenFields = new List<RegisteredHiddenField> ();
920 sm._hiddenFields.Add (new RegisteredHiddenField (control, hiddenFieldName, hiddenFieldInitialValue));
922 if (!sm.IsInAsyncPostBack)
923 page.ClientScript.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
926 public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script) {
927 RegisterOnSubmitStatement ((Control) page, type, key, script);
930 public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script) {
931 Page page = control.Page;
932 ScriptManager sm = GetCurrent (page);
934 RegisterScript (ref sm._onSubmitStatements, control, type, key, script, null, false, RegisteredScriptType.OnSubmitStatement);
936 if (!sm.IsInAsyncPostBack)
937 page.ClientScript.RegisterOnSubmitStatement (type, key, script);
940 public void RegisterPostBackControl (Control control) {
944 if (_postBackControls == null)
945 _postBackControls = new List<Control> ();
947 if (_postBackControls.Contains (control))
950 _postBackControls.Add (control);
953 internal void RegisterUpdatePanel (UpdatePanel updatePanel) {
954 if (_updatePanels == null)
955 _updatePanels = new List<UpdatePanel> ();
957 if (_updatePanels.Contains (updatePanel))
960 _updatePanels.Add (updatePanel);
963 public void RegisterScriptDescriptors (IExtenderControl extenderControl) {
964 if (extenderControl == null)
967 if (_registeredExtenderControls == null || !_registeredExtenderControls.ContainsKey (extenderControl))
970 Control targetControl = _registeredExtenderControls [extenderControl];
971 RegisterScriptDescriptors ((Control) extenderControl, extenderControl.GetScriptDescriptors (targetControl));
974 public void RegisterScriptDescriptors (IScriptControl scriptControl) {
975 if (scriptControl == null)
978 if (_registeredScriptControls == null || !_registeredScriptControls.Contains (scriptControl))
981 RegisterScriptDescriptors ((Control) scriptControl, scriptControl.GetScriptDescriptors ());
984 void RegisterScriptDescriptors (Control control, IEnumerable<ScriptDescriptor> scriptDescriptors) {
985 if (scriptDescriptors == null)
988 StringBuilder sb = new StringBuilder ();
989 foreach (ScriptDescriptor scriptDescriptor in scriptDescriptors) {
991 scriptDescriptor.FormID = Page.Form.ClientID;
992 sb.AppendLine ("Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).add_init(function() {");
995 sb.AppendLine ("Sys.Application.add_init(function() {");
996 sb.AppendLine (scriptDescriptor.GetScript ());
997 sb.AppendLine ("});");
999 string script = sb.ToString ();
1000 RegisterStartupScript (control, typeof (ScriptDescriptor), script, script, true);
1003 public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags) {
1004 RegisterStartupScript ((Control) page, type, key, script, addScriptTags);
1007 public static void RegisterStartupScript (Control control, Type type, string key, string script, bool addScriptTags) {
1008 Page page = control.Page;
1009 ScriptManager sm = GetCurrent (page);
1011 RegisterScript (ref sm._startupScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientStartupScript);
1013 if (!sm.IsInAsyncPostBack)
1014 page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
1017 public void RegisterScriptControl<TScriptControl> (TScriptControl scriptControl) where TScriptControl : Control, IScriptControl {
1018 if (scriptControl == null)
1019 throw new ArgumentNullException ("scriptControl");
1021 if (_registeredScriptControls == null)
1022 _registeredScriptControls = new List<IScriptControl> ();
1024 if (!_registeredScriptControls.Contains (scriptControl))
1025 _registeredScriptControls.Add (scriptControl);
1028 public void RegisterExtenderControl<TExtenderControl> (TExtenderControl extenderControl, Control targetControl) where TExtenderControl : Control, IExtenderControl {
1029 if (extenderControl == null)
1030 throw new ArgumentNullException ("extenderControl");
1031 if (targetControl == null)
1032 throw new ArgumentNullException ("targetControl");
1034 if (_registeredExtenderControls == null)
1035 _registeredExtenderControls = new Dictionary<IExtenderControl, Control> ();
1037 if (!_registeredExtenderControls.ContainsKey (extenderControl))
1038 _registeredExtenderControls.Add (extenderControl, targetControl);
1041 static void RegisterScript (ref List<RegisteredScript> scriptList, Control control, Type type, string key, string script, string url, bool addScriptTag, RegisteredScriptType scriptType) {
1042 if (scriptList == null)
1043 scriptList = new List<RegisteredScript> ();
1045 scriptList.Add (new RegisteredScript (control, type, key, script, url, addScriptTag, scriptType));
1048 protected override void Render (HtmlTextWriter writer) {
1049 // MSDN: This method is used by control developers to extend the ScriptManager control.
1050 // Notes to Inheritors:
1051 // When overriding this method, call the base Render(HtmlTextWriter) method
1052 // so that PageRequestManager is rendered on the page.
1053 if (SupportsPartialRendering) {
1054 writer.WriteLine ("<script type=\"text/javascript\">");
1055 writer.WriteLine ("//<![CDATA[");
1056 writer.WriteLine ("Sys.WebForms.PageRequestManager._initialize('{0}', document.getElementById('{1}'));", UniqueID, Page.Form.ClientID);
1058 writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance($get(\"{0}\"))._updateControls([{1}], [{2}], [{3}], {4});", Page.Form.ClientID, FormatUpdatePanelIDs (_updatePanels, true), FormatListIDs (_asyncPostBackControls, true), FormatListIDs (_postBackControls, true), AsyncPostBackTimeout);
1060 writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance()._updateControls([{0}], [{1}], [{2}], {3});", FormatUpdatePanelIDs (_updatePanels, true), FormatListIDs (_asyncPostBackControls, true), FormatListIDs (_postBackControls, true), AsyncPostBackTimeout);
1061 writer.WriteLine ("//]]");
1062 writer.WriteLine ("</script>");
1064 base.Render (writer);
1067 static string FormatUpdatePanelIDs (List<UpdatePanel> list, bool useSingleQuote) {
1068 if (list == null || list.Count == 0)
1071 StringBuilder sb = new StringBuilder ();
1072 for (int i = 0; i < list.Count; i++) {
1073 sb.AppendFormat ("{0}{1}{2}{0},", useSingleQuote ? "'" : String.Empty, list [i].ChildrenAsTriggers ? "t" : "f", list [i].UniqueID);
1078 return sb.ToString ();
1081 static string FormatListIDs<T> (List<T> list, bool useSingleQuote) where T : Control {
1082 if (list == null || list.Count == 0)
1085 StringBuilder sb = new StringBuilder ();
1086 for (int i = 0; i < list.Count; i++) {
1087 sb.AppendFormat ("{0}{1}{0},", useSingleQuote ? "'" : String.Empty, list [i].UniqueID);
1092 return sb.ToString ();
1095 public void SetFocus (Control control) {
1096 if (control == null)
1097 throw new ArgumentNullException ("control");
1099 if (IsInAsyncPostBack) {
1100 EnsureFocusClientScript ();
1101 _controlIDToFocus = control.ClientID;
1104 Page.SetFocus (control);
1107 public void SetFocus (string clientID) {
1108 if (String.IsNullOrEmpty (clientID))
1109 throw new ArgumentNullException ("control");
1111 if (IsInAsyncPostBack) {
1112 EnsureFocusClientScript ();
1113 _controlIDToFocus = clientID;
1116 Page.SetFocus (clientID);
1119 void EnsureFocusClientScript () {
1121 RegisterClientScriptResource (this, typeof (ClientScriptManager), "Focus.js");
1125 #region IPostBackDataHandler Members
1127 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection) {
1128 return LoadPostData (postDataKey, postCollection);
1131 void IPostBackDataHandler.RaisePostDataChangedEvent () {
1132 RaisePostDataChangedEvent ();
1137 internal void WriteCallbackException (TextWriter output, Exception ex, bool writeMessage) {
1139 if (ex is HttpUnhandledException)
1140 ex = ex.InnerException;
1142 HttpException httpEx = ex as HttpException;
1143 string message = AsyncPostBackErrorMessage;
1144 if (String.IsNullOrEmpty (message) && writeMessage)
1145 message = ex.Message;
1146 WriteCallbackOutput (output, error, httpEx == null ? "500" : httpEx.GetHttpCode ().ToString (), message);
1149 static internal void WriteCallbackRedirect (TextWriter output, string redirectUrl) {
1150 WriteCallbackOutput (output, pageRedirect, null, redirectUrl);
1153 internal void WriteCallbackPanel (TextWriter output, UpdatePanel panel, StringBuilder panelOutput) {
1154 if (_panelsToRefresh == null)
1155 _panelsToRefresh = new List<UpdatePanel> ();
1156 _panelsToRefresh.Add (panel);
1158 WriteCallbackOutput (output, updatePanel, panel.ClientID, panelOutput);
1161 internal void RegisterChildUpdatePanel (UpdatePanel updatePanel) {
1162 if (_childUpdatePanels == null)
1163 _childUpdatePanels = new List<UpdatePanel> ();
1164 _childUpdatePanels.Add (updatePanel);
1167 static void WriteCallbackOutput (TextWriter output, string type, string name, object value) {
1168 string str = value as string;
1169 StringBuilder sb = value as StringBuilder;
1172 length = str.Length;
1173 else if (sb != null)
1176 //output.Write ("{0}|{1}|{2}|{3}|", value == null ? 0 : value.Length, type, name, value);
1177 output.Write (length);
1179 output.Write (type);
1181 output.Write (name);
1183 for (int i = 0; i < length; i++)
1185 output.Write (str [i]);
1187 output.Write (sb [i]);
1191 void RenderPageCallback (HtmlTextWriter output, Control container) {
1192 Page page = (Page) container;
1194 page.Form.SetRenderMethodDelegate (RenderFormCallback);
1195 HtmlTextParser parser = new HtmlTextParser (output);
1196 page.Form.RenderControl (parser);
1198 WriteCallbackOutput (output, asyncPostBackControlIDs, null, FormatListIDs (_asyncPostBackControls, false));
1199 WriteCallbackOutput (output, postBackControlIDs, null, FormatListIDs (_postBackControls, false));
1200 WriteCallbackOutput (output, updatePanelIDs, null, FormatUpdatePanelIDs (_updatePanels, false));
1201 WriteCallbackOutput (output, childUpdatePanelIDs, null, FormatListIDs (_childUpdatePanels, false));
1202 WriteCallbackOutput (output, panelsToRefreshIDs, null, FormatListIDs (_panelsToRefresh, false));
1203 WriteCallbackOutput (output, asyncPostBackTimeout, null, AsyncPostBackTimeout.ToString ());
1205 WriteCallbackOutput (output, pageTitle, null, Page.Title);
1207 if (_dataItems != null)
1208 foreach (Control control in _dataItems.Keys) {
1209 DataItemEntry entry = _dataItems [control];
1210 WriteCallbackOutput (output, entry.IsJsonSerialized ? dataItemJson : dataItem, control.ClientID, entry.DataItem);
1213 WriteArrayDeclarations (output);
1214 WriteExpandoAttributes (output);
1215 WriteScriptBlocks (output, _clientScriptBlocks);
1216 WriteScriptBlocks (output, _startupScriptBlocks);
1217 WriteScriptBlocks (output, _onSubmitStatements);
1218 WriteHiddenFields (output);
1220 if (!String.IsNullOrEmpty (_controlIDToFocus))
1221 WriteCallbackOutput (output, focus, null, _controlIDToFocus);
1223 if (_disposeScripts != null)
1224 for (int i = 0; i < _disposeScripts.Count; i++) {
1225 RegisteredDisposeScript entry = _disposeScripts [i];
1226 if ((_panelsToRefresh != null && _panelsToRefresh.IndexOf (entry.UpdatePanel) >= 0) || (_childUpdatePanels != null && _childUpdatePanels.IndexOf (entry.UpdatePanel) >= 0))
1227 WriteCallbackOutput (output, scriptDispose, entry.UpdatePanel.ClientID, entry.Script);
1231 private void WriteExpandoAttributes (HtmlTextWriter writer) {
1232 if (_expandoAttributes != null) {
1233 for (int i = 0; i < _expandoAttributes.Count; i++) {
1234 RegisteredExpandoAttribute attr = _expandoAttributes [i];
1235 if (HasBeenRendered (attr.Control)) {
1238 StringWriter sw = new StringWriter ();
1239 Newtonsoft.Json.JavaScriptUtils.WriteEscapedJavaScriptString (attr.Value, sw);
1240 value = sw.ToString ();
1243 value = "\"" + attr.Value + "\"";
1244 WriteCallbackOutput (writer, expando, "document.getElementById('" + attr.ControlId + "')['" + attr.Name + "']", value);
1250 void WriteArrayDeclarations (HtmlTextWriter writer) {
1251 if (_arrayDeclarations != null) {
1252 for (int i = 0; i < _arrayDeclarations.Count; i++) {
1253 RegisteredArrayDeclaration array = _arrayDeclarations [i];
1254 if (Page == array.Control || HasBeenRendered (array.Control))
1255 WriteCallbackOutput (writer, arrayDeclaration, array.Name, array.Value);
1260 void WriteScriptBlocks (HtmlTextWriter output, List<RegisteredScript> scriptList) {
1261 if (scriptList == null)
1263 Hashtable registeredScripts = new Hashtable ();
1264 for (int i = 0; i < scriptList.Count; i++) {
1265 RegisteredScript scriptEntry = scriptList [i];
1266 if (registeredScripts.ContainsKey (scriptEntry.Key))
1268 if (Page == scriptEntry.Control || HasBeenRendered (scriptEntry.Control)) {
1269 registeredScripts.Add (scriptEntry.Key, scriptEntry);
1270 switch (scriptEntry.ScriptType) {
1271 case RegisteredScriptType.ClientScriptBlock:
1272 if (scriptEntry.AddScriptTags)
1273 WriteCallbackOutput (output, scriptBlock, scriptContentNoTags, scriptEntry.Script);
1275 WriteCallbackOutput (output, scriptBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
1277 case RegisteredScriptType.ClientStartupScript:
1278 if (scriptEntry.AddScriptTags)
1279 WriteCallbackOutput (output, scriptStartupBlock, scriptContentNoTags, scriptEntry.Script);
1281 WriteCallbackOutput (output, scriptStartupBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
1283 case RegisteredScriptType.ClientScriptInclude:
1284 WriteCallbackOutput (output, scriptBlock, scriptPath, scriptEntry.Url);
1286 case RegisteredScriptType.OnSubmitStatement:
1287 WriteCallbackOutput (output, onSubmit, null, scriptEntry.Script);
1294 void WriteHiddenFields (HtmlTextWriter output) {
1295 if (_hiddenFields == null)
1297 Hashtable registeredFields = new Hashtable ();
1298 for (int i = 0; i < _hiddenFields.Count; i++) {
1299 RegisteredHiddenField field = _hiddenFields [i];
1300 if (registeredFields.ContainsKey (field.Name))
1302 if (Page == field.Control || HasBeenRendered (field.Control)) {
1303 registeredFields.Add (field.Name, field);
1304 WriteCallbackOutput (output, hiddenField, field.Name, field.InitialValue);
1309 static string SerializeScriptBlock (RegisteredScript scriptEntry) {
1311 XmlTextReader reader = new XmlTextReader (new StringReader (scriptEntry.Script));
1312 while (reader.Read ()) {
1313 switch (reader.NodeType) {
1314 case XmlNodeType.Element:
1315 if (String.Compare ("script", reader.Name, StringComparison.OrdinalIgnoreCase) == 0) {
1316 Dictionary<string, string> dic = new Dictionary<string, string> ();
1317 while (reader.MoveToNextAttribute ()) {
1318 dic.Add (reader.Name, reader.Value);
1320 reader.MoveToContent ();
1321 dic.Add ("text", reader.ReadInnerXml ());
1322 return JavaScriptSerializer.DefaultSerializer.Serialize (dic);
1332 throw new InvalidOperationException (String.Format ("The script tag registered for type '{0}' and key '{1}' has invalid characters outside of the script tags: {2}. Only properly formatted script tags can be registered.", scriptEntry.Type, scriptEntry.Key, scriptEntry.Script));
1335 void RenderFormCallback (HtmlTextWriter output, Control container) {
1336 output = ((HtmlTextParser) output).ResponseOutput;
1337 HtmlForm form = (HtmlForm) container;
1338 HtmlTextWriter writer = new HtmlDropWriter (output);
1339 if (form.HasControls ()) {
1340 for (int i = 0; i < form.Controls.Count; i++) {
1341 form.Controls [i].RenderControl (writer);
1346 internal class AlternativeHtmlTextWriter : HtmlTextWriter
1348 readonly HtmlTextWriter _responseOutput;
1350 public HtmlTextWriter ResponseOutput {
1351 get { return _responseOutput; }
1354 public AlternativeHtmlTextWriter (TextWriter writer, HtmlTextWriter responseOutput)
1356 _responseOutput = responseOutput;
1360 sealed class HtmlTextParser : AlternativeHtmlTextWriter
1364 public HtmlTextParser (HtmlTextWriter responseOutput)
1365 : base (new TextParser (responseOutput), responseOutput) {
1368 public override void WriteAttribute (string name, string value) {
1369 if (!_done && String.Compare ("action", name, StringComparison.OrdinalIgnoreCase) == 0) {
1371 ScriptManager.WriteCallbackOutput (ResponseOutput, formAction, null, value);
1374 base.WriteAttribute (name, value);
1378 sealed class TextParser : TextWriter
1381 char _charState = (char) 255;
1382 const char nullCharState = (char) 255;
1383 StringBuilder _sb = new StringBuilder ();
1384 Dictionary<string, string> _currentField;
1385 string _currentAttribute;
1386 readonly HtmlTextWriter _responseOutput;
1388 public override Encoding Encoding {
1389 get { return Encoding.UTF8; }
1392 public TextParser (HtmlTextWriter responseOutput) {
1393 _responseOutput = responseOutput;
1396 public override void Write (char value) {
1399 ParseBeginTag (value);
1402 ParseAttributeName (value);
1405 ParseAttributeValue (value);
1410 private void ParseAttributeValue (char value) {
1416 _currentField.Add (_currentAttribute, _sb.ToString ());
1419 ProbeWriteOutput ();
1427 private void ParseAttributeName (char value) {
1436 _currentAttribute = _sb.ToString ();
1446 void ParseBeginTag (char value) {
1447 switch (_charState) {
1485 _currentField = new Dictionary<string, string> ();
1493 private void ResetState () {
1494 _charState = nullCharState;
1499 private void ProbeWriteOutput () {
1500 if (!_currentField.ContainsKey ("name"))
1502 if (!_currentField.ContainsKey ("value"))
1505 string value = _currentField ["value"];
1506 if (String.IsNullOrEmpty (value))
1509 ScriptManager.WriteCallbackOutput (_responseOutput, hiddenField, _currentField ["name"], HttpUtility.HtmlDecode (value));
1513 sealed class HtmlDropWriter : AlternativeHtmlTextWriter
1515 public HtmlDropWriter (HtmlTextWriter responseOutput)
1516 : base (new DropWriter (), responseOutput) {
1520 sealed class DropWriter : TextWriter
1522 public override Encoding Encoding {
1523 get { return Encoding.UTF8; }
1527 sealed class CultureInfoSerializer : JavaScriptSerializer.LazyDictionary
1529 readonly CultureInfo _ci;
1530 public CultureInfoSerializer (CultureInfo ci) {
1532 throw new ArgumentNullException ("ci");
1535 protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
1536 yield return new KeyValuePair<string, object> ("name", _ci.Name);
1537 yield return new KeyValuePair<string, object> ("numberFormat", _ci.NumberFormat);
1538 yield return new KeyValuePair<string, object> ("dateTimeFormat", _ci.DateTimeFormat);
1542 sealed class ScriptReferenceEntry
1544 readonly Control _control;
1545 readonly ScriptReference _scriptReference;
1546 readonly bool _loadBeforeUI;
1548 public Control Control { get { return _control; } }
1549 public ScriptReference ScriptReference { get { return _scriptReference; } }
1550 public bool LoadScriptsBeforeUI { get { return _loadBeforeUI; } }
1552 public ScriptReferenceEntry (Control control, ScriptReference scriptReference, bool loadBeforeUI) {
1554 _scriptReference = scriptReference;
1555 _loadBeforeUI = loadBeforeUI;
1559 sealed class DataItemEntry
1561 readonly string _dataItem;
1562 readonly bool _isJsonSerialized;
1564 public string DataItem { get { return _dataItem; } }
1565 public bool IsJsonSerialized { get { return _isJsonSerialized; } }
1567 public DataItemEntry (string dataItem, bool isJsonSerialized) {
1568 _dataItem = dataItem;
1569 _isJsonSerialized = isJsonSerialized;
1573 internal void RegisterProxy (ScriptManagerProxy scriptManagerProxy) {
1574 if (_proxies == null)
1575 _proxies = new List<ScriptManagerProxy> ();
1577 _proxies.Add (scriptManagerProxy);
1580 #region IScriptManager Members
1582 void IScriptManager.RegisterOnSubmitStatementExternal (Control control, Type type, string key, string script) {
1583 RegisterOnSubmitStatement (control, type, key, script);
1586 void IScriptManager.RegisterExpandoAttributeExternal (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
1587 RegisterExpandoAttribute (control, controlId, attributeName, attributeValue, encode);
1590 void IScriptManager.RegisterHiddenFieldExternal (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
1591 RegisterHiddenField (control, hiddenFieldName, hiddenFieldInitialValue);
1594 void IScriptManager.RegisterStartupScriptExternal (Control control, Type type, string key, string script, bool addScriptTags) {
1595 RegisterStartupScript (control, type, key, script, addScriptTags);
1598 void IScriptManager.RegisterArrayDeclarationExternal (Control control, string arrayName, string arrayValue) {
1599 RegisterArrayDeclaration (control, arrayName, arrayValue);
1602 void IScriptManager.RegisterClientScriptBlockExternal (Control control, Type type, string key, string script, bool addScriptTags) {
1603 RegisterClientScriptBlock (control, type, key, script, addScriptTags);
1606 void IScriptManager.RegisterClientScriptIncludeExternal (Control control, Type type, string key, string url) {
1607 RegisterClientScriptInclude (control, type, key, url);
1610 void IScriptManager.RegisterClientScriptResourceExternal (Control control, Type type, string resourceName) {
1611 RegisterClientScriptResource (control, type, resourceName);