consider update panel correctly within follow api:
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI / ScriptManager.cs
1 //
2 // ScriptManager.cs
3 //
4 // Author:
5 //   Igor Zelmanovich <igorz@mainsoft.com>
6 //
7 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
8 //
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Text;
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;
41 using System.IO;
42 using System.Globalization;
43 using System.Threading;
44 using System.Web.Script.Serialization;
45 using System.Web.Script.Services;
46 using System.Xml;
47 using System.Collections.ObjectModel;
48
49 namespace System.Web.UI
50 {
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
59         {
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";
85
86                 static readonly object ScriptManagerKey = typeof (IScriptManager);
87
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;
102                 string _scriptPath;
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;
113                 bool _init;
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;
126
127                 [DefaultValue (true)]
128                 [Category ("Behavior")]
129                 public bool AllowCustomErrorsRedirect {
130                         get {
131                                 return _allowCustomErrorsRedirect;
132                         }
133                         set {
134                                 _allowCustomErrorsRedirect = value;
135                         }
136                 }
137
138                 [Category ("Behavior")]
139                 [DefaultValue ("")]
140                 public string AsyncPostBackErrorMessage {
141                         get {
142                                 if (String.IsNullOrEmpty (_asyncPostBackErrorMessage))
143                                         return String.Empty;
144                                 return _asyncPostBackErrorMessage;
145                         }
146                         set {
147                                 _asyncPostBackErrorMessage = value;
148                         }
149                 }
150
151                 [Browsable (false)]
152                 public string AsyncPostBackSourceElementID {
153                         get {
154                                 if (_asyncPostBackSourceElementID == null)
155                                         return String.Empty;
156                                 return _asyncPostBackSourceElementID;
157                         }
158                 }
159
160                 [DefaultValue (90)]
161                 [Category ("Behavior")]
162                 public int AsyncPostBackTimeout {
163                         get {
164                                 return _asyncPostBackTimeout;
165                         }
166                         set {
167                                 _asyncPostBackTimeout = value;
168                         }
169                 }
170
171                 [Category ("Behavior")]
172                 [MergableProperty (false)]
173                 [DefaultValue ("")]
174                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
175                 [PersistenceMode (PersistenceMode.InnerProperty)]
176                 public AuthenticationServiceManager AuthenticationService {
177                         get {
178                                 if (_authenticationService == null)
179                                         _authenticationService = new AuthenticationServiceManager ();
180                                 return _authenticationService;
181                         }
182                 }
183
184                 [Category ("Behavior")]
185                 [DefaultValue (false)]
186                 public bool EnablePageMethods {
187                         get {
188                                 return _enablePageMethods;
189                         }
190                         set {
191                                 _enablePageMethods = value;
192                         }
193                 }
194
195                 [DefaultValue (true)]
196                 [Category ("Behavior")]
197                 public bool EnablePartialRendering {
198                         get {
199                                 return _enablePartialRendering;
200                         }
201                         set {
202                                 if (_init)
203                                         throw new InvalidOperationException ();
204                                 _enablePartialRendering = value;
205                         }
206                 }
207
208                 [DefaultValue (false)]
209                 [Category ("Behavior")]
210                 public bool EnableScriptGlobalization {
211                         get {
212                                 return _enableScriptGlobalization;
213                         }
214                         set {
215                                 _enableScriptGlobalization = value;
216                         }
217                 }
218
219                 [Category ("Behavior")]
220                 [DefaultValue (false)]
221                 public bool EnableScriptLocalization {
222                         get {
223                                 return _enableScriptLocalization;
224                         }
225                         set {
226                                 _enableScriptLocalization = value;
227                         }
228                 }
229
230                 [Browsable (false)]
231                 public bool IsDebuggingEnabled {
232                         get {
233                                 if (IsDeploymentRetail)
234                                         return false;
235
236                                 CompilationSection compilation = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
237                                 if (!compilation.Debug && (ScriptMode == ScriptMode.Auto || ScriptMode == ScriptMode.Inherit))
238                                         return false;
239
240                                 if (ScriptMode == ScriptMode.Release)
241                                         return false;
242
243                                 return true;
244                         }
245                 }
246
247                 bool IsDeploymentRetail {
248                         get {
249 #if TARGET_J2EE
250                                 return false;
251 #else
252                                 DeploymentSection deployment = (DeploymentSection) WebConfigurationManager.GetSection ("system.web/deployment");
253                                 return deployment.Retail;
254 #endif
255                         }
256                 }
257
258                 [Browsable (false)]
259                 public bool IsInAsyncPostBack {
260                         get {
261                                 return _isInAsyncPostBack;
262                         }
263                 }
264
265                 internal bool IsInPartialRendering {
266                         get {
267                                 return _isInPartialRendering;
268                         }
269                         set {
270                                 _isInPartialRendering = value;
271                         }
272                 }
273
274                 [Category ("Behavior")]
275                 [DefaultValue (true)]
276                 public bool LoadScriptsBeforeUI {
277                         get {
278                                 return _loadScriptsBeforeUI;
279                         }
280                         set {
281                                 _loadScriptsBeforeUI = value;
282                         }
283                 }
284
285                 [PersistenceMode (PersistenceMode.InnerProperty)]
286                 [DefaultValue ("")]
287                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
288                 [Category ("Behavior")]
289                 [MergableProperty (false)]
290                 public ProfileServiceManager ProfileService {
291                         get {
292                                 if (_profileService == null)
293                                         _profileService = new ProfileServiceManager ();
294                                 return _profileService;
295                         }
296                 }
297
298                 [Category ("Behavior")]
299 #if TARGET_J2EE
300                 [MonoLimitation ("The 'Auto' value is the same as 'Debug'.")]
301 #endif
302                 public ScriptMode ScriptMode {
303                         get {
304                                 return _scriptMode;
305                         }
306                         set {
307                                 if (value == ScriptMode.Inherit)
308                                         value = ScriptMode.Auto;
309                                 _scriptMode = value;
310                         }
311                 }
312
313                 [DefaultValue ("")]
314                 [Category ("Behavior")]
315                 public string ScriptPath {
316                         get {
317                                 if (_scriptPath == null)
318                                         return String.Empty;
319                                 return _scriptPath;
320                         }
321                         set {
322                                 _scriptPath = value;
323                         }
324                 }
325
326                 [PersistenceMode (PersistenceMode.InnerProperty)]
327                 [DefaultValue ("")]
328                 [Category ("Behavior")]
329                 [MergableProperty (false)]
330                 public ScriptReferenceCollection Scripts {
331                         get {
332                                 if (_scripts == null)
333                                         _scripts = new ScriptReferenceCollection ();
334
335                                 return _scripts;
336                         }
337                 }
338
339                 [PersistenceMode (PersistenceMode.InnerProperty)]
340                 [DefaultValue ("")]
341                 [MergableProperty (false)]
342                 [Category ("Behavior")]
343                 public ServiceReferenceCollection Services {
344                         get {
345                                 if (_services == null)
346                                         _services = new ServiceReferenceCollection ();
347
348                                 return _services;
349                         }
350                 }
351
352                 [DefaultValue (true)]
353                 [Browsable (false)]
354                 public bool SupportsPartialRendering {
355                         get {
356                                 if (!_supportsPartialRendering.HasValue)
357                                         _supportsPartialRendering = CheckSupportsPartialRendering ();
358                                 return _supportsPartialRendering.Value;
359                         }
360                         set {
361                                 if (_init)
362                                         throw new InvalidOperationException ();
363                                 if (!EnablePartialRendering && value)
364                                         throw new InvalidOperationException ("The SupportsPartialRendering property cannot be set when EnablePartialRendering is false.");
365
366                                 _supportsPartialRendering = value;
367                         }
368                 }
369
370                 bool CheckSupportsPartialRendering () {
371                         if (!EnablePartialRendering)
372                                 return false;
373                         // TODO: consider browser capabilities
374                         return true;
375                 }
376
377                 [EditorBrowsable (EditorBrowsableState.Never)]
378                 [Browsable (false)]
379                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
380                 public override bool Visible {
381                         get {
382                                 return true;
383                         }
384                         set {
385                                 throw new NotImplementedException ();
386                         }
387                 }
388
389                 [Category ("Action")]
390                 public event EventHandler<AsyncPostBackErrorEventArgs> AsyncPostBackError;
391
392                 [Category ("Action")]
393                 public event EventHandler<ScriptReferenceEventArgs> ResolveScriptReference;
394
395                 public static ScriptManager GetCurrent (Page page) {
396                         if (page == null)
397                                 throw new ArgumentNullException ("page");
398                         return (ScriptManager) page.Items [ScriptManagerKey];
399                 }
400
401                 static void SetCurrent (Page page, ScriptManager instance) {
402                         page.Items [ScriptManagerKey] = instance;
403                 }
404
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];
412                                 return true;
413                         }
414                         return false;
415                 }
416
417                 protected internal virtual void OnAsyncPostBackError (AsyncPostBackErrorEventArgs e) {
418                         if (AsyncPostBackError != null)
419                                 AsyncPostBackError (this, e);
420                 }
421
422                 protected override void OnInit (EventArgs e) {
423                         base.OnInit (e);
424
425                         if (GetCurrent (Page) != null)
426                                 throw new InvalidOperationException ("Only one instance of a ScriptManager can be added to the page.");
427
428                         SetCurrent (Page, this);
429                         Page.Error += new EventHandler (OnPageError);
430                         _init = true;
431                 }
432
433                 void OnPageError (object sender, EventArgs e) {
434                         if (IsInAsyncPostBack)
435                                 OnAsyncPostBackError (new AsyncPostBackErrorEventArgs (Context.Error));
436                 }
437
438                 protected override void OnPreRender (EventArgs e) {
439                         base.OnPreRender (e);
440
441                         Page.PreRenderComplete += new EventHandler (OnPreRenderComplete);
442
443                         if (IsInAsyncPostBack) {
444                                 Page.SetRenderMethodDelegate (RenderPageCallback);
445                         }
446                         else {
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);
451                                 }
452
453                                 // Register dispose script
454                                 if (_disposeScripts != null && _disposeScripts.Count > 0) {
455                                         StringBuilder sb = new StringBuilder ();
456                                         sb.AppendLine ();
457                                         for (int i = 0; i < _disposeScripts.Count; i++) {
458                                                 RegisteredDisposeScript entry = _disposeScripts [i];
459                                                 if (IsMultiForm)
460                                                         sb.Append ("Sys.WebForms.PageRequestManager.getInstance($get(\"" + Page.Form.ClientID + "\"))._registerDisposeScript(\"");
461                                                 else
462                                                         sb.Append ("Sys.WebForms.PageRequestManager.getInstance()._registerDisposeScript(\"");
463                                                 sb.Append (entry.UpdatePanel.ClientID);
464                                                 sb.Append ("\", ");
465                                                 sb.Append (JavaScriptSerializer.DefaultSerializer.Serialize (entry.Script)); //JavaScriptSerializer.Serialize used escape script literal 
466                                                 sb.AppendLine (");");
467                                         }
468                                         RegisterStartupScript (this, typeof (ExtenderControl), "disposeScripts;", sb.ToString (), true);
469                                 }
470
471 #if TARGET_DOTNET
472                                 // to cause webform client script being included
473                                 Page.ClientScript.GetPostBackEventReference (new PostBackOptions (this, null, null, false, false, false, true, true, null));
474 #else
475                                 Page.ClientScript.GetPostBackEventReference (this, null);
476 #endif
477                         }
478                 }
479
480                 void OnPreRenderComplete (object sender, EventArgs e) {
481                         // Resolve Scripts
482                         ScriptReference ajaxScript = new ScriptReference ("MicrosoftAjax.js", String.Empty);
483                         ajaxScript.NotifyScriptLoaded = false;
484                         OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxScript));
485
486                         ScriptReference ajaxWebFormsScript = new ScriptReference ("MicrosoftAjaxWebForms.js", String.Empty);
487                         ajaxWebFormsScript.NotifyScriptLoaded = false;
488                         OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxWebFormsScript));
489
490                         ScriptReference ajaxExtensionScript = null;
491                         ScriptReference ajaxWebFormsExtensionScript = null;
492                         if (IsMultiForm) {
493                                 ajaxExtensionScript = new ScriptReference ("MicrosoftAjaxExtension.js", String.Empty);
494                                 OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxExtensionScript));
495
496                                 ajaxWebFormsExtensionScript = new ScriptReference ("MicrosoftAjaxWebFormsExtension.js", String.Empty);
497                                 OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxWebFormsExtensionScript));
498                         }
499
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);
505                         }
506
507                         if (!IsInAsyncPostBack) {
508                                 // Register Ajax framework script.
509                                 RegisterScriptReference (this, ajaxScript, true);
510                                 if (IsMultiForm) {
511                                         RegisterScriptReference (this, ajaxExtensionScript, true);
512                                         RegisterClientScriptBlock (this, typeof (ScriptManager), "Sys.Application", "\nSys.Application._initialize(document.getElementById('" + Page.Form.ClientID + "'));\n", true);
513                                 }
514
515                                 StringBuilder sb = new StringBuilder ();
516                                 sb.AppendLine ("if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');");
517
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 + "');");
524
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);");
530                                 }
531                                 string authenticationServicePath = GetAuthenticationServicePath ();
532                                 if (!String.IsNullOrEmpty (authenticationServicePath))
533                                         sb.AppendLine ("Sys.Services.AuthenticationService.set_path('" + authenticationServicePath + "');");
534
535                                 RegisterClientScriptBlock (this, typeof (ScriptManager), "Framework", sb.ToString (), true);
536
537                                 RegisterScriptReference (this, ajaxWebFormsScript, true);
538
539                                 if (IsMultiForm)
540                                         RegisterScriptReference (this, ajaxWebFormsExtensionScript, true);
541                         }
542
543                         // Register Scripts
544                         if (_scriptToRegister != null)
545                                 for (int i = 0; i < _scriptToRegister.Count; i++)
546                                         RegisterScriptReference (_scriptToRegister [i].Control, _scriptToRegister [i].ScriptReference, _scriptToRegister [i].LoadScriptsBeforeUI);
547
548                         if (!IsInAsyncPostBack) {
549                                 // Register services
550                                 if (_services != null && _services.Count > 0) {
551                                         for (int i = 0; i < _services.Count; i++) {
552                                                 RegisterServiceReference (this, _services [i]);
553                                         }
554                                 }
555
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]);
561                                                 }
562                                         }
563                                 }
564
565                                 if (EnablePageMethods) {
566                                         LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (Page.GetType (), Page.Request.FilePath);
567                                         RegisterClientScriptBlock (this, typeof (ScriptManager), "PageMethods", logicalTypeInfo.Proxy, true);
568                                 }
569
570                                 // Register startup script
571                                 if (IsMultiForm)
572                                         RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", "Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).initialize();\n", true);
573                                 else
574                                         RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", "Sys.Application.initialize();\n", true);
575                         }
576                 }
577
578                 string GetProfileServicePath () {
579                         if (_profileService != null && !String.IsNullOrEmpty (_profileService.Path))
580                                 return ResolveClientUrl (_profileService.Path);
581
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);
586                         return null;
587                 }
588
589                 string GetAuthenticationServicePath () {
590                         if (_authenticationService != null && !String.IsNullOrEmpty (_authenticationService.Path))
591                                 return ResolveClientUrl (_authenticationService.Path);
592
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);
597                         return null;
598                 }
599
600                 public ReadOnlyCollection<RegisteredArrayDeclaration> GetRegisteredArrayDeclarations () {
601                         if (_arrayDeclarations == null)
602                                 _arrayDeclarations = new List<RegisteredArrayDeclaration> ();
603                         return new ReadOnlyCollection<RegisteredArrayDeclaration> (_arrayDeclarations);
604                 }
605
606                 public ReadOnlyCollection<RegisteredScript> GetRegisteredClientScriptBlocks () {
607                         if (_clientScriptBlocks == null)
608                                 _clientScriptBlocks = new List<RegisteredScript> ();
609                         return new ReadOnlyCollection<RegisteredScript> (_clientScriptBlocks);
610                 }
611
612                 public ReadOnlyCollection<RegisteredDisposeScript> GetRegisteredDisposeScripts () {
613                         if (_disposeScripts == null)
614                                 _disposeScripts = new List<RegisteredDisposeScript> ();
615                         return new ReadOnlyCollection<RegisteredDisposeScript> (_disposeScripts);
616                 }
617
618                 public ReadOnlyCollection<RegisteredExpandoAttribute> GetRegisteredExpandoAttributes () {
619                         if (_expandoAttributes == null)
620                                 _expandoAttributes = new List<RegisteredExpandoAttribute> ();
621                         return new ReadOnlyCollection<RegisteredExpandoAttribute> (_expandoAttributes);
622                 }
623
624                 public ReadOnlyCollection<RegisteredHiddenField> GetRegisteredHiddenFields () {
625                         if (_hiddenFields == null)
626                                 _hiddenFields = new List<RegisteredHiddenField> ();
627                         return new ReadOnlyCollection<RegisteredHiddenField> (_hiddenFields);
628                 }
629
630                 public ReadOnlyCollection<RegisteredScript> GetRegisteredOnSubmitStatements () {
631                         if (_onSubmitStatements == null)
632                                 _onSubmitStatements = new List<RegisteredScript> ();
633                         return new ReadOnlyCollection<RegisteredScript> (_onSubmitStatements);
634                 }
635
636                 public ReadOnlyCollection<RegisteredScript> GetRegisteredStartupScripts () {
637                         if (_startupScriptBlocks == null)
638                                 _startupScriptBlocks = new List<RegisteredScript> ();
639                         return new ReadOnlyCollection<RegisteredScript> (_startupScriptBlocks);
640                 }
641
642 #if TARGET_J2EE
643                 bool _isMultiForm = false;
644                 bool _isMultiFormInited = false;
645
646                 bool IsMultiForm {
647                         get {
648                                 if (!_isMultiFormInited) {
649                                         string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
650                                         _isMultiForm = isMultiForm != null ? Boolean.Parse (isMultiForm) : false;
651
652                                         _isMultiFormInited = true;
653                                 }
654                                 return _isMultiForm;
655                         }
656                 }
657 #else
658                 bool IsMultiForm {
659                         get { return false; }
660                 }
661 #endif
662
663                 static bool HasBeenRendered (Control control) {
664                         if (control == null)
665                                 return false;
666
667                         if (control is UpdatePanel && ((UpdatePanel) control).RequiresUpdate)
668                                 return true;
669
670                         return HasBeenRendered (control.Parent);
671                 }
672
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);
677                                 }
678                         }
679
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);
685                                 }
686                         }
687
688                         if (_registeredScriptControls != null && _registeredScriptControls.Count > 0) {
689                                 for (int i = 0; i < _registeredScriptControls.Count; i++) {
690                                         IEnumerable<ScriptReference> scripts = _registeredScriptControls [i].GetScriptReferences ();
691                                         if (scripts != null)
692                                                 foreach (ScriptReference s in scripts)
693                                                         yield return new ScriptReferenceEntry ((Control) _registeredScriptControls [i], s, LoadScriptsBeforeUI);
694                                 }
695                         }
696
697                         if (_registeredExtenderControls != null && _registeredExtenderControls.Count > 0) {
698                                 foreach (IExtenderControl ex in _registeredExtenderControls.Keys) {
699                                         IEnumerable<ScriptReference> scripts = ex.GetScriptReferences ();
700                                         if (scripts != null)
701                                                 foreach (ScriptReference s in scripts)
702                                                         yield return new ScriptReferenceEntry ((Control) ex, s, LoadScriptsBeforeUI);
703                                 }
704                         }
705                 }
706
707                 protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e) {
708                         if (ResolveScriptReference != null)
709                                 ResolveScriptReference (this, e);
710                 }
711
712                 protected virtual void RaisePostDataChangedEvent () {
713                         UpdatePanel up = Page.FindControl (_panelToRefreshID) as UpdatePanel;
714                         if (up != null && up.ChildrenAsTriggers)
715                                 up.Update ();
716                 }
717
718                 public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue) {
719                         RegisterArrayDeclaration ((Control) page, arrayName, arrayValue);
720                 }
721
722                 public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue) {
723                         Page page = control.Page;
724                         ScriptManager sm = GetCurrent (page);
725
726                         if (sm._arrayDeclarations == null)
727                                 sm._arrayDeclarations = new List<RegisteredArrayDeclaration> ();
728
729                         sm._arrayDeclarations.Add (new RegisteredArrayDeclaration (control, arrayName, arrayValue));
730
731                         if (!sm.IsInAsyncPostBack)
732                                 page.ClientScript.RegisterArrayDeclaration (arrayName, arrayValue);
733                 }
734
735                 public void RegisterAsyncPostBackControl (Control control) {
736                         if (control == null)
737                                 return;
738
739                         if (_asyncPostBackControls == null)
740                                 _asyncPostBackControls = new List<Control> ();
741
742                         if (_asyncPostBackControls.Contains (control))
743                                 return;
744
745                         _asyncPostBackControls.Add (control);
746                 }
747
748                 public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags) {
749                         RegisterClientScriptBlock ((Control) page, type, key, script, addScriptTags);
750                 }
751
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);
755
756                         RegisterScript (ref sm._clientScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientScriptBlock);
757
758                         if (!sm.IsInAsyncPostBack)
759                                 page.ClientScript.RegisterClientScriptBlock (type, key, script, addScriptTags);
760                 }
761
762                 public static void RegisterClientScriptInclude (Page page, Type type, string key, string url) {
763                         RegisterClientScriptInclude ((Control) page, type, key, url);
764                 }
765
766                 public static void RegisterClientScriptInclude (Control control, Type type, string key, string url) {
767                         Page page = control.Page;
768                         ScriptManager sm = GetCurrent (page);
769
770                         RegisterScript (ref sm._clientScriptBlocks, control, type, key, null, url, false, RegisteredScriptType.ClientScriptInclude);
771
772                         if (!sm.IsInAsyncPostBack)
773                                 page.ClientScript.RegisterClientScriptInclude (type, key, url);
774                 }
775
776                 public static void RegisterClientScriptResource (Page page, Type type, string resourceName) {
777                         RegisterClientScriptResource ((Control) page, type, resourceName);
778                 }
779
780                 public static void RegisterClientScriptResource (Control control, Type type, string resourceName) {
781                         RegisterClientScriptInclude (control, type, resourceName, ScriptResourceHandler.GetResourceUrl (type.Assembly, resourceName, true));
782                 }
783
784                 void RegisterScriptReference (Control control, ScriptReference script, bool loadScriptsBeforeUI) {
785
786                         bool isDebugMode = IsDeploymentRetail ? false : (script.ScriptModeInternal == ScriptMode.Inherit ? IsDebuggingEnabled : (script.ScriptModeInternal == ScriptMode.Debug));
787                         string url;
788                         if (!String.IsNullOrEmpty (script.Path)) {
789                                 url = GetScriptName (control.ResolveClientUrl (script.Path), isDebugMode, EnableScriptLocalization ? script.ResourceUICultures : null);
790                         }
791                         else if (!String.IsNullOrEmpty (script.Name)) {
792                                 Assembly assembly;
793                                 if (String.IsNullOrEmpty (script.Assembly))
794                                         assembly = typeof (ScriptManager).Assembly;
795                                 else
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);
800                                 else {
801                                         AssemblyName an = assembly.GetName ();
802                                         url = ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (ScriptPath), an.Name, '/', an.Version, '/', name));
803                                 }
804                         }
805                         else {
806                                 throw new InvalidOperationException ("Name and Path cannot both be empty.");
807                         }
808
809                         if (loadScriptsBeforeUI)
810                                 RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
811                         else
812                                 RegisterStartupScript (control, typeof (ScriptManager), url, String.Format ("<script src=\"{0}\" type=\"text/javascript\"></script>", url), false);
813                 }
814
815                 static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures) {
816                         if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
817                                 return releaseName;
818
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));
821
822                         StringBuilder sb = new StringBuilder (releaseName);
823                         sb.Length -= 3;
824                         if (isDebugMode)
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);
829                         sb.Append (".js");
830
831                         return sb.ToString ();
832                 }
833
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);
839                         }
840                         else {
841 #if TARGET_J2EE
842                                 string pathInfo = "/js.invoke";
843 #else
844                                 string pathInfo = "/js";
845 #endif
846                                 string url = String.Concat (control.ResolveClientUrl (serviceReference.Path), pathInfo);
847                                 RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
848                         }
849                 }
850
851                 public void RegisterDataItem (Control control, string dataItem) {
852                         RegisterDataItem (control, dataItem, false);
853                 }
854
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.");
858                         if (control == null)
859                                 throw new ArgumentNullException ("control");
860
861                         if (_dataItems == null)
862                                 _dataItems = new Dictionary<Control, DataItemEntry> ();
863
864                         if (_dataItems.ContainsKey (control))
865                                 throw new ArgumentException (String.Format ("'{0}' already has a data item registered.", control.ID), "control");
866
867                         _dataItems.Add (control, new DataItemEntry (dataItem, isJsonSerialized));
868                 }
869
870                 public void RegisterDispose (Control control, string disposeScript) {
871                         if (control == null)
872                                 throw new ArgumentNullException ("control");
873                         if (disposeScript == null)
874                                 throw new ArgumentNullException ("disposeScript");
875
876                         UpdatePanel updatePanel = GetUpdatePanel (control);
877                         if (updatePanel == null)
878                                 return;
879
880                         if (_disposeScripts == null)
881                                 _disposeScripts = new List<RegisteredDisposeScript> ();
882                         _disposeScripts.Add (new RegisteredDisposeScript (control, disposeScript, updatePanel));
883                 }
884
885                 static UpdatePanel GetUpdatePanel (Control control) {
886                         if (control == null)
887                                 return null;
888
889                         UpdatePanel parent = control.Parent as UpdatePanel;
890                         if (parent != null)
891                                 return parent;
892
893                         return GetUpdatePanel (control.Parent);
894                 }
895
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);
899
900                         if (sm._expandoAttributes == null)
901                                 sm._expandoAttributes = new List<RegisteredExpandoAttribute> ();
902
903                         sm._expandoAttributes.Add (new RegisteredExpandoAttribute (control, controlId, attributeName, attributeValue, encode));
904
905                         if (!sm.IsInAsyncPostBack)
906                                 page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
907                 }
908
909                 public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue) {
910                         RegisterHiddenField ((Control) page, hiddenFieldName, hiddenFieldInitialValue);
911                 }
912
913                 public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
914                         Page page = control.Page;
915                         ScriptManager sm = GetCurrent (page);
916
917                         if (sm._hiddenFields == null)
918                                 sm._hiddenFields = new List<RegisteredHiddenField> ();
919
920                         sm._hiddenFields.Add (new RegisteredHiddenField (control, hiddenFieldName, hiddenFieldInitialValue));
921
922                         if (!sm.IsInAsyncPostBack)
923                                 page.ClientScript.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
924                 }
925
926                 public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script) {
927                         RegisterOnSubmitStatement ((Control) page, type, key, script);
928                 }
929
930                 public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script) {
931                         Page page = control.Page;
932                         ScriptManager sm = GetCurrent (page);
933
934                         RegisterScript (ref sm._onSubmitStatements, control, type, key, script, null, false, RegisteredScriptType.OnSubmitStatement);
935
936                         if (!sm.IsInAsyncPostBack)
937                                 page.ClientScript.RegisterOnSubmitStatement (type, key, script);
938                 }
939
940                 public void RegisterPostBackControl (Control control) {
941                         if (control == null)
942                                 return;
943
944                         if (_postBackControls == null)
945                                 _postBackControls = new List<Control> ();
946
947                         if (_postBackControls.Contains (control))
948                                 return;
949
950                         _postBackControls.Add (control);
951                 }
952
953                 internal void RegisterUpdatePanel (UpdatePanel updatePanel) {
954                         if (_updatePanels == null)
955                                 _updatePanels = new List<UpdatePanel> ();
956
957                         if (_updatePanels.Contains (updatePanel))
958                                 return;
959
960                         _updatePanels.Add (updatePanel);
961                 }
962
963                 public void RegisterScriptDescriptors (IExtenderControl extenderControl) {
964                         if (extenderControl == null)
965                                 return;
966
967                         if (_registeredExtenderControls == null || !_registeredExtenderControls.ContainsKey (extenderControl))
968                                 return;
969
970                         Control targetControl = _registeredExtenderControls [extenderControl];
971                         RegisterScriptDescriptors ((Control) extenderControl, extenderControl.GetScriptDescriptors (targetControl));
972                 }
973
974                 public void RegisterScriptDescriptors (IScriptControl scriptControl) {
975                         if (scriptControl == null)
976                                 return;
977
978                         if (_registeredScriptControls == null || !_registeredScriptControls.Contains (scriptControl))
979                                 return;
980
981                         RegisterScriptDescriptors ((Control) scriptControl, scriptControl.GetScriptDescriptors ());
982                 }
983
984                 void RegisterScriptDescriptors (Control control, IEnumerable<ScriptDescriptor> scriptDescriptors) {
985                         if (scriptDescriptors == null)
986                                 return;
987
988                         StringBuilder sb = new StringBuilder ();
989                         foreach (ScriptDescriptor scriptDescriptor in scriptDescriptors) {
990                                 if (IsMultiForm) {
991                                         scriptDescriptor.FormID = Page.Form.ClientID;
992                                         sb.AppendLine ("Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).add_init(function() {");
993                                 }
994                                 else
995                                         sb.AppendLine ("Sys.Application.add_init(function() {");
996                                 sb.AppendLine (scriptDescriptor.GetScript ());
997                                 sb.AppendLine ("});");
998                         }
999                         string script = sb.ToString ();
1000                         RegisterStartupScript (control, typeof (ScriptDescriptor), script, script, true);
1001                 }
1002
1003                 public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags) {
1004                         RegisterStartupScript ((Control) page, type, key, script, addScriptTags);
1005                 }
1006
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);
1010
1011                         RegisterScript (ref sm._startupScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientStartupScript);
1012
1013                         if (!sm.IsInAsyncPostBack)
1014                                 page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
1015                 }
1016
1017                 public void RegisterScriptControl<TScriptControl> (TScriptControl scriptControl) where TScriptControl : Control, IScriptControl {
1018                         if (scriptControl == null)
1019                                 throw new ArgumentNullException ("scriptControl");
1020
1021                         if (_registeredScriptControls == null)
1022                                 _registeredScriptControls = new List<IScriptControl> ();
1023
1024                         if (!_registeredScriptControls.Contains (scriptControl))
1025                                 _registeredScriptControls.Add (scriptControl);
1026                 }
1027
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");
1033
1034                         if (_registeredExtenderControls == null)
1035                                 _registeredExtenderControls = new Dictionary<IExtenderControl, Control> ();
1036
1037                         if (!_registeredExtenderControls.ContainsKey (extenderControl))
1038                                 _registeredExtenderControls.Add (extenderControl, targetControl);
1039                 }
1040
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> ();
1044
1045                         scriptList.Add (new RegisteredScript (control, type, key, script, url, addScriptTag, scriptType));
1046                 }
1047
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);
1057                                 if (IsMultiForm)
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);
1059                                 else
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>");
1063                         }
1064                         base.Render (writer);
1065                 }
1066
1067                 static string FormatUpdatePanelIDs (List<UpdatePanel> list, bool useSingleQuote) {
1068                         if (list == null || list.Count == 0)
1069                                 return null;
1070
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);
1074                         }
1075                         if (sb.Length > 0)
1076                                 sb.Length--;
1077
1078                         return sb.ToString ();
1079                 }
1080
1081                 static string FormatListIDs<T> (List<T> list, bool useSingleQuote) where T : Control {
1082                         if (list == null || list.Count == 0)
1083                                 return null;
1084
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);
1088                         }
1089                         if (sb.Length > 0)
1090                                 sb.Length--;
1091
1092                         return sb.ToString ();
1093                 }
1094
1095                 public void SetFocus (Control control) {
1096                         if (control == null)
1097                                 throw new ArgumentNullException ("control");
1098
1099                         if (IsInAsyncPostBack) {
1100                                 EnsureFocusClientScript ();
1101                                 _controlIDToFocus = control.ClientID;
1102                         }
1103                         else
1104                                 Page.SetFocus (control);
1105                 }
1106
1107                 public void SetFocus (string clientID) {
1108                         if (String.IsNullOrEmpty (clientID))
1109                                 throw new ArgumentNullException ("control");
1110
1111                         if (IsInAsyncPostBack) {
1112                                 EnsureFocusClientScript ();
1113                                 _controlIDToFocus = clientID;
1114                         }
1115                         else
1116                                 Page.SetFocus (clientID);
1117                 }
1118
1119                 void EnsureFocusClientScript () {
1120 #if     TARGET_DOTNET
1121                         RegisterClientScriptResource (this, typeof (ClientScriptManager), "Focus.js");
1122 #endif
1123                 }
1124
1125                 #region IPostBackDataHandler Members
1126
1127                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection) {
1128                         return LoadPostData (postDataKey, postCollection);
1129                 }
1130
1131                 void IPostBackDataHandler.RaisePostDataChangedEvent () {
1132                         RaisePostDataChangedEvent ();
1133                 }
1134
1135                 #endregion
1136
1137                 internal void WriteCallbackException (TextWriter output, Exception ex, bool writeMessage) {
1138 #if TARGET_DOTNET
1139                         if (ex is HttpUnhandledException)
1140                                 ex = ex.InnerException;
1141 #endif
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);
1147                 }
1148
1149                 static internal void WriteCallbackRedirect (TextWriter output, string redirectUrl) {
1150                         WriteCallbackOutput (output, pageRedirect, null, redirectUrl);
1151                 }
1152
1153                 internal void WriteCallbackPanel (TextWriter output, UpdatePanel panel, StringBuilder panelOutput) {
1154                         if (_panelsToRefresh == null)
1155                                 _panelsToRefresh = new List<UpdatePanel> ();
1156                         _panelsToRefresh.Add (panel);
1157
1158                         WriteCallbackOutput (output, updatePanel, panel.ClientID, panelOutput);
1159                 }
1160
1161                 internal void RegisterChildUpdatePanel (UpdatePanel updatePanel) {
1162                         if (_childUpdatePanels == null)
1163                                 _childUpdatePanels = new List<UpdatePanel> ();
1164                         _childUpdatePanels.Add (updatePanel);
1165                 }
1166
1167                 static void WriteCallbackOutput (TextWriter output, string type, string name, object value) {
1168                         string str = value as string;
1169                         StringBuilder sb = value as StringBuilder;
1170                         int length = 0;
1171                         if (str != null)
1172                                 length = str.Length;
1173                         else if (sb != null)
1174                                 length = sb.Length;
1175
1176                         //output.Write ("{0}|{1}|{2}|{3}|", value == null ? 0 : value.Length, type, name, value);
1177                         output.Write (length);
1178                         output.Write ('|');
1179                         output.Write (type);
1180                         output.Write ('|');
1181                         output.Write (name);
1182                         output.Write ('|');
1183                         for (int i = 0; i < length; i++)
1184                                 if (str != null)
1185                                         output.Write (str [i]);
1186                                 else
1187                                         output.Write (sb [i]);
1188                         output.Write ('|');
1189                 }
1190
1191                 void RenderPageCallback (HtmlTextWriter output, Control container) {
1192                         Page page = (Page) container;
1193
1194                         page.Form.SetRenderMethodDelegate (RenderFormCallback);
1195                         HtmlTextParser parser = new HtmlTextParser (output);
1196                         page.Form.RenderControl (parser);
1197
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 ());
1204                         if (!IsMultiForm)
1205                                 WriteCallbackOutput (output, pageTitle, null, Page.Title);
1206
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);
1211                                 }
1212
1213                         WriteArrayDeclarations (output);
1214                         WriteExpandoAttributes (output);
1215                         WriteScriptBlocks (output, _clientScriptBlocks);
1216                         WriteScriptBlocks (output, _startupScriptBlocks);
1217                         WriteScriptBlocks (output, _onSubmitStatements);
1218                         WriteHiddenFields (output);
1219
1220                         if (!String.IsNullOrEmpty (_controlIDToFocus))
1221                                 WriteCallbackOutput (output, focus, null, _controlIDToFocus);
1222
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);
1228                                 }
1229                 }
1230
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)) {
1236                                                 string value;
1237                                                 if (attr.Encode) {
1238                                                         StringWriter sw = new StringWriter ();
1239                                                         Newtonsoft.Json.JavaScriptUtils.WriteEscapedJavaScriptString (attr.Value, sw);
1240                                                         value = sw.ToString ();
1241                                                 }
1242                                                 else
1243                                                         value = "\"" + attr.Value + "\"";
1244                                                 WriteCallbackOutput (writer, expando, "document.getElementById('" + attr.ControlId + "')['" + attr.Name + "']", value);
1245                                         }
1246                                 }
1247                         }
1248                 }
1249
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);
1256                                 }
1257                         }
1258                 }
1259
1260                 void WriteScriptBlocks (HtmlTextWriter output, List<RegisteredScript> scriptList) {
1261                         if (scriptList == null)
1262                                 return;
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))
1267                                         continue;
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);
1274                                                 else
1275                                                         WriteCallbackOutput (output, scriptBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
1276                                                 break;
1277                                         case RegisteredScriptType.ClientStartupScript:
1278                                                 if (scriptEntry.AddScriptTags)
1279                                                         WriteCallbackOutput (output, scriptStartupBlock, scriptContentNoTags, scriptEntry.Script);
1280                                                 else
1281                                                         WriteCallbackOutput (output, scriptStartupBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
1282                                                 break;
1283                                         case RegisteredScriptType.ClientScriptInclude:
1284                                                 WriteCallbackOutput (output, scriptBlock, scriptPath, scriptEntry.Url);
1285                                                 break;
1286                                         case RegisteredScriptType.OnSubmitStatement:
1287                                                 WriteCallbackOutput (output, onSubmit, null, scriptEntry.Script);
1288                                                 break;
1289                                         }
1290                                 }
1291                         }
1292                 }
1293
1294                 void WriteHiddenFields (HtmlTextWriter output) {
1295                         if (_hiddenFields == null)
1296                                 return;
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))
1301                                         continue;
1302                                 if (Page == field.Control || HasBeenRendered (field.Control)) {
1303                                         registeredFields.Add (field.Name, field);
1304                                         WriteCallbackOutput (output, hiddenField, field.Name, field.InitialValue);
1305                                 }
1306                         }
1307                 }
1308
1309                 static string SerializeScriptBlock (RegisteredScript scriptEntry) {
1310                         try {
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);
1319                                                         }
1320                                                         reader.MoveToContent ();
1321                                                         dic.Add ("text", reader.ReadInnerXml ());
1322                                                         return JavaScriptSerializer.DefaultSerializer.Serialize (dic);
1323                                                 }
1324                                                 break;
1325                                         default:
1326                                                 continue;
1327                                         }
1328                                 }
1329                         }
1330                         catch {
1331                         }
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));
1333                 }
1334
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);
1342                                 }
1343                         }
1344                 }
1345
1346                 internal class AlternativeHtmlTextWriter : HtmlTextWriter
1347                 {
1348                         readonly HtmlTextWriter _responseOutput;
1349
1350                         public HtmlTextWriter ResponseOutput {
1351                                 get { return _responseOutput; }
1352                         }
1353
1354                         public AlternativeHtmlTextWriter (TextWriter writer, HtmlTextWriter responseOutput)
1355                                 : base (writer) {
1356                                 _responseOutput = responseOutput;
1357                         }
1358                 }
1359
1360                 sealed class HtmlTextParser : AlternativeHtmlTextWriter
1361                 {
1362                         bool _done;
1363
1364                         public HtmlTextParser (HtmlTextWriter responseOutput)
1365                                 : base (new TextParser (responseOutput), responseOutput) {
1366                         }
1367
1368                         public override void WriteAttribute (string name, string value) {
1369                                 if (!_done && String.Compare ("action", name, StringComparison.OrdinalIgnoreCase) == 0) {
1370                                         _done = true;
1371                                         ScriptManager.WriteCallbackOutput (ResponseOutput, formAction, null, value);
1372                                         return;
1373                                 }
1374                                 base.WriteAttribute (name, value);
1375                         }
1376                 }
1377
1378                 sealed class TextParser : TextWriter
1379                 {
1380                         int _state;
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;
1387
1388                         public override Encoding Encoding {
1389                                 get { return Encoding.UTF8; }
1390                         }
1391
1392                         public TextParser (HtmlTextWriter responseOutput) {
1393                                 _responseOutput = responseOutput;
1394                         }
1395
1396                         public override void Write (char value) {
1397                                 switch (_state) {
1398                                 case 0:
1399                                         ParseBeginTag (value);
1400                                         break;
1401                                 case 1:
1402                                         ParseAttributeName (value);
1403                                         break;
1404                                 case 2:
1405                                         ParseAttributeValue (value);
1406                                         break;
1407                                 }
1408                         }
1409
1410                         private void ParseAttributeValue (char value) {
1411                                 switch (value) {
1412                                 case '>':
1413                                         ResetState ();
1414                                         break;
1415                                 case '"':
1416                                         _currentField.Add (_currentAttribute, _sb.ToString ());
1417                                         _state = 1;
1418                                         _sb.Length = 0;
1419                                         ProbeWriteOutput ();
1420                                         break;
1421                                 default:
1422                                         _sb.Append (value);
1423                                         break;
1424                                 }
1425                         }
1426
1427                         private void ParseAttributeName (char value) {
1428                                 switch (value) {
1429                                 case '>':
1430                                         ResetState ();
1431                                         break;
1432                                 case ' ':
1433                                 case '=':
1434                                         break;
1435                                 case '"':
1436                                         _currentAttribute = _sb.ToString ();
1437                                         _state = 2;
1438                                         _sb.Length = 0;
1439                                         break;
1440                                 default:
1441                                         _sb.Append (value);
1442                                         break;
1443                                 }
1444                         }
1445
1446                         void ParseBeginTag (char value) {
1447                                 switch (_charState) {
1448                                 case nullCharState:
1449                                         if (value == '<')
1450                                                 _charState = value;
1451                                         break;
1452                                 case '<':
1453                                         if (value == 'i')
1454                                                 _charState = value;
1455                                         else
1456                                                 ResetState ();
1457                                         break;
1458                                 case 'i':
1459                                         if (value == 'n')
1460                                                 _charState = value;
1461                                         else
1462                                                 ResetState ();
1463                                         break;
1464                                 case 'n':
1465                                         if (value == 'p')
1466                                                 _charState = value;
1467                                         else
1468                                                 ResetState ();
1469                                         break;
1470                                 case 'p':
1471                                         if (value == 'u')
1472                                                 _charState = value;
1473                                         else
1474                                                 ResetState ();
1475                                         break;
1476                                 case 'u':
1477                                         if (value == 't')
1478                                                 _charState = value;
1479                                         else
1480                                                 ResetState ();
1481                                         break;
1482                                 case 't':
1483                                         if (value == ' ') {
1484                                                 _state = 1;
1485                                                 _currentField = new Dictionary<string, string> ();
1486                                         }
1487                                         else
1488                                                 ResetState ();
1489                                         break;
1490                                 }
1491                         }
1492
1493                         private void ResetState () {
1494                                 _charState = nullCharState;
1495                                 _state = 0;
1496                                 _sb.Length = 0;
1497                         }
1498
1499                         private void ProbeWriteOutput () {
1500                                 if (!_currentField.ContainsKey ("name"))
1501                                         return;
1502                                 if (!_currentField.ContainsKey ("value"))
1503                                         return;
1504
1505                                 string value = _currentField ["value"];
1506                                 if (String.IsNullOrEmpty (value))
1507                                         return;
1508
1509                                 ScriptManager.WriteCallbackOutput (_responseOutput, hiddenField, _currentField ["name"], HttpUtility.HtmlDecode (value));
1510                         }
1511                 }
1512
1513                 sealed class HtmlDropWriter : AlternativeHtmlTextWriter
1514                 {
1515                         public HtmlDropWriter (HtmlTextWriter responseOutput)
1516                                 : base (new DropWriter (), responseOutput) {
1517                         }
1518                 }
1519
1520                 sealed class DropWriter : TextWriter
1521                 {
1522                         public override Encoding Encoding {
1523                                 get { return Encoding.UTF8; }
1524                         }
1525                 }
1526
1527                 sealed class CultureInfoSerializer : JavaScriptSerializer.LazyDictionary
1528                 {
1529                         readonly CultureInfo _ci;
1530                         public CultureInfoSerializer (CultureInfo ci) {
1531                                 if (ci == null)
1532                                         throw new ArgumentNullException ("ci");
1533                                 _ci = ci;
1534                         }
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);
1539                         }
1540                 }
1541
1542                 sealed class ScriptReferenceEntry
1543                 {
1544                         readonly Control _control;
1545                         readonly ScriptReference _scriptReference;
1546                         readonly bool _loadBeforeUI;
1547
1548                         public Control Control { get { return _control; } }
1549                         public ScriptReference ScriptReference { get { return _scriptReference; } }
1550                         public bool LoadScriptsBeforeUI { get { return _loadBeforeUI; } }
1551
1552                         public ScriptReferenceEntry (Control control, ScriptReference scriptReference, bool loadBeforeUI) {
1553                                 _control = control;
1554                                 _scriptReference = scriptReference;
1555                                 _loadBeforeUI = loadBeforeUI;
1556                         }
1557                 }
1558
1559                 sealed class DataItemEntry
1560                 {
1561                         readonly string _dataItem;
1562                         readonly bool _isJsonSerialized;
1563
1564                         public string DataItem { get { return _dataItem; } }
1565                         public bool IsJsonSerialized { get { return _isJsonSerialized; } }
1566
1567                         public DataItemEntry (string dataItem, bool isJsonSerialized) {
1568                                 _dataItem = dataItem;
1569                                 _isJsonSerialized = isJsonSerialized;
1570                         }
1571                 }
1572
1573                 internal void RegisterProxy (ScriptManagerProxy scriptManagerProxy) {
1574                         if (_proxies == null)
1575                                 _proxies = new List<ScriptManagerProxy> ();
1576
1577                         _proxies.Add (scriptManagerProxy);
1578                 }
1579
1580                 #region IScriptManager Members
1581
1582                 void IScriptManager.RegisterOnSubmitStatementExternal (Control control, Type type, string key, string script) {
1583                         RegisterOnSubmitStatement (control, type, key, script);
1584                 }
1585
1586                 void IScriptManager.RegisterExpandoAttributeExternal (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
1587                         RegisterExpandoAttribute (control, controlId, attributeName, attributeValue, encode);
1588                 }
1589
1590                 void IScriptManager.RegisterHiddenFieldExternal (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
1591                         RegisterHiddenField (control, hiddenFieldName, hiddenFieldInitialValue);
1592                 }
1593
1594                 void IScriptManager.RegisterStartupScriptExternal (Control control, Type type, string key, string script, bool addScriptTags) {
1595                         RegisterStartupScript (control, type, key, script, addScriptTags);
1596                 }
1597
1598                 void IScriptManager.RegisterArrayDeclarationExternal (Control control, string arrayName, string arrayValue) {
1599                         RegisterArrayDeclaration (control, arrayName, arrayValue);
1600                 }
1601
1602                 void IScriptManager.RegisterClientScriptBlockExternal (Control control, Type type, string key, string script, bool addScriptTags) {
1603                         RegisterClientScriptBlock (control, type, key, script, addScriptTags);
1604                 }
1605
1606                 void IScriptManager.RegisterClientScriptIncludeExternal (Control control, Type type, string key, string url) {
1607                         RegisterClientScriptInclude (control, type, key, url);
1608                 }
1609
1610                 void IScriptManager.RegisterClientScriptResourceExternal (Control control, Type type, string resourceName) {
1611                         RegisterClientScriptResource (control, type, resourceName);
1612                 }
1613
1614                 #endregion
1615         }
1616 }