| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790 |
- //
- // ScriptManager.cs
- //
- // Author:
- // Igor Zelmanovich <[email protected]>
- //
- // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel;
- using System.Security.Permissions;
- using System.Collections.Specialized;
- using System.Collections;
- using System.Web.Handlers;
- using System.Reflection;
- using System.Web.Configuration;
- using System.Web.UI.HtmlControls;
- using System.IO;
- using System.Globalization;
- using System.Threading;
- using System.Web.Script.Serialization;
- using System.Web.Script.Services;
- using System.Xml;
- using System.Collections.ObjectModel;
- namespace System.Web.UI
- {
- [ParseChildrenAttribute (true)]
- [DefaultPropertyAttribute ("Scripts")]
- [DesignerAttribute ("System.Web.UI.Design.ScriptManagerDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
- [NonVisualControlAttribute]
- [PersistChildrenAttribute (false)]
- [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- public class ScriptManager : Control, IPostBackDataHandler, IScriptManager
- {
- // the keywords are used in fomatting async response
- const string updatePanel = "updatePanel";
- const string hiddenField = "hiddenField";
- const string arrayDeclaration = "arrayDeclaration";
- const string scriptBlock = "scriptBlock";
- const string scriptStartupBlock = "scriptStartupBlock";
- const string expando = "expando";
- const string onSubmit = "onSubmit";
- const string asyncPostBackControlIDs = "asyncPostBackControlIDs";
- const string postBackControlIDs = "postBackControlIDs";
- const string updatePanelIDs = "updatePanelIDs";
- const string asyncPostBackTimeout = "asyncPostBackTimeout";
- const string childUpdatePanelIDs = "childUpdatePanelIDs";
- const string panelsToRefreshIDs = "panelsToRefreshIDs";
- const string formAction = "formAction";
- const string dataItem = "dataItem";
- const string dataItemJson = "dataItemJson";
- const string scriptDispose = "scriptDispose";
- const string pageRedirect = "pageRedirect";
- const string error = "error";
- const string pageTitle = "pageTitle";
- const string focus = "focus";
- const string scriptContentNoTags = "ScriptContentNoTags";
- const string scriptContentWithTags = "ScriptContentWithTags";
- const string scriptPath = "ScriptPath";
- static readonly object ScriptManagerKey = typeof (IScriptManager);
- int _asyncPostBackTimeout = 90;
- List<Control> _asyncPostBackControls;
- List<Control> _postBackControls;
- List<UpdatePanel> _childUpdatePanels;
- List<UpdatePanel> _panelsToRefresh;
- List<UpdatePanel> _updatePanels;
- ScriptReferenceCollection _scripts;
- ServiceReferenceCollection _services;
- bool _isInAsyncPostBack;
- bool _isInPartialRendering;
- string _asyncPostBackSourceElementID;
- ScriptMode _scriptMode = ScriptMode.Auto;
- bool _enableScriptGlobalization;
- bool _enableScriptLocalization;
- string _scriptPath;
- List<RegisteredScript> _clientScriptBlocks;
- List<RegisteredScript> _startupScriptBlocks;
- List<RegisteredScript> _onSubmitStatements;
- List<RegisteredArrayDeclaration> _arrayDeclarations;
- List<RegisteredExpandoAttribute> _expandoAttributes;
- List<RegisteredHiddenField> _hiddenFields;
- List<IScriptControl> _registeredScriptControls;
- Dictionary<IExtenderControl, Control> _registeredExtenderControls;
- bool? _supportsPartialRendering;
- bool _enablePartialRendering = true;
- bool _init;
- string _panelToRefreshID;
- Dictionary<Control, DataItemEntry> _dataItems;
- bool _enablePageMethods;
- string _controlIDToFocus;
- bool _allowCustomErrorsRedirect = true;
- string _asyncPostBackErrorMessage;
- List<RegisteredDisposeScript> _disposeScripts;
- List<ScriptReferenceEntry> _scriptToRegister;
- bool _loadScriptsBeforeUI = true;
- AuthenticationServiceManager _authenticationService;
- ProfileServiceManager _profileService;
- List<ScriptManagerProxy> _proxies;
- [DefaultValue (true)]
- [Category ("Behavior")]
- public bool AllowCustomErrorsRedirect {
- get {
- return _allowCustomErrorsRedirect;
- }
- set {
- _allowCustomErrorsRedirect = value;
- }
- }
- [Category ("Behavior")]
- [DefaultValue ("")]
- public string AsyncPostBackErrorMessage {
- get {
- if (String.IsNullOrEmpty (_asyncPostBackErrorMessage))
- return String.Empty;
- return _asyncPostBackErrorMessage;
- }
- set {
- _asyncPostBackErrorMessage = value;
- }
- }
- [Browsable (false)]
- public string AsyncPostBackSourceElementID {
- get {
- if (_asyncPostBackSourceElementID == null)
- return String.Empty;
- return _asyncPostBackSourceElementID;
- }
- }
- [DefaultValue (90)]
- [Category ("Behavior")]
- public int AsyncPostBackTimeout {
- get {
- return _asyncPostBackTimeout;
- }
- set {
- _asyncPostBackTimeout = value;
- }
- }
- [Category ("Behavior")]
- [MergableProperty (false)]
- [DefaultValue ("")]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public AuthenticationServiceManager AuthenticationService {
- get {
- if (_authenticationService == null)
- _authenticationService = new AuthenticationServiceManager ();
- return _authenticationService;
- }
- }
- [Category ("Behavior")]
- [DefaultValue (false)]
- public bool EnablePageMethods {
- get {
- return _enablePageMethods;
- }
- set {
- _enablePageMethods = value;
- }
- }
- [DefaultValue (true)]
- [Category ("Behavior")]
- public bool EnablePartialRendering {
- get {
- return _enablePartialRendering;
- }
- set {
- if (_init)
- throw new InvalidOperationException ();
- _enablePartialRendering = value;
- }
- }
- [DefaultValue (false)]
- [Category ("Behavior")]
- public bool EnableScriptGlobalization {
- get {
- return _enableScriptGlobalization;
- }
- set {
- _enableScriptGlobalization = value;
- }
- }
- [Category ("Behavior")]
- [DefaultValue (false)]
- public bool EnableScriptLocalization {
- get {
- return _enableScriptLocalization;
- }
- set {
- _enableScriptLocalization = value;
- }
- }
- [Browsable (false)]
- public bool IsDebuggingEnabled {
- get {
- if (IsDeploymentRetail)
- return false;
- CompilationSection compilation = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
- if (!compilation.Debug && (ScriptMode == ScriptMode.Auto || ScriptMode == ScriptMode.Inherit))
- return false;
- if (ScriptMode == ScriptMode.Release)
- return false;
- return true;
- }
- }
- internal bool IsDeploymentRetail {
- get {
- #if TARGET_J2EE
- return false;
- #else
- DeploymentSection deployment = (DeploymentSection) WebConfigurationManager.GetSection ("system.web/deployment");
- return deployment.Retail;
- #endif
- }
- }
- [Browsable (false)]
- public bool IsInAsyncPostBack {
- get {
- return _isInAsyncPostBack;
- }
- }
- internal bool IsInPartialRendering {
- get {
- return _isInPartialRendering;
- }
- set {
- _isInPartialRendering = value;
- }
- }
- [Category ("Behavior")]
- [DefaultValue (true)]
- public bool LoadScriptsBeforeUI {
- get {
- return _loadScriptsBeforeUI;
- }
- set {
- _loadScriptsBeforeUI = value;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [DefaultValue ("")]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [Category ("Behavior")]
- [MergableProperty (false)]
- public ProfileServiceManager ProfileService {
- get {
- if (_profileService == null)
- _profileService = new ProfileServiceManager ();
- return _profileService;
- }
- }
- [Category ("Behavior")]
- #if TARGET_J2EE
- [MonoLimitation ("The 'Auto' value is the same as 'Debug'.")]
- #endif
- public ScriptMode ScriptMode {
- get {
- return _scriptMode;
- }
- set {
- if (value == ScriptMode.Inherit)
- value = ScriptMode.Auto;
- _scriptMode = value;
- }
- }
- [DefaultValue ("")]
- [Category ("Behavior")]
- public string ScriptPath {
- get {
- if (_scriptPath == null)
- return String.Empty;
- return _scriptPath;
- }
- set {
- _scriptPath = value;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [DefaultValue ("")]
- [Category ("Behavior")]
- [MergableProperty (false)]
- public ScriptReferenceCollection Scripts {
- get {
- if (_scripts == null)
- _scripts = new ScriptReferenceCollection ();
- return _scripts;
- }
- }
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [DefaultValue ("")]
- [MergableProperty (false)]
- [Category ("Behavior")]
- public ServiceReferenceCollection Services {
- get {
- if (_services == null)
- _services = new ServiceReferenceCollection ();
- return _services;
- }
- }
- [DefaultValue (true)]
- [Browsable (false)]
- public bool SupportsPartialRendering {
- get {
- if (!_supportsPartialRendering.HasValue)
- _supportsPartialRendering = CheckSupportsPartialRendering ();
- return _supportsPartialRendering.Value;
- }
- set {
- if (_init)
- throw new InvalidOperationException ();
- if (!EnablePartialRendering && value)
- throw new InvalidOperationException ("The SupportsPartialRendering property cannot be set when EnablePartialRendering is false.");
- _supportsPartialRendering = value;
- }
- }
- bool CheckSupportsPartialRendering () {
- if (!EnablePartialRendering)
- return false;
- // TODO: consider browser capabilities
- return true;
- }
- [EditorBrowsable (EditorBrowsableState.Never)]
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public override bool Visible {
- get {
- return true;
- }
- set {
- throw new NotImplementedException ();
- }
- }
- [Category ("Action")]
- public event EventHandler<AsyncPostBackErrorEventArgs> AsyncPostBackError;
- [Category ("Action")]
- public event EventHandler<ScriptReferenceEventArgs> ResolveScriptReference;
- public static ScriptManager GetCurrent (Page page) {
- if (page == null)
- throw new ArgumentNullException ("page");
- return (ScriptManager) page.Items [ScriptManagerKey];
- }
- static void SetCurrent (Page page, ScriptManager instance) {
- page.Items [ScriptManagerKey] = instance;
- page.ClientScript.RegisterWebFormClientScript ();
- }
- UpdatePanel FindPanelWithId (string id)
- {
- if (_updatePanels == null)
- return null;
- foreach (UpdatePanel panel in _updatePanels) {
- if (panel.ID == id)
- return panel;
- }
- return null;
- }
-
- protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection) {
- _isInAsyncPostBack = true;
- string arg = postCollection [postDataKey];
- if (!String.IsNullOrEmpty (arg)) {
- string [] args = arg.Split ('|');
- switch (args.Length) {
- case 1:
- _asyncPostBackSourceElementID = args [0];
- break;
- case 2:
- _panelToRefreshID = args [0];
- _asyncPostBackSourceElementID = args [1];
- break;
- default: // "impossible situation"
- throw new InvalidOperationException ("Unexpected format of post data.");
- }
-
- return true;
- }
- return false;
- }
- protected internal virtual void OnAsyncPostBackError (AsyncPostBackErrorEventArgs e) {
- if (AsyncPostBackError != null)
- AsyncPostBackError (this, e);
- }
- protected override void OnInit (EventArgs e) {
- base.OnInit (e);
- if (GetCurrent (Page) != null)
- throw new InvalidOperationException ("Only one instance of a ScriptManager can be added to the page.");
- SetCurrent (Page, this);
- Page.Error += new EventHandler (OnPageError);
- _init = true;
- }
- void OnPageError (object sender, EventArgs e) {
- if (IsInAsyncPostBack)
- OnAsyncPostBackError (new AsyncPostBackErrorEventArgs (Context.Error));
- }
- protected override void OnPreRender (EventArgs e) {
- base.OnPreRender (e);
- Page.PreRenderComplete += new EventHandler (OnPreRenderComplete);
- if (IsInAsyncPostBack) {
- Page.SetRenderMethodDelegate (RenderPageCallback);
- }
- else {
- if (EnableScriptGlobalization) {
- CultureInfo culture = Thread.CurrentThread.CurrentCulture;
- string script = String.Format ("var __cultureInfo = '{0}';", JavaScriptSerializer.DefaultSerializer.Serialize (new CultureInfoSerializer (culture)));
- RegisterClientScriptBlock (this, typeof (ScriptManager), "ScriptGlobalization", script, true);
- }
- // Register dispose script
- if (_disposeScripts != null && _disposeScripts.Count > 0) {
- StringBuilder sb = new StringBuilder ();
- sb.AppendLine ();
- for (int i = 0; i < _disposeScripts.Count; i++) {
- RegisteredDisposeScript entry = _disposeScripts [i];
- if (IsMultiForm)
- sb.Append ("Sys.WebForms.PageRequestManager.getInstance($get(\"" + Page.Form.ClientID + "\"))._registerDisposeScript(\"");
- else
- sb.Append ("Sys.WebForms.PageRequestManager.getInstance()._registerDisposeScript(\"");
- sb.Append (entry.UpdatePanel.ClientID);
- sb.Append ("\", ");
- sb.Append (JavaScriptSerializer.DefaultSerializer.Serialize (entry.Script)); //JavaScriptSerializer.Serialize used escape script literal
- sb.AppendLine (");");
- }
- RegisterStartupScript (this, typeof (ExtenderControl), "disposeScripts;", sb.ToString (), true);
- }
- #if TARGET_DOTNET
- // to cause webform client script being included
- Page.ClientScript.GetPostBackEventReference (new PostBackOptions (this, null, null, false, false, false, true, true, null));
- #else
- Page.ClientScript.GetPostBackEventReference (this, null);
- #endif
- }
- }
- void OnPreRenderComplete (object sender, EventArgs e) {
- // Resolve Scripts
- ScriptReference ajaxScript = new ScriptReference ("MicrosoftAjax.js", String.Empty);
- ajaxScript.NotifyScriptLoaded = false;
- OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxScript));
- ScriptReference ajaxWebFormsScript = new ScriptReference ("MicrosoftAjaxWebForms.js", String.Empty);
- ajaxWebFormsScript.NotifyScriptLoaded = false;
- OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxWebFormsScript));
- ScriptReference ajaxExtensionScript = null;
- ScriptReference ajaxWebFormsExtensionScript = null;
- if (IsMultiForm) {
- ajaxExtensionScript = new ScriptReference ("MicrosoftAjaxExtension.js", String.Empty);
- OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxExtensionScript));
- ajaxWebFormsExtensionScript = new ScriptReference ("MicrosoftAjaxWebFormsExtension.js", String.Empty);
- OnResolveScriptReference (new ScriptReferenceEventArgs (ajaxWebFormsExtensionScript));
- }
- foreach (ScriptReferenceEntry script in GetScriptReferences ()) {
- OnResolveScriptReference (new ScriptReferenceEventArgs (script.ScriptReference));
- if (_scriptToRegister == null)
- _scriptToRegister = new List<ScriptReferenceEntry> ();
- _scriptToRegister.Add (script);
- }
- if (!IsInAsyncPostBack) {
- // Register Ajax framework script.
- RegisterScriptReference (this, ajaxScript, true);
- if (IsMultiForm) {
- RegisterScriptReference (this, ajaxExtensionScript, true);
- RegisterClientScriptBlock (this, typeof (ScriptManager), "Sys.Application", "\nSys.Application._initialize(document.getElementById('" + Page.Form.ClientID + "'));\n", true);
- }
- StringBuilder sb = new StringBuilder ();
- sb.AppendLine ("if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');");
- ScriptingProfileServiceSection profileService = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
- if (profileService != null && profileService.Enabled)
- sb.AppendLine ("Sys.Services._ProfileService.DefaultWebServicePath = '" + ResolveClientUrl ("~" + System.Web.Script.Services.ProfileService.DefaultWebServicePath) + "';");
- string profileServicePath = GetProfileServicePath ();
- if (!String.IsNullOrEmpty (profileServicePath))
- sb.AppendLine ("Sys.Services.ProfileService.set_path('" + profileServicePath + "');");
- ScriptingAuthenticationServiceSection authenticationService = (ScriptingAuthenticationServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/authenticationService");
- if (authenticationService != null && authenticationService.Enabled) {
- sb.AppendLine ("Sys.Services._AuthenticationService.DefaultWebServicePath = '" + ResolveClientUrl ("~/Authentication_JSON_AppService.axd") + "';");
- if (Page.User.Identity.IsAuthenticated)
- sb.AppendLine ("Sys.Services.AuthenticationService._setAuthenticated(true);");
- }
- string authenticationServicePath = GetAuthenticationServicePath ();
- if (!String.IsNullOrEmpty (authenticationServicePath))
- sb.AppendLine ("Sys.Services.AuthenticationService.set_path('" + authenticationServicePath + "');");
- RegisterClientScriptBlock (this, typeof (ScriptManager), "Framework", sb.ToString (), true);
- RegisterScriptReference (this, ajaxWebFormsScript, true);
- if (IsMultiForm)
- RegisterScriptReference (this, ajaxWebFormsExtensionScript, true);
- }
-
- // Register Scripts
- if (_scriptToRegister != null)
- for (int i = 0; i < _scriptToRegister.Count; i++)
- RegisterScriptReference (_scriptToRegister [i].Control, _scriptToRegister [i].ScriptReference, _scriptToRegister [i].LoadScriptsBeforeUI);
- if (!IsInAsyncPostBack) {
- // Register services
- if (_services != null && _services.Count > 0) {
- for (int i = 0; i < _services.Count; i++) {
- RegisterServiceReference (this, _services [i]);
- }
- }
- if (_proxies != null && _proxies.Count > 0) {
- for (int i = 0; i < _proxies.Count; i++) {
- ScriptManagerProxy proxy = _proxies [i];
- for (int j = 0; j < proxy.Services.Count; j++) {
- RegisterServiceReference (proxy, proxy.Services [j]);
- }
- }
- }
- if (EnablePageMethods) {
- LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (Page.GetType (), Page.Request.FilePath);
- RegisterClientScriptBlock (this, typeof (ScriptManager), "PageMethods", logicalTypeInfo.Proxy, true);
- }
- // Register startup script
- if (IsMultiForm)
- RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", "Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).initialize();\n", true);
- else
- RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", "Sys.Application.initialize();\n", true);
- }
- }
- string GetProfileServicePath () {
- if (_profileService != null && !String.IsNullOrEmpty (_profileService.Path))
- return ResolveClientUrl (_profileService.Path);
- if (_proxies != null && _proxies.Count > 0)
- for (int i = 0; i < _proxies.Count; i++)
- if (!String.IsNullOrEmpty (_proxies [i].ProfileService.Path))
- return _proxies [i].ResolveClientUrl (_proxies [i].ProfileService.Path);
- return null;
- }
- string GetAuthenticationServicePath () {
- if (_authenticationService != null && !String.IsNullOrEmpty (_authenticationService.Path))
- return ResolveClientUrl (_authenticationService.Path);
- if (_proxies != null && _proxies.Count > 0)
- for (int i = 0; i < _proxies.Count; i++)
- if (!String.IsNullOrEmpty (_proxies [i].AuthenticationService.Path))
- return _proxies [i].ResolveClientUrl (_proxies [i].AuthenticationService.Path);
- return null;
- }
- public ReadOnlyCollection<RegisteredArrayDeclaration> GetRegisteredArrayDeclarations () {
- if (_arrayDeclarations == null)
- _arrayDeclarations = new List<RegisteredArrayDeclaration> ();
- return new ReadOnlyCollection<RegisteredArrayDeclaration> (_arrayDeclarations);
- }
- public ReadOnlyCollection<RegisteredScript> GetRegisteredClientScriptBlocks () {
- if (_clientScriptBlocks == null)
- _clientScriptBlocks = new List<RegisteredScript> ();
- return new ReadOnlyCollection<RegisteredScript> (_clientScriptBlocks);
- }
- public ReadOnlyCollection<RegisteredDisposeScript> GetRegisteredDisposeScripts () {
- if (_disposeScripts == null)
- _disposeScripts = new List<RegisteredDisposeScript> ();
- return new ReadOnlyCollection<RegisteredDisposeScript> (_disposeScripts);
- }
- public ReadOnlyCollection<RegisteredExpandoAttribute> GetRegisteredExpandoAttributes () {
- if (_expandoAttributes == null)
- _expandoAttributes = new List<RegisteredExpandoAttribute> ();
- return new ReadOnlyCollection<RegisteredExpandoAttribute> (_expandoAttributes);
- }
- public ReadOnlyCollection<RegisteredHiddenField> GetRegisteredHiddenFields () {
- if (_hiddenFields == null)
- _hiddenFields = new List<RegisteredHiddenField> ();
- return new ReadOnlyCollection<RegisteredHiddenField> (_hiddenFields);
- }
- public ReadOnlyCollection<RegisteredScript> GetRegisteredOnSubmitStatements () {
- if (_onSubmitStatements == null)
- _onSubmitStatements = new List<RegisteredScript> ();
- return new ReadOnlyCollection<RegisteredScript> (_onSubmitStatements);
- }
- public ReadOnlyCollection<RegisteredScript> GetRegisteredStartupScripts () {
- if (_startupScriptBlocks == null)
- _startupScriptBlocks = new List<RegisteredScript> ();
- return new ReadOnlyCollection<RegisteredScript> (_startupScriptBlocks);
- }
- #if TARGET_J2EE
- bool _isMultiForm = false;
- bool _isMultiFormInited = false;
- bool IsMultiForm {
- get {
- if (!_isMultiFormInited) {
- string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
- _isMultiForm = isMultiForm != null ? Boolean.Parse (isMultiForm) : false;
- _isMultiFormInited = true;
- }
- return _isMultiForm;
- }
- }
- #else
- bool IsMultiForm {
- get { return false; }
- }
- #endif
- bool PanelRequiresUpdate (UpdatePanel panel)
- {
- if (panel == null || _panelsToRefresh == null || _panelsToRefresh.Count == 0)
- return false;
- return _panelsToRefresh.Contains (panel);
- }
-
- bool HasBeenRendered (Control control)
- {
- if (control == null)
- return false;
- UpdatePanel panel = control as UpdatePanel;
- if (PanelRequiresUpdate (panel))
- return true;
- return HasBeenRendered (control.Parent);
- }
- IEnumerable<ScriptReferenceEntry> GetScriptReferences () {
- if (_scripts != null && _scripts.Count > 0) {
- for (int i = 0; i < _scripts.Count; i++) {
- yield return new ScriptReferenceEntry (this, _scripts [i], LoadScriptsBeforeUI);
- }
- }
- if (_proxies != null && _proxies.Count > 0) {
- for (int i = 0; i < _proxies.Count; i++) {
- ScriptManagerProxy proxy = _proxies [i];
- for (int j = 0; j < proxy.Scripts.Count; j++)
- yield return new ScriptReferenceEntry (proxy, proxy.Scripts [j], LoadScriptsBeforeUI);
- }
- }
- if (_registeredScriptControls != null && _registeredScriptControls.Count > 0) {
- for (int i = 0; i < _registeredScriptControls.Count; i++) {
- IEnumerable<ScriptReference> scripts = _registeredScriptControls [i].GetScriptReferences ();
- if (scripts != null)
- foreach (ScriptReference s in scripts)
- yield return new ScriptReferenceEntry ((Control) _registeredScriptControls [i], s, LoadScriptsBeforeUI);
- }
- }
- if (_registeredExtenderControls != null && _registeredExtenderControls.Count > 0) {
- foreach (IExtenderControl ex in _registeredExtenderControls.Keys) {
- IEnumerable<ScriptReference> scripts = ex.GetScriptReferences ();
- if (scripts != null)
- foreach (ScriptReference s in scripts)
- yield return new ScriptReferenceEntry ((Control) ex, s, LoadScriptsBeforeUI);
- }
- }
- }
- protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e) {
- if (ResolveScriptReference != null)
- ResolveScriptReference (this, e);
- }
- protected virtual void RaisePostDataChangedEvent ()
- {
- // Why override?
- }
- public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue) {
- RegisterArrayDeclaration ((Control) page, arrayName, arrayValue);
- }
- public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- if (sm._arrayDeclarations == null)
- sm._arrayDeclarations = new List<RegisteredArrayDeclaration> ();
- sm._arrayDeclarations.Add (new RegisteredArrayDeclaration (control, arrayName, arrayValue));
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterArrayDeclaration (arrayName, arrayValue);
- }
- public void RegisterAsyncPostBackControl (Control control) {
- if (control == null)
- return;
- if (_asyncPostBackControls == null)
- _asyncPostBackControls = new List<Control> ();
- if (_asyncPostBackControls.Contains (control))
- return;
- _asyncPostBackControls.Add (control);
- }
- public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags) {
- RegisterClientScriptBlock ((Control) page, type, key, script, addScriptTags);
- }
- public static void RegisterClientScriptBlock (Control control, Type type, string key, string script, bool addScriptTags) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- RegisterScript (ref sm._clientScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientScriptBlock);
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterClientScriptBlock (type, key, script, addScriptTags);
- }
- public static void RegisterClientScriptInclude (Page page, Type type, string key, string url) {
- RegisterClientScriptInclude ((Control) page, type, key, url);
- }
- public static void RegisterClientScriptInclude (Control control, Type type, string key, string url) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- RegisterScript (ref sm._clientScriptBlocks, control, type, key, null, url, false, RegisteredScriptType.ClientScriptInclude);
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterClientScriptInclude (type, key, url);
- }
- public static void RegisterClientScriptResource (Page page, Type type, string resourceName) {
- RegisterClientScriptResource ((Control) page, type, resourceName);
- }
- public static void RegisterClientScriptResource (Control control, Type type, string resourceName) {
- RegisterClientScriptInclude (control, type, resourceName, ScriptResourceHandler.GetResourceUrl (type.Assembly, resourceName, true));
- }
- void RegisterScriptReference (Control control, ScriptReference script, bool loadScriptsBeforeUI)
- {
- string scriptPath = script.Path;
- string url = script.GetUrl (this, false);
- if (control != this && !String.IsNullOrEmpty (scriptPath))
- url = control.ResolveClientUrl (url);
-
- if (loadScriptsBeforeUI)
- RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
- else
- RegisterStartupScript (control, typeof (ScriptManager), url, String.Format ("<script src=\"{0}\" type=\"text/javascript\"></script>", url), false);
- }
- void RegisterServiceReference (Control control, ServiceReference serviceReference)
- {
- if (serviceReference.InlineScript) {
- string url = control.ResolveUrl (serviceReference.Path);
- Type type = WebServiceParser.GetCompiledType (url, Context);
- if (type != null) {
- object[] attributes = type.GetCustomAttributes (typeof (ScriptServiceAttribute), true);
- if (attributes.Length == 0)
- throw new InvalidOperationException ("Only Web services with a [ScriptService] attribute on the class definition can be called from script.");
- }
-
- LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (type, url);
- RegisterClientScriptBlock (control, typeof (ScriptManager), url, logicalTypeInfo.Proxy, true);
- }
- else {
- #if TARGET_J2EE
- string pathInfo = "/js.invoke";
- #else
- string pathInfo = "/js";
- if (IsDebuggingEnabled)
- pathInfo += "debug";
- #endif
- string url = String.Concat (control.ResolveClientUrl (serviceReference.Path), pathInfo);
- RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
- }
- }
- public void RegisterDataItem (Control control, string dataItem) {
- RegisterDataItem (control, dataItem, false);
- }
- public void RegisterDataItem (Control control, string dataItem, bool isJsonSerialized) {
- if (!IsInAsyncPostBack)
- throw new InvalidOperationException ("RegisterDataItem can only be called during an async postback.");
- if (control == null)
- throw new ArgumentNullException ("control");
- if (_dataItems == null)
- _dataItems = new Dictionary<Control, DataItemEntry> ();
- if (_dataItems.ContainsKey (control))
- throw new ArgumentException (String.Format ("'{0}' already has a data item registered.", control.ID), "control");
- _dataItems.Add (control, new DataItemEntry (dataItem, isJsonSerialized));
- }
- public void RegisterDispose (Control control, string disposeScript) {
- if (control == null)
- throw new ArgumentNullException ("control");
- if (disposeScript == null)
- throw new ArgumentNullException ("disposeScript");
- UpdatePanel updatePanel = GetUpdatePanel (control);
- if (updatePanel == null)
- return;
- if (_disposeScripts == null)
- _disposeScripts = new List<RegisteredDisposeScript> ();
- _disposeScripts.Add (new RegisteredDisposeScript (control, disposeScript, updatePanel));
- }
- static UpdatePanel GetUpdatePanel (Control control) {
- if (control == null)
- return null;
- UpdatePanel parent = control.Parent as UpdatePanel;
- if (parent != null)
- return parent;
- return GetUpdatePanel (control.Parent);
- }
- public static void RegisterExpandoAttribute (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- if (sm._expandoAttributes == null)
- sm._expandoAttributes = new List<RegisteredExpandoAttribute> ();
- sm._expandoAttributes.Add (new RegisteredExpandoAttribute (control, controlId, attributeName, attributeValue, encode));
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
- }
- public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue) {
- RegisterHiddenField ((Control) page, hiddenFieldName, hiddenFieldInitialValue);
- }
- public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- if (sm._hiddenFields == null)
- sm._hiddenFields = new List<RegisteredHiddenField> ();
- sm._hiddenFields.Add (new RegisteredHiddenField (control, hiddenFieldName, hiddenFieldInitialValue));
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
- }
- public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script) {
- RegisterOnSubmitStatement ((Control) page, type, key, script);
- }
- public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- RegisterScript (ref sm._onSubmitStatements, control, type, key, script, null, false, RegisteredScriptType.OnSubmitStatement);
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterOnSubmitStatement (type, key, script);
- }
- public void RegisterPostBackControl (Control control) {
- if (control == null)
- return;
- if (_postBackControls == null)
- _postBackControls = new List<Control> ();
- if (_postBackControls.Contains (control))
- return;
- _postBackControls.Add (control);
- }
- internal void RegisterUpdatePanel (UpdatePanel updatePanel) {
- if (_updatePanels == null)
- _updatePanels = new List<UpdatePanel> ();
- if (_updatePanels.Contains (updatePanel))
- return;
- _updatePanels.Add (updatePanel);
- }
- public void RegisterScriptDescriptors (IExtenderControl extenderControl) {
- if (extenderControl == null)
- return;
- if (_registeredExtenderControls == null || !_registeredExtenderControls.ContainsKey (extenderControl))
- return;
- Control targetControl = _registeredExtenderControls [extenderControl];
- RegisterScriptDescriptors ((Control) extenderControl, extenderControl.GetScriptDescriptors (targetControl));
- }
- public void RegisterScriptDescriptors (IScriptControl scriptControl) {
- if (scriptControl == null)
- return;
- if (_registeredScriptControls == null || !_registeredScriptControls.Contains (scriptControl))
- return;
- RegisterScriptDescriptors ((Control) scriptControl, scriptControl.GetScriptDescriptors ());
- }
- void RegisterScriptDescriptors (Control control, IEnumerable<ScriptDescriptor> scriptDescriptors)
- {
- if (scriptDescriptors == null)
- return;
- StringBuilder sb = new StringBuilder ();
- foreach (ScriptDescriptor scriptDescriptor in scriptDescriptors) {
- if (IsMultiForm) {
- scriptDescriptor.FormID = Page.Form.ClientID;
- sb.AppendLine ("Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).add_init(function() {");
- }
- else
- sb.AppendLine ("Sys.Application.add_init(function() {");
- sb.Append ("\t");
- sb.AppendLine (scriptDescriptor.GetScript ());
- sb.AppendLine ("});");
- }
- string script = sb.ToString ();
- RegisterStartupScript (control, typeof (ScriptDescriptor), script, script, true);
- }
- public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags) {
- RegisterStartupScript ((Control) page, type, key, script, addScriptTags);
- }
- public static void RegisterStartupScript (Control control, Type type, string key, string script, bool addScriptTags) {
- Page page = control.Page;
- ScriptManager sm = GetCurrent (page);
- RegisterScript (ref sm._startupScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientStartupScript);
- if (!sm.IsInAsyncPostBack)
- page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
- }
- public void RegisterScriptControl<TScriptControl> (TScriptControl scriptControl) where TScriptControl : Control, IScriptControl {
- if (scriptControl == null)
- throw new ArgumentNullException ("scriptControl");
- if (_registeredScriptControls == null)
- _registeredScriptControls = new List<IScriptControl> ();
- if (!_registeredScriptControls.Contains (scriptControl))
- _registeredScriptControls.Add (scriptControl);
- }
- public void RegisterExtenderControl<TExtenderControl> (TExtenderControl extenderControl, Control targetControl) where TExtenderControl : Control, IExtenderControl {
- if (extenderControl == null)
- throw new ArgumentNullException ("extenderControl");
- if (targetControl == null)
- throw new ArgumentNullException ("targetControl");
- if (_registeredExtenderControls == null)
- _registeredExtenderControls = new Dictionary<IExtenderControl, Control> ();
- if (!_registeredExtenderControls.ContainsKey (extenderControl))
- _registeredExtenderControls.Add (extenderControl, targetControl);
- }
- static void RegisterScript (ref List<RegisteredScript> scriptList, Control control, Type type, string key, string script, string url, bool addScriptTag, RegisteredScriptType scriptType) {
- if (scriptList == null)
- scriptList = new List<RegisteredScript> ();
- scriptList.Add (new RegisteredScript (control, type, key, script, url, addScriptTag, scriptType));
- }
- protected override void Render (HtmlTextWriter writer) {
- // MSDN: This method is used by control developers to extend the ScriptManager control.
- // Notes to Inheritors:
- // When overriding this method, call the base Render(HtmlTextWriter) method
- // so that PageRequestManager is rendered on the page.
- if (SupportsPartialRendering) {
- writer.WriteLine ("<script type=\"text/javascript\">");
- writer.WriteLine ("//<![CDATA[");
- writer.WriteLine ("Sys.WebForms.PageRequestManager._initialize('{0}', document.getElementById('{1}'));", UniqueID, Page.Form.ClientID);
- if (IsMultiForm)
- writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance($get(\"{0}\"))._updateControls([{1}], [{2}], [{3}], {4});",
- Page.Form.ClientID,
- FormatUpdatePanelIDs (_updatePanels, true),
- FormatListIDs (_asyncPostBackControls, true, false),
- FormatListIDs (_postBackControls, true, false),
- AsyncPostBackTimeout);
- else
- writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance()._updateControls([{0}], [{1}], [{2}], {3});",
- FormatUpdatePanelIDs (_updatePanels, true),
- FormatListIDs (_asyncPostBackControls, true, false),
- FormatListIDs (_postBackControls, true, false),
- AsyncPostBackTimeout);
- writer.WriteLine ("//]]");
- writer.WriteLine ("</script>");
- }
- base.Render (writer);
- }
- static string FormatUpdatePanelIDs (List<UpdatePanel> list, bool useSingleQuote) {
- if (list == null || list.Count == 0)
- return null;
- StringBuilder sb = new StringBuilder ();
- foreach (UpdatePanel panel in list) {
- if (!panel.Visible)
- continue;
-
- sb.AppendFormat ("{0}{1}{2}{0},", useSingleQuote ? "'" : String.Empty, panel.ChildrenAsTriggers ? "t" : "f", panel.UniqueID);
- }
-
- if (sb.Length > 0)
- sb.Length--;
- return sb.ToString ();
- }
- static string FormatListIDs<T> (List<T> list, bool useSingleQuote, bool skipInvisible) where T : Control
- {
- if (list == null || list.Count == 0)
- return null;
- StringBuilder sb = new StringBuilder ();
- for (int i = 0; i < list.Count; i++) {
- if (skipInvisible && !list [i].Visible)
- continue;
-
- sb.AppendFormat ("{0}{1}{0},", useSingleQuote ? "'" : String.Empty, list [i].UniqueID);
- }
- if (sb.Length > 0)
- sb.Length--;
- return sb.ToString ();
- }
- public void SetFocus (Control control) {
- if (control == null)
- throw new ArgumentNullException ("control");
- if (IsInAsyncPostBack) {
- EnsureFocusClientScript ();
- _controlIDToFocus = control.ClientID;
- }
- else
- Page.SetFocus (control);
- }
- public void SetFocus (string clientID) {
- if (String.IsNullOrEmpty (clientID))
- throw new ArgumentNullException ("control");
- if (IsInAsyncPostBack) {
- EnsureFocusClientScript ();
- _controlIDToFocus = clientID;
- }
- else
- Page.SetFocus (clientID);
- }
- void EnsureFocusClientScript () {
- #if TARGET_DOTNET
- RegisterClientScriptResource (this, typeof (ClientScriptManager), "Focus.js");
- #endif
- }
- #region IPostBackDataHandler Members
- bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection) {
- return LoadPostData (postDataKey, postCollection);
- }
- void IPostBackDataHandler.RaisePostDataChangedEvent () {
- RaisePostDataChangedEvent ();
- }
- #endregion
- internal void WriteCallbackException (TextWriter output, Exception ex, bool writeMessage) {
- #if TARGET_DOTNET
- if (ex is HttpUnhandledException)
- ex = ex.InnerException;
- #endif
- HttpException httpEx = ex as HttpException;
- string message = AsyncPostBackErrorMessage;
- if (String.IsNullOrEmpty (message) && writeMessage) {
- HttpContext ctx = HttpContext.Current;
-
- if (IsDebuggingEnabled)
- message = ex.ToString ();
- else
- message = ex.Message;
- }
-
- WriteCallbackOutput (output, error, httpEx == null ? "500" : httpEx.GetHttpCode ().ToString (), message);
- }
- static internal void WriteCallbackRedirect (TextWriter output, string redirectUrl) {
- WriteCallbackOutput (output, pageRedirect, null, redirectUrl);
- }
- void RegisterPanelForRefresh (UpdatePanel panel)
- {
- if (_panelsToRefresh == null)
- _panelsToRefresh = new List<UpdatePanel> ();
- else if (_panelsToRefresh.Contains (panel))
- return;
- _panelsToRefresh.Add (panel);
- }
-
- internal void WriteCallbackPanel (TextWriter output, UpdatePanel panel, StringBuilder panelOutput)
- {
- WriteCallbackOutput (output, updatePanel, panel.ClientID, panelOutput);
- }
- internal void RegisterChildUpdatePanel (UpdatePanel updatePanel) {
- if (_childUpdatePanels == null)
- _childUpdatePanels = new List<UpdatePanel> ();
- _childUpdatePanels.Add (updatePanel);
- }
- static void WriteCallbackOutput (TextWriter output, string type, string name, object value) {
- string str = value as string;
- StringBuilder sb = value as StringBuilder;
- int length = 0;
- if (str != null)
- length = str.Length;
- else if (sb != null)
- length = sb.Length;
- //output.Write ("{0}|{1}|{2}|{3}|", value == null ? 0 : value.Length, type, name, value);
- output.Write (length);
- output.Write ('|');
- output.Write (type);
- output.Write ('|');
- output.Write (name);
- output.Write ('|');
- for (int i = 0; i < length; i++)
- if (str != null)
- output.Write (str [i]);
- else
- output.Write (sb [i]);
- output.Write ('|');
- }
- void RenderPageCallback (HtmlTextWriter output, Control container)
- {
- Page page = (Page) container;
- // MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx
- //
- // Refreshing UpdatePanel Content
- //
- // When partial-page rendering is enabled, a control can perform a postback
- // that updates the whole page or an asynchronous postback that updates the
- // content of one or more UpdatePanel controls. Whether a control causes an
- // asynchronous postback and updates an UpdatePanel control depends on the
- // following:
- //
- // * If the UpdateMode property of the UpdatePanel control is set to Always,
- // the UpdatePanel control's content is updated on every postback that
- // originates from the page. This includes asynchronous postbacks from
- // controls that are inside other UpdatePanel controls and postbacks from
- // controls that are not inside UpdatePanel controls.
- //
- // * If the UpdateMode property is set to Conditional, the UpdatePanel
- // control's content is updated in the following circumstances:
- //
- // o When you call the Update method of the UpdatePanel control
- // explicitly.
- // o When the UpdatePanel control is nested inside another UpdatePanel
- // control, and the parent panel is updated.
- // o When a postback is caused by a control that is defined as a
- // trigger by using the Triggers property of the UpdatePanel
- // control. In this scenario, the control explicitly triggers an
- // update of the panel content. The control can be either inside or
- // outside the UpdatePanel control that the trigger is associated
- // with.
- // o When the ChildrenAsTriggers property is set to true and a child
- // control of the UpdatePanel control causes a postback. Child
- // controls of nested UpdatePanel controls do not cause an update to
- // the outer UpdatePanel control unless they are explicitly defined
- // as triggers.
- //
- if (_updatePanels != null && _updatePanels.Count > 0) {
- bool needsUpdate;
-
- foreach (UpdatePanel panel in _updatePanels) {
- if (panel.RequiresUpdate || (!String.IsNullOrEmpty (_panelToRefreshID) && String.Compare (_panelToRefreshID, panel.UniqueID, StringComparison.Ordinal) == 0))
- needsUpdate = true;
- else
- needsUpdate = false;
-
- if (needsUpdate == false) {
- Control parent = panel.Parent;
- UpdatePanel parentPanel;
-
- bool havePanelsToRefresh = _panelsToRefresh != null ? _panelsToRefresh.Count > 0 : false;
- while (parent != null) {
- parentPanel = parent as UpdatePanel;
- if (havePanelsToRefresh && parentPanel != null && _panelsToRefresh.Contains (parentPanel)) {
- needsUpdate = true;
- break;
- }
- parent = parent.Parent;
- }
- }
-
- panel.SetInPartialRendering (needsUpdate);
- if (needsUpdate)
- RegisterPanelForRefresh (panel);
- }
- }
-
- page.Form.SetRenderMethodDelegate (RenderFormCallback);
- HtmlTextParser parser = new HtmlTextParser (output);
- page.Form.RenderControl (parser);
- Dictionary <string, string> pageHiddenFields = parser.HiddenFields;
- if (pageHiddenFields != null)
- foreach (KeyValuePair <string, string> kvp in pageHiddenFields)
- WriteCallbackOutput (output, hiddenField, kvp.Key, kvp.Value);
-
- WriteCallbackOutput (output, asyncPostBackControlIDs, null, FormatListIDs (_asyncPostBackControls, false, false));
- WriteCallbackOutput (output, postBackControlIDs, null, FormatListIDs (_postBackControls, false, false));
- WriteCallbackOutput (output, updatePanelIDs, null, FormatUpdatePanelIDs (_updatePanels, false));
- WriteCallbackOutput (output, childUpdatePanelIDs, null, FormatListIDs (_childUpdatePanels, false, true));
- WriteCallbackOutput (output, panelsToRefreshIDs, null, FormatListIDs (_panelsToRefresh, false, true));
- WriteCallbackOutput (output, asyncPostBackTimeout, null, AsyncPostBackTimeout.ToString ());
- if (!IsMultiForm)
- WriteCallbackOutput (output, pageTitle, null, Page.Title);
- if (_dataItems != null)
- foreach (Control control in _dataItems.Keys) {
- DataItemEntry entry = _dataItems [control];
- WriteCallbackOutput (output, entry.IsJsonSerialized ? dataItemJson : dataItem, control.ClientID, entry.DataItem);
- }
- WriteArrayDeclarations (output);
- WriteExpandoAttributes (output);
- WriteScriptBlocks (output, _clientScriptBlocks);
- WriteScriptBlocks (output, _startupScriptBlocks);
- WriteScriptBlocks (output, _onSubmitStatements);
- WriteHiddenFields (output);
- if (!String.IsNullOrEmpty (_controlIDToFocus))
- WriteCallbackOutput (output, focus, null, _controlIDToFocus);
- if (_disposeScripts != null)
- for (int i = 0; i < _disposeScripts.Count; i++) {
- RegisteredDisposeScript entry = _disposeScripts [i];
- if ((_panelsToRefresh != null && _panelsToRefresh.IndexOf (entry.UpdatePanel) >= 0) || (_childUpdatePanels != null && _childUpdatePanels.IndexOf (entry.UpdatePanel) >= 0))
- WriteCallbackOutput (output, scriptDispose, entry.UpdatePanel.ClientID, entry.Script);
- }
- }
- private void WriteExpandoAttributes (HtmlTextWriter writer) {
- if (_expandoAttributes != null) {
- for (int i = 0; i < _expandoAttributes.Count; i++) {
- RegisteredExpandoAttribute attr = _expandoAttributes [i];
- if (HasBeenRendered (attr.Control)) {
- string value;
- if (attr.Encode) {
- StringWriter sw = new StringWriter ();
- Newtonsoft.Json.JavaScriptUtils.WriteEscapedJavaScriptString (attr.Value, sw);
- value = sw.ToString ();
- }
- else
- value = "\"" + attr.Value + "\"";
- WriteCallbackOutput (writer, expando, "document.getElementById('" + attr.ControlId + "')['" + attr.Name + "']", value);
- }
- }
- }
- }
- void WriteArrayDeclarations (HtmlTextWriter writer) {
- if (_arrayDeclarations != null) {
- for (int i = 0; i < _arrayDeclarations.Count; i++) {
- RegisteredArrayDeclaration array = _arrayDeclarations [i];
- if (Page == array.Control || HasBeenRendered (array.Control))
- WriteCallbackOutput (writer, arrayDeclaration, array.Name, array.Value);
- }
- }
- }
- void WriteScriptBlocks (HtmlTextWriter output, List<RegisteredScript> scriptList) {
- if (scriptList == null)
- return;
- var registeredScripts = new Dictionary <string, RegisteredScript> ();
- Control control;
- Page page = Page;
-
- for (int i = 0; i < scriptList.Count; i++) {
- RegisteredScript scriptEntry = scriptList [i];
- if (registeredScripts.ContainsKey (scriptEntry.Key))
- continue;
- control = scriptEntry.Control;
- if (page == control || HasBeenRendered (control)) {
- registeredScripts.Add (scriptEntry.Key, scriptEntry);
- switch (scriptEntry.ScriptType) {
- case RegisteredScriptType.ClientScriptBlock:
- if (scriptEntry.AddScriptTags)
- WriteCallbackOutput (output, scriptBlock, scriptContentNoTags, scriptEntry.Script);
- else
- WriteCallbackOutput (output, scriptBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
- break;
- case RegisteredScriptType.ClientStartupScript:
- if (scriptEntry.AddScriptTags)
- WriteCallbackOutput (output, scriptStartupBlock, scriptContentNoTags, scriptEntry.Script);
- else
- WriteCallbackOutput (output, scriptStartupBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
- break;
- case RegisteredScriptType.ClientScriptInclude:
- WriteCallbackOutput (output, scriptBlock, scriptPath, scriptEntry.Url);
- break;
- case RegisteredScriptType.OnSubmitStatement:
- WriteCallbackOutput (output, onSubmit, null, scriptEntry.Script);
- break;
- }
- }
- }
- }
- void WriteHiddenFields (HtmlTextWriter output) {
- if (_hiddenFields == null)
- return;
- Hashtable registeredFields = new Hashtable ();
- for (int i = 0; i < _hiddenFields.Count; i++) {
- RegisteredHiddenField field = _hiddenFields [i];
- if (registeredFields.ContainsKey (field.Name))
- continue;
- if (Page == field.Control || HasBeenRendered (field.Control)) {
- registeredFields.Add (field.Name, field);
- WriteCallbackOutput (output, hiddenField, field.Name, field.InitialValue);
- }
- }
- }
- static string SerializeScriptBlock (RegisteredScript scriptEntry) {
- try {
- XmlTextReader reader = new XmlTextReader (new StringReader (scriptEntry.Script));
- while (reader.Read ()) {
- switch (reader.NodeType) {
- case XmlNodeType.Element:
- if (String.Compare ("script", reader.Name, StringComparison.OrdinalIgnoreCase) == 0) {
- Dictionary<string, string> dic = new Dictionary<string, string> ();
- while (reader.MoveToNextAttribute ()) {
- dic.Add (reader.Name, reader.Value);
- }
- reader.MoveToContent ();
- dic.Add ("text", reader.ReadInnerXml ());
- return JavaScriptSerializer.DefaultSerializer.Serialize (dic);
- }
- break;
- default:
- continue;
- }
- }
- }
- catch {
- }
- 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));
- }
- void RenderFormCallback (HtmlTextWriter output, Control container)
- {
- output = ((HtmlTextParser) output).ResponseOutput;
- HtmlForm form = (HtmlForm) container;
- HtmlTextWriter writer = new HtmlDropWriter (output);
-
- if (form.HasControls ()) {
- foreach (Control control in form.Controls)
- control.RenderControl (writer);
- }
- }
- internal class AlternativeHtmlTextWriter : HtmlTextWriter
- {
- readonly HtmlTextWriter _responseOutput;
- public HtmlTextWriter ResponseOutput {
- get { return _responseOutput; }
- }
- public AlternativeHtmlTextWriter (TextWriter writer, HtmlTextWriter responseOutput)
- : base (writer) {
- _responseOutput = responseOutput;
- }
- }
- sealed class HtmlTextParser : AlternativeHtmlTextWriter
- {
- bool _done;
- public Dictionary <string, string> _hiddenFields;
- public Dictionary <string, string> HiddenFields {
- get { return _hiddenFields; }
- }
-
- public HtmlTextParser (HtmlTextWriter responseOutput)
- : this (new TextParser (responseOutput), responseOutput) {
- }
- HtmlTextParser (TextParser parser, HtmlTextWriter responseOutput)
- : base (parser, responseOutput)
- {
- parser.HiddenFieldParsed += new TextParser.TextParserHiddenFieldParsedEventHandler (OnHiddenFieldParsed);
- }
-
- void OnHiddenFieldParsed (TextParser sender, TextParserHiddenFieldParsedEventArgs args)
- {
- if (_hiddenFields == null)
- _hiddenFields = new Dictionary <string, string> ();
- _hiddenFields [args.Name] = args.Value;
- }
-
- public override void WriteAttribute (string name, string value) {
- if (!_done && String.Compare ("action", name, StringComparison.OrdinalIgnoreCase) == 0) {
- _done = true;
- ScriptManager.WriteCallbackOutput (ResponseOutput, formAction, null, value);
- return;
- }
- base.WriteAttribute (name, value);
- }
- }
- sealed class TextParserHiddenFieldParsedEventArgs : EventArgs
- {
- public string Name {
- get;
- private set;
- }
- public string Value {
- get;
- private set;
- }
-
- public TextParserHiddenFieldParsedEventArgs (string name, string value)
- {
- this.Name = name;
- this.Value = value;
- }
- }
-
- sealed class TextParser : TextWriter
- {
- public delegate void TextParserHiddenFieldParsedEventHandler (TextParser sender, TextParserHiddenFieldParsedEventArgs args);
- static object textParserHiddenFieldParsedEvent = new object ();
-
- int _state;
- char _charState = (char) 255;
- const char nullCharState = (char) 255;
- StringBuilder _sb = new StringBuilder ();
- Dictionary<string, string> _currentField;
- string _currentAttribute;
- readonly HtmlTextWriter _responseOutput;
- TextParserHiddenFieldParsedEventHandler _hiddenFieldParsedHandler;
-
- public event TextParserHiddenFieldParsedEventHandler HiddenFieldParsed {
- add { _hiddenFieldParsedHandler = value; }
- remove { _hiddenFieldParsedHandler = null; }
- }
-
- public override Encoding Encoding {
- get { return Encoding.UTF8; }
- }
- public TextParser (HtmlTextWriter responseOutput) {
- _responseOutput = responseOutput;
- }
- public override void Write (char value) {
- switch (_state) {
- case 0:
- ParseBeginTag (value);
- break;
- case 1:
- ParseAttributeName (value);
- break;
- case 2:
- ParseAttributeValue (value);
- break;
- }
- }
- private void ParseAttributeValue (char value) {
- switch (value) {
- case '>':
- ResetState ();
- break;
- case '"':
- _currentField.Add (_currentAttribute, _sb.ToString ());
- _state = 1;
- _sb.Length = 0;
- ProbeWriteOutput ();
- break;
- default:
- _sb.Append (value);
- break;
- }
- }
- private void ParseAttributeName (char value) {
- switch (value) {
- case '>':
- ResetState ();
- break;
- case ' ':
- case '=':
- break;
- case '"':
- _currentAttribute = _sb.ToString ();
- _state = 2;
- _sb.Length = 0;
- break;
- default:
- _sb.Append (value);
- break;
- }
- }
- void ParseBeginTag (char value) {
- switch (_charState) {
- case nullCharState:
- if (value == '<')
- _charState = value;
- break;
- case '<':
- if (value == 'i')
- _charState = value;
- else
- ResetState ();
- break;
- case 'i':
- if (value == 'n')
- _charState = value;
- else
- ResetState ();
- break;
- case 'n':
- if (value == 'p')
- _charState = value;
- else
- ResetState ();
- break;
- case 'p':
- if (value == 'u')
- _charState = value;
- else
- ResetState ();
- break;
- case 'u':
- if (value == 't')
- _charState = value;
- else
- ResetState ();
- break;
- case 't':
- if (value == ' ') {
- _state = 1;
- _currentField = new Dictionary<string, string> ();
- }
- else
- ResetState ();
- break;
- }
- }
- private void ResetState () {
- _charState = nullCharState;
- _state = 0;
- _sb.Length = 0;
- }
- private void ProbeWriteOutput () {
- if (!_currentField.ContainsKey ("name"))
- return;
- if (!_currentField.ContainsKey ("value"))
- return;
- string value = _currentField ["value"];
- if (value == null)
- return;
- if (_hiddenFieldParsedHandler != null)
- _hiddenFieldParsedHandler (this, new TextParserHiddenFieldParsedEventArgs (_currentField ["name"], HttpUtility.HtmlDecode (value)));
- else
- ScriptManager.WriteCallbackOutput (_responseOutput, hiddenField, _currentField ["name"], HttpUtility.HtmlDecode (value));
- }
- }
- sealed class HtmlDropWriter : AlternativeHtmlTextWriter
- {
- public HtmlDropWriter (HtmlTextWriter responseOutput)
- : base (new DropWriter (), responseOutput) {
- }
- }
- sealed class DropWriter : TextWriter
- {
- public override Encoding Encoding {
- get { return Encoding.UTF8; }
- }
- }
- sealed class CultureInfoSerializer
- {
- readonly CultureInfo _ci;
- public CultureInfoSerializer (CultureInfo ci) {
- if (ci == null)
- throw new ArgumentNullException ("ci");
- _ci = ci;
- }
- public string name {
- get { return _ci.Name; }
- }
- public NumberFormatInfo numberFormat {
- get { return _ci.NumberFormat; }
- }
- public DateTimeFormatInfo dateTimeFormat {
- get { return _ci.DateTimeFormat; }
- }
- }
- sealed class ScriptReferenceEntry
- {
- readonly Control _control;
- readonly ScriptReference _scriptReference;
- readonly bool _loadBeforeUI;
- public Control Control { get { return _control; } }
- public ScriptReference ScriptReference { get { return _scriptReference; } }
- public bool LoadScriptsBeforeUI { get { return _loadBeforeUI; } }
- public ScriptReferenceEntry (Control control, ScriptReference scriptReference, bool loadBeforeUI) {
- _control = control;
- _scriptReference = scriptReference;
- _loadBeforeUI = loadBeforeUI;
- }
- }
- sealed class DataItemEntry
- {
- readonly string _dataItem;
- readonly bool _isJsonSerialized;
- public string DataItem { get { return _dataItem; } }
- public bool IsJsonSerialized { get { return _isJsonSerialized; } }
- public DataItemEntry (string dataItem, bool isJsonSerialized) {
- _dataItem = dataItem;
- _isJsonSerialized = isJsonSerialized;
- }
- }
- internal void RegisterProxy (ScriptManagerProxy scriptManagerProxy) {
- if (_proxies == null)
- _proxies = new List<ScriptManagerProxy> ();
- _proxies.Add (scriptManagerProxy);
- }
- #region IScriptManager Members
- void IScriptManager.RegisterOnSubmitStatementExternal (Control control, Type type, string key, string script) {
- RegisterOnSubmitStatement (control, type, key, script);
- }
- void IScriptManager.RegisterExpandoAttributeExternal (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
- RegisterExpandoAttribute (control, controlId, attributeName, attributeValue, encode);
- }
- void IScriptManager.RegisterHiddenFieldExternal (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
- RegisterHiddenField (control, hiddenFieldName, hiddenFieldInitialValue);
- }
- void IScriptManager.RegisterStartupScriptExternal (Control control, Type type, string key, string script, bool addScriptTags) {
- RegisterStartupScript (control, type, key, script, addScriptTags);
- }
- void IScriptManager.RegisterArrayDeclarationExternal (Control control, string arrayName, string arrayValue) {
- RegisterArrayDeclaration (control, arrayName, arrayValue);
- }
- void IScriptManager.RegisterClientScriptBlockExternal (Control control, Type type, string key, string script, bool addScriptTags) {
- RegisterClientScriptBlock (control, type, key, script, addScriptTags);
- }
- void IScriptManager.RegisterClientScriptIncludeExternal (Control control, Type type, string key, string url) {
- RegisterClientScriptInclude (control, type, key, url);
- }
- void IScriptManager.RegisterClientScriptResourceExternal (Control control, Type type, string resourceName) {
- RegisterClientScriptResource (control, type, resourceName);
- }
- #endregion
- }
- }
|