ScriptManager.cs 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. //
  2. // ScriptManager.cs
  3. //
  4. // Authors:
  5. // Igor Zelmanovich <[email protected]>
  6. // Marek Habersack <[email protected]>
  7. //
  8. // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
  9. // (C) 2007-2010 Novell, Inc (http://novell.com/)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Text;
  34. using System.ComponentModel;
  35. using System.Security.Permissions;
  36. using System.Collections.Specialized;
  37. using System.Collections;
  38. using System.Web.Handlers;
  39. using System.Reflection;
  40. using System.Web.Configuration;
  41. using System.Web.UI.HtmlControls;
  42. using System.IO;
  43. using System.Globalization;
  44. using System.Threading;
  45. using System.Web.Script.Serialization;
  46. using System.Web.Script.Services;
  47. using System.Xml;
  48. using System.Collections.ObjectModel;
  49. using System.Web.Util;
  50. namespace System.Web.UI
  51. {
  52. [ParseChildrenAttribute (true)]
  53. [DefaultPropertyAttribute ("Scripts")]
  54. [DesignerAttribute ("System.Web.UI.Design.ScriptManagerDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  55. [NonVisualControlAttribute]
  56. [PersistChildrenAttribute (false)]
  57. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  58. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  59. public class ScriptManager : Control, IPostBackDataHandler, IScriptManager
  60. {
  61. // the keywords are used in fomatting async response
  62. const string updatePanel = "updatePanel";
  63. const string hiddenField = "hiddenField";
  64. const string arrayDeclaration = "arrayDeclaration";
  65. const string scriptBlock = "scriptBlock";
  66. #if NET_3_5
  67. const string scriptStartupBlock = "scriptStartupBlock";
  68. #endif
  69. const string expando = "expando";
  70. const string onSubmit = "onSubmit";
  71. const string asyncPostBackControlIDs = "asyncPostBackControlIDs";
  72. const string postBackControlIDs = "postBackControlIDs";
  73. const string updatePanelIDs = "updatePanelIDs";
  74. const string asyncPostBackTimeout = "asyncPostBackTimeout";
  75. const string childUpdatePanelIDs = "childUpdatePanelIDs";
  76. const string panelsToRefreshIDs = "panelsToRefreshIDs";
  77. const string formAction = "formAction";
  78. const string dataItem = "dataItem";
  79. const string dataItemJson = "dataItemJson";
  80. const string scriptDispose = "scriptDispose";
  81. const string pageRedirect = "pageRedirect";
  82. const string error = "error";
  83. const string pageTitle = "pageTitle";
  84. const string focus = "focus";
  85. const string scriptContentNoTags = "ScriptContentNoTags";
  86. const string scriptContentWithTags = "ScriptContentWithTags";
  87. const string scriptPath = "ScriptPath";
  88. static readonly object ScriptManagerKey = typeof (IScriptManager);
  89. int _asyncPostBackTimeout = 90;
  90. List<Control> _asyncPostBackControls;
  91. List<Control> _postBackControls;
  92. List<UpdatePanel> _childUpdatePanels;
  93. List<UpdatePanel> _panelsToRefresh;
  94. List<UpdatePanel> _updatePanels;
  95. ScriptReferenceCollection _scripts;
  96. #if NET_3_5
  97. CompositeScriptReference _compositeScript;
  98. #endif
  99. ServiceReferenceCollection _services;
  100. bool _isInAsyncPostBack;
  101. bool _isInPartialRendering;
  102. string _asyncPostBackSourceElementID;
  103. ScriptMode _scriptMode = ScriptMode.Auto;
  104. bool _enableScriptGlobalization;
  105. bool _enableScriptLocalization;
  106. string _scriptPath;
  107. List<RegisteredScript> _clientScriptBlocks;
  108. List<RegisteredScript> _startupScriptBlocks;
  109. List<RegisteredScript> _onSubmitStatements;
  110. List<RegisteredArrayDeclaration> _arrayDeclarations;
  111. List<RegisteredExpandoAttribute> _expandoAttributes;
  112. List<RegisteredHiddenField> _hiddenFields;
  113. List<IScriptControl> _registeredScriptControls;
  114. Dictionary<IExtenderControl, Control> _registeredExtenderControls;
  115. bool? _supportsPartialRendering;
  116. bool _enablePartialRendering = true;
  117. bool _init;
  118. string _panelToRefreshID;
  119. Dictionary<Control, DataItemEntry> _dataItems;
  120. bool _enablePageMethods;
  121. string _controlIDToFocus;
  122. bool _allowCustomErrorsRedirect = true;
  123. string _asyncPostBackErrorMessage;
  124. List<RegisteredDisposeScript> _disposeScripts;
  125. List<ScriptReferenceEntry> _scriptToRegister;
  126. bool _loadScriptsBeforeUI = true;
  127. AuthenticationServiceManager _authenticationService;
  128. ProfileServiceManager _profileService;
  129. List<ScriptManagerProxy> _proxies;
  130. [DefaultValue (true)]
  131. [Category ("Behavior")]
  132. public bool AllowCustomErrorsRedirect {
  133. get {
  134. return _allowCustomErrorsRedirect;
  135. }
  136. set {
  137. _allowCustomErrorsRedirect = value;
  138. }
  139. }
  140. [Category ("Behavior")]
  141. [DefaultValue ("")]
  142. public string AsyncPostBackErrorMessage {
  143. get {
  144. if (String.IsNullOrEmpty (_asyncPostBackErrorMessage))
  145. return String.Empty;
  146. return _asyncPostBackErrorMessage;
  147. }
  148. set {
  149. _asyncPostBackErrorMessage = value;
  150. }
  151. }
  152. [Browsable (false)]
  153. public string AsyncPostBackSourceElementID {
  154. get {
  155. if (_asyncPostBackSourceElementID == null)
  156. return String.Empty;
  157. return _asyncPostBackSourceElementID;
  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. [Category ("Behavior")]
  171. [MergableProperty (false)]
  172. [DefaultValue ("")]
  173. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  174. [PersistenceMode (PersistenceMode.InnerProperty)]
  175. public AuthenticationServiceManager AuthenticationService {
  176. get {
  177. if (_authenticationService == null)
  178. _authenticationService = new AuthenticationServiceManager ();
  179. return _authenticationService;
  180. }
  181. }
  182. [Category ("Behavior")]
  183. [DefaultValue (false)]
  184. public bool EnablePageMethods {
  185. get {
  186. return _enablePageMethods;
  187. }
  188. set {
  189. _enablePageMethods = value;
  190. }
  191. }
  192. [DefaultValue (true)]
  193. [Category ("Behavior")]
  194. public bool EnablePartialRendering {
  195. get {
  196. return _enablePartialRendering;
  197. }
  198. set {
  199. if (_init)
  200. throw new InvalidOperationException ();
  201. _enablePartialRendering = value;
  202. }
  203. }
  204. [DefaultValue (false)]
  205. [Category ("Behavior")]
  206. public bool EnableScriptGlobalization {
  207. get {
  208. return _enableScriptGlobalization;
  209. }
  210. set {
  211. _enableScriptGlobalization = value;
  212. }
  213. }
  214. [Category ("Behavior")]
  215. [DefaultValue (false)]
  216. public bool EnableScriptLocalization {
  217. get {
  218. return _enableScriptLocalization;
  219. }
  220. set {
  221. _enableScriptLocalization = value;
  222. }
  223. }
  224. [Browsable (false)]
  225. public bool IsDebuggingEnabled {
  226. get {
  227. if (IsDeploymentRetail)
  228. return false;
  229. if (!RuntimeHelpers.DebuggingEnabled && (ScriptMode == ScriptMode.Auto || ScriptMode == ScriptMode.Inherit))
  230. return false;
  231. if (ScriptMode == ScriptMode.Release)
  232. return false;
  233. return true;
  234. }
  235. }
  236. internal bool IsDeploymentRetail {
  237. get {
  238. #if TARGET_J2EE
  239. return false;
  240. #else
  241. DeploymentSection deployment = (DeploymentSection) WebConfigurationManager.GetSection ("system.web/deployment");
  242. return deployment.Retail;
  243. #endif
  244. }
  245. }
  246. [Browsable (false)]
  247. public bool IsInAsyncPostBack {
  248. get {
  249. return _isInAsyncPostBack;
  250. }
  251. }
  252. internal bool IsInPartialRendering {
  253. get {
  254. return _isInPartialRendering;
  255. }
  256. set {
  257. _isInPartialRendering = value;
  258. }
  259. }
  260. [Category ("Behavior")]
  261. [DefaultValue (true)]
  262. public bool LoadScriptsBeforeUI {
  263. get {
  264. return _loadScriptsBeforeUI;
  265. }
  266. set {
  267. _loadScriptsBeforeUI = value;
  268. }
  269. }
  270. [PersistenceMode (PersistenceMode.InnerProperty)]
  271. [DefaultValue ("")]
  272. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  273. [Category ("Behavior")]
  274. [MergableProperty (false)]
  275. public ProfileServiceManager ProfileService {
  276. get {
  277. if (_profileService == null)
  278. _profileService = new ProfileServiceManager ();
  279. return _profileService;
  280. }
  281. }
  282. [Category ("Behavior")]
  283. #if TARGET_J2EE
  284. [MonoLimitation ("The 'Auto' value is the same as 'Debug'.")]
  285. #endif
  286. public ScriptMode ScriptMode {
  287. get {
  288. return _scriptMode;
  289. }
  290. set {
  291. if (value == ScriptMode.Inherit)
  292. value = ScriptMode.Auto;
  293. _scriptMode = value;
  294. }
  295. }
  296. [DefaultValue ("")]
  297. [Category ("Behavior")]
  298. public string ScriptPath {
  299. get {
  300. if (_scriptPath == null)
  301. return String.Empty;
  302. return _scriptPath;
  303. }
  304. set {
  305. _scriptPath = value;
  306. }
  307. }
  308. [PersistenceMode (PersistenceMode.InnerProperty)]
  309. [DefaultValue ("")]
  310. [Category ("Behavior")]
  311. [MergableProperty (false)]
  312. public ScriptReferenceCollection Scripts {
  313. get {
  314. if (_scripts == null)
  315. _scripts = new ScriptReferenceCollection ();
  316. return _scripts;
  317. }
  318. }
  319. #if NET_3_5
  320. [PersistenceMode (PersistenceMode.InnerProperty)]
  321. [Category ("Behavior")]
  322. [DefaultValue (null)]
  323. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  324. [MergableProperty (false)]
  325. public CompositeScriptReference CompositeScript {
  326. get {
  327. if (_compositeScript == null)
  328. _compositeScript = new CompositeScriptReference ();
  329. return _compositeScript;
  330. }
  331. }
  332. #endif
  333. [PersistenceMode (PersistenceMode.InnerProperty)]
  334. [DefaultValue ("")]
  335. [MergableProperty (false)]
  336. [Category ("Behavior")]
  337. public ServiceReferenceCollection Services {
  338. get {
  339. if (_services == null)
  340. _services = new ServiceReferenceCollection ();
  341. return _services;
  342. }
  343. }
  344. [DefaultValue (true)]
  345. [Browsable (false)]
  346. public bool SupportsPartialRendering {
  347. get {
  348. if (!_supportsPartialRendering.HasValue)
  349. _supportsPartialRendering = CheckSupportsPartialRendering ();
  350. return _supportsPartialRendering.Value;
  351. }
  352. set {
  353. if (_init)
  354. throw new InvalidOperationException ();
  355. if (!EnablePartialRendering && value)
  356. throw new InvalidOperationException ("The SupportsPartialRendering property cannot be set when EnablePartialRendering is false.");
  357. _supportsPartialRendering = value;
  358. }
  359. }
  360. bool CheckSupportsPartialRendering () {
  361. if (!EnablePartialRendering)
  362. return false;
  363. // TODO: consider browser capabilities
  364. return true;
  365. }
  366. [EditorBrowsable (EditorBrowsableState.Never)]
  367. [Browsable (false)]
  368. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  369. public override bool Visible {
  370. get {
  371. return true;
  372. }
  373. set {
  374. throw new NotImplementedException ();
  375. }
  376. }
  377. [Category ("Action")]
  378. public event EventHandler<AsyncPostBackErrorEventArgs> AsyncPostBackError;
  379. [Category ("Action")]
  380. public event EventHandler<ScriptReferenceEventArgs> ResolveScriptReference;
  381. #if NET_3_5
  382. [Category ("Action")]
  383. public event EventHandler<CompositeScriptReferenceEventArgs> ResolveCompositeScriptReference;
  384. #endif
  385. public static ScriptManager GetCurrent (Page page) {
  386. if (page == null)
  387. throw new ArgumentNullException ("page");
  388. return GetCurrentInternal (page);
  389. }
  390. internal static ScriptManager GetCurrentInternal (Page page)
  391. {
  392. if (page == null)
  393. return null;
  394. return (ScriptManager) page.Items [ScriptManagerKey];
  395. }
  396. static void SetCurrent (Page page, ScriptManager instance) {
  397. page.Items [ScriptManagerKey] = instance;
  398. page.ClientScript.RegisterWebFormClientScript ();
  399. }
  400. UpdatePanel FindPanelWithId (string id)
  401. {
  402. if (_updatePanels == null)
  403. return null;
  404. foreach (UpdatePanel panel in _updatePanels) {
  405. if (panel.ID == id)
  406. return panel;
  407. }
  408. return null;
  409. }
  410. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection) {
  411. _isInAsyncPostBack = true;
  412. string arg = postCollection [postDataKey];
  413. if (!String.IsNullOrEmpty (arg)) {
  414. string [] args = arg.Split ('|');
  415. switch (args.Length) {
  416. case 1:
  417. _asyncPostBackSourceElementID = args [0];
  418. break;
  419. case 2:
  420. _panelToRefreshID = args [0];
  421. _asyncPostBackSourceElementID = args [1];
  422. break;
  423. default: // "impossible situation"
  424. throw new InvalidOperationException ("Unexpected format of post data.");
  425. }
  426. return true;
  427. }
  428. return false;
  429. }
  430. protected internal virtual void OnAsyncPostBackError (AsyncPostBackErrorEventArgs e) {
  431. if (AsyncPostBackError != null)
  432. AsyncPostBackError (this, e);
  433. }
  434. protected internal override void OnInit (EventArgs e) {
  435. base.OnInit (e);
  436. if (GetCurrentInternal (Page) != null)
  437. throw new InvalidOperationException ("Only one instance of a ScriptManager can be added to the page.");
  438. SetCurrent (Page, this);
  439. Page.Error += new EventHandler (OnPageError);
  440. _init = true;
  441. }
  442. void OnPageError (object sender, EventArgs e) {
  443. if (IsInAsyncPostBack)
  444. OnAsyncPostBackError (new AsyncPostBackErrorEventArgs (Context.Error));
  445. }
  446. protected internal override void OnPreRender (EventArgs e) {
  447. base.OnPreRender (e);
  448. Page.PreRenderComplete += new EventHandler (OnPreRenderComplete);
  449. if (IsInAsyncPostBack) {
  450. Page.SetRenderMethodDelegate (RenderPageCallback);
  451. }
  452. else {
  453. if (EnableScriptGlobalization) {
  454. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  455. string script = String.Format ("var __cultureInfo = '{0}';", JavaScriptSerializer.DefaultSerializer.Serialize (new CultureInfoSerializer (culture)));
  456. RegisterClientScriptBlock (this, typeof (ScriptManager), "ScriptGlobalization", script, true);
  457. }
  458. // Register dispose script
  459. if (_disposeScripts != null && _disposeScripts.Count > 0) {
  460. StringBuilder sb = new StringBuilder ();
  461. sb.AppendLine ();
  462. for (int i = 0; i < _disposeScripts.Count; i++) {
  463. RegisteredDisposeScript entry = _disposeScripts [i];
  464. if (IsMultiForm)
  465. sb.Append ("Sys.WebForms.PageRequestManager.getInstance($get(\"" + Page.Form.ClientID + "\"))._registerDisposeScript(\"");
  466. else
  467. sb.Append ("Sys.WebForms.PageRequestManager.getInstance()._registerDisposeScript(\"");
  468. sb.Append (entry.UpdatePanel.ClientID);
  469. sb.Append ("\", ");
  470. sb.Append (JavaScriptSerializer.DefaultSerializer.Serialize (entry.Script)); //JavaScriptSerializer.Serialize used escape script literal
  471. sb.AppendLine (");");
  472. }
  473. RegisterStartupScript (this, typeof (ExtenderControl), "disposeScripts;", sb.ToString (), true);
  474. }
  475. #if TARGET_DOTNET
  476. // to cause webform client script being included
  477. Page.ClientScript.GetPostBackEventReference (new PostBackOptions (this, null, null, false, false, false, true, true, null));
  478. #else
  479. Page.ClientScript.GetPostBackEventReference (this, null);
  480. #endif
  481. }
  482. }
  483. ScriptReference CreateScriptReference (string path, string assembly, bool notifyScriptLoaded)
  484. {
  485. var ret = new ScriptReference (path, assembly);
  486. if (!notifyScriptLoaded)
  487. ret.NotifyScriptLoaded = false;
  488. OnResolveScriptReference (new ScriptReferenceEventArgs (ret));
  489. return ret;
  490. }
  491. ScriptReference CreateScriptReference (string path, string assembly)
  492. {
  493. return CreateScriptReference (path, assembly, true);
  494. }
  495. void OnPreRenderComplete (object sender, EventArgs e)
  496. {
  497. // Resolve Scripts
  498. ScriptReference ajaxScript = CreateScriptReference ("MicrosoftAjax.js", String.Empty, false);
  499. ScriptReference ajaxWebFormsScript = CreateScriptReference ("MicrosoftAjaxWebForms.js", String.Empty, false);
  500. ScriptReference ajaxExtensionScript = null;
  501. ScriptReference ajaxWebFormsExtensionScript = null;
  502. if (IsMultiForm) {
  503. ajaxExtensionScript = CreateScriptReference ("MicrosoftAjaxExtension.js", String.Empty);
  504. ajaxWebFormsExtensionScript = CreateScriptReference ("MicrosoftAjaxWebFormsExtension.js", String.Empty);
  505. }
  506. foreach (ScriptReferenceEntry script in GetScriptReferences ()) {
  507. OnResolveScriptReference (new ScriptReferenceEventArgs (script.ScriptReference));
  508. if (_scriptToRegister == null)
  509. _scriptToRegister = new List<ScriptReferenceEntry> ();
  510. _scriptToRegister.Add (script);
  511. }
  512. if (!IsInAsyncPostBack) {
  513. // Register Ajax framework script.
  514. RegisterScriptReference (this, ajaxScript, true);
  515. if (IsMultiForm) {
  516. RegisterScriptReference (this, ajaxExtensionScript, true);
  517. RegisterClientScriptBlock (this, typeof (ScriptManager), "Sys.Application", "\nSys.Application._initialize(document.getElementById('" + Page.Form.ClientID + "'));\n", true);
  518. }
  519. StringBuilder sb = new StringBuilder ();
  520. sb.AppendLine ("if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');");
  521. ScriptingProfileServiceSection profileService = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
  522. if (profileService != null && profileService.Enabled)
  523. sb.AppendLine ("Sys.Services._ProfileService.DefaultWebServicePath = '" + ResolveClientUrl ("~" + System.Web.Script.Services.ProfileService.DefaultWebServicePath) + "';");
  524. string profileServicePath = GetProfileServicePath ();
  525. if (!String.IsNullOrEmpty (profileServicePath))
  526. sb.AppendLine ("Sys.Services.ProfileService.set_path('" + profileServicePath + "');");
  527. ScriptingAuthenticationServiceSection authenticationService = (ScriptingAuthenticationServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/authenticationService");
  528. if (authenticationService != null && authenticationService.Enabled) {
  529. sb.AppendLine ("Sys.Services._AuthenticationService.DefaultWebServicePath = '" + ResolveClientUrl ("~/Authentication_JSON_AppService.axd") + "';");
  530. if (Page.User.Identity.IsAuthenticated)
  531. sb.AppendLine ("Sys.Services.AuthenticationService._setAuthenticated(true);");
  532. }
  533. string authenticationServicePath = GetAuthenticationServicePath ();
  534. if (!String.IsNullOrEmpty (authenticationServicePath))
  535. sb.AppendLine ("Sys.Services.AuthenticationService.set_path('" + authenticationServicePath + "');");
  536. RegisterClientScriptBlock (this, typeof (ScriptManager), "Framework", sb.ToString (), true);
  537. RegisterScriptReference (this, ajaxWebFormsScript, true);
  538. if (IsMultiForm)
  539. RegisterScriptReference (this, ajaxWebFormsExtensionScript, true);
  540. }
  541. #if NET_3_5
  542. if (_compositeScript != null && _compositeScript.HaveScripts ()) {
  543. OnResolveCompositeScriptReference (new CompositeScriptReferenceEventArgs (_compositeScript));
  544. RegisterScriptReference (this, _compositeScript, true);
  545. }
  546. #endif
  547. // Register Scripts
  548. if (_scriptToRegister != null)
  549. for (int i = 0; i < _scriptToRegister.Count; i++)
  550. RegisterScriptReference (_scriptToRegister [i].Control, _scriptToRegister [i].ScriptReference, _scriptToRegister [i].LoadScriptsBeforeUI);
  551. if (!IsInAsyncPostBack) {
  552. // Register services
  553. if (_services != null && _services.Count > 0) {
  554. for (int i = 0; i < _services.Count; i++) {
  555. RegisterServiceReference (this, _services [i]);
  556. }
  557. }
  558. if (_proxies != null && _proxies.Count > 0) {
  559. for (int i = 0; i < _proxies.Count; i++) {
  560. ScriptManagerProxy proxy = _proxies [i];
  561. for (int j = 0; j < proxy.Services.Count; j++) {
  562. RegisterServiceReference (proxy, proxy.Services [j]);
  563. }
  564. }
  565. }
  566. if (EnablePageMethods) {
  567. LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (Page.GetType (), Page.Request.FilePath);
  568. RegisterClientScriptBlock (this, typeof (ScriptManager), "PageMethods", logicalTypeInfo.Proxy, true);
  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. string GetProfileServicePath () {
  578. if (_profileService != null && !String.IsNullOrEmpty (_profileService.Path))
  579. return ResolveClientUrl (_profileService.Path);
  580. if (_proxies != null && _proxies.Count > 0)
  581. for (int i = 0; i < _proxies.Count; i++)
  582. if (!String.IsNullOrEmpty (_proxies [i].ProfileService.Path))
  583. return _proxies [i].ResolveClientUrl (_proxies [i].ProfileService.Path);
  584. return null;
  585. }
  586. string GetAuthenticationServicePath () {
  587. if (_authenticationService != null && !String.IsNullOrEmpty (_authenticationService.Path))
  588. return ResolveClientUrl (_authenticationService.Path);
  589. if (_proxies != null && _proxies.Count > 0)
  590. for (int i = 0; i < _proxies.Count; i++)
  591. if (!String.IsNullOrEmpty (_proxies [i].AuthenticationService.Path))
  592. return _proxies [i].ResolveClientUrl (_proxies [i].AuthenticationService.Path);
  593. return null;
  594. }
  595. public ReadOnlyCollection<RegisteredArrayDeclaration> GetRegisteredArrayDeclarations () {
  596. if (_arrayDeclarations == null)
  597. _arrayDeclarations = new List<RegisteredArrayDeclaration> ();
  598. return new ReadOnlyCollection<RegisteredArrayDeclaration> (_arrayDeclarations);
  599. }
  600. public ReadOnlyCollection<RegisteredScript> GetRegisteredClientScriptBlocks () {
  601. if (_clientScriptBlocks == null)
  602. _clientScriptBlocks = new List<RegisteredScript> ();
  603. return new ReadOnlyCollection<RegisteredScript> (_clientScriptBlocks);
  604. }
  605. public ReadOnlyCollection<RegisteredDisposeScript> GetRegisteredDisposeScripts () {
  606. if (_disposeScripts == null)
  607. _disposeScripts = new List<RegisteredDisposeScript> ();
  608. return new ReadOnlyCollection<RegisteredDisposeScript> (_disposeScripts);
  609. }
  610. public ReadOnlyCollection<RegisteredExpandoAttribute> GetRegisteredExpandoAttributes () {
  611. if (_expandoAttributes == null)
  612. _expandoAttributes = new List<RegisteredExpandoAttribute> ();
  613. return new ReadOnlyCollection<RegisteredExpandoAttribute> (_expandoAttributes);
  614. }
  615. public ReadOnlyCollection<RegisteredHiddenField> GetRegisteredHiddenFields () {
  616. if (_hiddenFields == null)
  617. _hiddenFields = new List<RegisteredHiddenField> ();
  618. return new ReadOnlyCollection<RegisteredHiddenField> (_hiddenFields);
  619. }
  620. public ReadOnlyCollection<RegisteredScript> GetRegisteredOnSubmitStatements () {
  621. if (_onSubmitStatements == null)
  622. _onSubmitStatements = new List<RegisteredScript> ();
  623. return new ReadOnlyCollection<RegisteredScript> (_onSubmitStatements);
  624. }
  625. public ReadOnlyCollection<RegisteredScript> GetRegisteredStartupScripts () {
  626. if (_startupScriptBlocks == null)
  627. _startupScriptBlocks = new List<RegisteredScript> ();
  628. return new ReadOnlyCollection<RegisteredScript> (_startupScriptBlocks);
  629. }
  630. #if TARGET_J2EE
  631. bool _isMultiForm = false;
  632. bool _isMultiFormInited = false;
  633. bool IsMultiForm {
  634. get {
  635. if (!_isMultiFormInited) {
  636. string isMultiForm = WebConfigurationManager.AppSettings ["mainsoft.use.portlet.namespace"];
  637. _isMultiForm = isMultiForm != null ? Boolean.Parse (isMultiForm) : false;
  638. _isMultiFormInited = true;
  639. }
  640. return _isMultiForm;
  641. }
  642. }
  643. #else
  644. bool IsMultiForm {
  645. get { return false; }
  646. }
  647. #endif
  648. bool PanelRequiresUpdate (UpdatePanel panel)
  649. {
  650. if (panel == null || _panelsToRefresh == null || _panelsToRefresh.Count == 0)
  651. return false;
  652. return _panelsToRefresh.Contains (panel);
  653. }
  654. bool HasBeenRendered (Control control)
  655. {
  656. if (control == null)
  657. return false;
  658. UpdatePanel panel = control as UpdatePanel;
  659. if (PanelRequiresUpdate (panel))
  660. return true;
  661. return HasBeenRendered (control.Parent);
  662. }
  663. IEnumerable<ScriptReferenceEntry> GetScriptReferences () {
  664. if (_scripts != null && _scripts.Count > 0) {
  665. for (int i = 0; i < _scripts.Count; i++) {
  666. yield return new ScriptReferenceEntry (this, _scripts [i], LoadScriptsBeforeUI);
  667. }
  668. }
  669. if (_proxies != null && _proxies.Count > 0) {
  670. for (int i = 0; i < _proxies.Count; i++) {
  671. ScriptManagerProxy proxy = _proxies [i];
  672. for (int j = 0; j < proxy.Scripts.Count; j++)
  673. yield return new ScriptReferenceEntry (proxy, proxy.Scripts [j], LoadScriptsBeforeUI);
  674. }
  675. }
  676. if (_registeredScriptControls != null && _registeredScriptControls.Count > 0) {
  677. for (int i = 0; i < _registeredScriptControls.Count; i++) {
  678. IEnumerable<ScriptReference> scripts = _registeredScriptControls [i].GetScriptReferences ();
  679. if (scripts != null)
  680. foreach (ScriptReference s in scripts)
  681. yield return new ScriptReferenceEntry ((Control) _registeredScriptControls [i], s, LoadScriptsBeforeUI);
  682. }
  683. }
  684. if (_registeredExtenderControls != null && _registeredExtenderControls.Count > 0) {
  685. foreach (IExtenderControl ex in _registeredExtenderControls.Keys) {
  686. IEnumerable<ScriptReference> scripts = ex.GetScriptReferences ();
  687. if (scripts != null)
  688. foreach (ScriptReference s in scripts)
  689. yield return new ScriptReferenceEntry ((Control) ex, s, LoadScriptsBeforeUI);
  690. }
  691. }
  692. }
  693. #if NET_3_5
  694. protected virtual void OnResolveCompositeScriptReference (CompositeScriptReferenceEventArgs e)
  695. {
  696. EventHandler <CompositeScriptReferenceEventArgs> evt = ResolveCompositeScriptReference;
  697. if (evt != null)
  698. evt (this, e);
  699. }
  700. #endif
  701. protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e) {
  702. if (ResolveScriptReference != null)
  703. ResolveScriptReference (this, e);
  704. }
  705. protected virtual void RaisePostDataChangedEvent ()
  706. {
  707. // Why override?
  708. }
  709. public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue) {
  710. RegisterArrayDeclaration ((Control) page, arrayName, arrayValue);
  711. }
  712. public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue) {
  713. Page page = control.Page;
  714. ScriptManager sm = GetCurrentInternal (page);
  715. if (sm == null)
  716. return;
  717. if (sm._arrayDeclarations == null)
  718. sm._arrayDeclarations = new List<RegisteredArrayDeclaration> ();
  719. sm._arrayDeclarations.Add (new RegisteredArrayDeclaration (control, arrayName, arrayValue));
  720. if (!sm.IsInAsyncPostBack)
  721. page.ClientScript.RegisterArrayDeclaration (arrayName, arrayValue);
  722. }
  723. public void RegisterAsyncPostBackControl (Control control) {
  724. if (control == null)
  725. return;
  726. if (_asyncPostBackControls == null)
  727. _asyncPostBackControls = new List<Control> ();
  728. if (_asyncPostBackControls.Contains (control))
  729. return;
  730. _asyncPostBackControls.Add (control);
  731. }
  732. public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags) {
  733. RegisterClientScriptBlock ((Control) page, type, key, script, addScriptTags);
  734. }
  735. public static void RegisterClientScriptBlock (Control control, Type type, string key, string script, bool addScriptTags) {
  736. Page page = control.Page;
  737. ScriptManager sm = GetCurrentInternal (page);
  738. if (sm == null)
  739. return;
  740. RegisterScript (ref sm._clientScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientScriptBlock);
  741. if (!sm.IsInAsyncPostBack)
  742. page.ClientScript.RegisterClientScriptBlock (type, key, script, addScriptTags);
  743. }
  744. public static void RegisterClientScriptInclude (Page page, Type type, string key, string url) {
  745. RegisterClientScriptInclude ((Control) page, type, key, url);
  746. }
  747. public static void RegisterClientScriptInclude (Control control, Type type, string key, string url) {
  748. Page page = control.Page;
  749. ScriptManager sm = GetCurrentInternal (page);
  750. if (sm == null)
  751. return;
  752. RegisterScript (ref sm._clientScriptBlocks, control, type, key, null, url, false, RegisteredScriptType.ClientScriptInclude);
  753. if (!sm.IsInAsyncPostBack)
  754. page.ClientScript.RegisterClientScriptInclude (type, key, url);
  755. }
  756. public static void RegisterClientScriptResource (Page page, Type type, string resourceName) {
  757. RegisterClientScriptResource ((Control) page, type, resourceName);
  758. }
  759. public static void RegisterClientScriptResource (Control control, Type type, string resourceName) {
  760. RegisterClientScriptInclude (control, type, resourceName, ScriptResourceHandler.GetResourceUrl (type.Assembly, resourceName, true));
  761. }
  762. void RegisterScriptReference (Control control, ScriptReferenceBase script, bool loadScriptsBeforeUI)
  763. {
  764. string scriptPath = script.Path;
  765. string url = script.GetUrl (this, false);
  766. if (control != this && !String.IsNullOrEmpty (scriptPath))
  767. url = control.ResolveClientUrl (url);
  768. if (String.IsNullOrEmpty (url))
  769. return;
  770. if (loadScriptsBeforeUI)
  771. RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
  772. else
  773. RegisterStartupScript (control, typeof (ScriptManager), url, String.Format ("<script src=\"{0}\" type=\"text/javascript\"></script>", url), false);
  774. }
  775. void RegisterServiceReference (Control control, ServiceReference serviceReference)
  776. {
  777. if (serviceReference.InlineScript) {
  778. string url = control.ResolveUrl (serviceReference.Path);
  779. Type type = WebServiceParser.GetCompiledType (url, Context);
  780. if (type != null) {
  781. object[] attributes = type.GetCustomAttributes (typeof (ScriptServiceAttribute), true);
  782. if (attributes.Length == 0)
  783. throw new InvalidOperationException ("Only Web services with a [ScriptService] attribute on the class definition can be called from script.");
  784. }
  785. LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (type, url);
  786. RegisterClientScriptBlock (control, typeof (ScriptManager), url, logicalTypeInfo.Proxy, true);
  787. }
  788. else {
  789. #if TARGET_J2EE
  790. string pathInfo = "/js.invoke";
  791. #else
  792. string pathInfo = "/js";
  793. if (IsDebuggingEnabled)
  794. pathInfo += "debug";
  795. #endif
  796. string url = String.Concat (control.ResolveClientUrl (serviceReference.Path), pathInfo);
  797. RegisterClientScriptInclude (control, typeof (ScriptManager), url, url);
  798. }
  799. }
  800. public void RegisterDataItem (Control control, string dataItem) {
  801. RegisterDataItem (control, dataItem, false);
  802. }
  803. public void RegisterDataItem (Control control, string dataItem, bool isJsonSerialized) {
  804. if (!IsInAsyncPostBack)
  805. throw new InvalidOperationException ("RegisterDataItem can only be called during an async postback.");
  806. if (control == null)
  807. throw new ArgumentNullException ("control");
  808. if (_dataItems == null)
  809. _dataItems = new Dictionary<Control, DataItemEntry> ();
  810. if (_dataItems.ContainsKey (control))
  811. throw new ArgumentException (String.Format ("'{0}' already has a data item registered.", control.ID), "control");
  812. _dataItems.Add (control, new DataItemEntry (dataItem, isJsonSerialized));
  813. }
  814. public void RegisterDispose (Control control, string disposeScript) {
  815. if (control == null)
  816. throw new ArgumentNullException ("control");
  817. if (disposeScript == null)
  818. throw new ArgumentNullException ("disposeScript");
  819. UpdatePanel updatePanel = GetUpdatePanel (control);
  820. if (updatePanel == null)
  821. return;
  822. if (_disposeScripts == null)
  823. _disposeScripts = new List<RegisteredDisposeScript> ();
  824. _disposeScripts.Add (new RegisteredDisposeScript (control, disposeScript, updatePanel));
  825. }
  826. static UpdatePanel GetUpdatePanel (Control control) {
  827. if (control == null)
  828. return null;
  829. UpdatePanel parent = control.Parent as UpdatePanel;
  830. if (parent != null)
  831. return parent;
  832. return GetUpdatePanel (control.Parent);
  833. }
  834. public static void RegisterExpandoAttribute (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
  835. Page page = control.Page;
  836. ScriptManager sm = GetCurrentInternal (page);
  837. if (sm == null)
  838. return;
  839. if (sm._expandoAttributes == null)
  840. sm._expandoAttributes = new List<RegisteredExpandoAttribute> ();
  841. sm._expandoAttributes.Add (new RegisteredExpandoAttribute (control, controlId, attributeName, attributeValue, encode));
  842. if (!sm.IsInAsyncPostBack)
  843. page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  844. }
  845. public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue) {
  846. RegisterHiddenField ((Control) page, hiddenFieldName, hiddenFieldInitialValue);
  847. }
  848. public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
  849. Page page = control.Page;
  850. ScriptManager sm = GetCurrentInternal (page);
  851. if (sm == null)
  852. return;
  853. if (sm._hiddenFields == null)
  854. sm._hiddenFields = new List<RegisteredHiddenField> ();
  855. sm._hiddenFields.Add (new RegisteredHiddenField (control, hiddenFieldName, hiddenFieldInitialValue));
  856. if (!sm.IsInAsyncPostBack)
  857. page.ClientScript.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  858. }
  859. public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script) {
  860. RegisterOnSubmitStatement ((Control) page, type, key, script);
  861. }
  862. public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script) {
  863. Page page = control.Page;
  864. ScriptManager sm = GetCurrentInternal (page);
  865. if (sm == null)
  866. return;
  867. RegisterScript (ref sm._onSubmitStatements, control, type, key, script, null, false, RegisteredScriptType.OnSubmitStatement);
  868. if (!sm.IsInAsyncPostBack)
  869. page.ClientScript.RegisterOnSubmitStatement (type, key, script);
  870. }
  871. public void RegisterPostBackControl (Control control) {
  872. if (control == null)
  873. return;
  874. if (_postBackControls == null)
  875. _postBackControls = new List<Control> ();
  876. if (_postBackControls.Contains (control))
  877. return;
  878. _postBackControls.Add (control);
  879. }
  880. internal void RegisterUpdatePanel (UpdatePanel updatePanel) {
  881. if (_updatePanels == null)
  882. _updatePanels = new List<UpdatePanel> ();
  883. if (_updatePanels.Contains (updatePanel))
  884. return;
  885. _updatePanels.Add (updatePanel);
  886. }
  887. public void RegisterScriptDescriptors (IExtenderControl extenderControl) {
  888. if (extenderControl == null)
  889. return;
  890. if (_registeredExtenderControls == null || !_registeredExtenderControls.ContainsKey (extenderControl))
  891. return;
  892. Control targetControl = _registeredExtenderControls [extenderControl];
  893. RegisterScriptDescriptors ((Control) extenderControl, extenderControl.GetScriptDescriptors (targetControl));
  894. }
  895. public void RegisterScriptDescriptors (IScriptControl scriptControl) {
  896. if (scriptControl == null)
  897. return;
  898. if (_registeredScriptControls == null || !_registeredScriptControls.Contains (scriptControl))
  899. return;
  900. RegisterScriptDescriptors ((Control) scriptControl, scriptControl.GetScriptDescriptors ());
  901. }
  902. void RegisterScriptDescriptors (Control control, IEnumerable<ScriptDescriptor> scriptDescriptors)
  903. {
  904. if (scriptDescriptors == null)
  905. return;
  906. StringBuilder sb = new StringBuilder ();
  907. foreach (ScriptDescriptor scriptDescriptor in scriptDescriptors) {
  908. if (IsMultiForm) {
  909. scriptDescriptor.FormID = Page.Form.ClientID;
  910. sb.AppendLine ("Sys.Application.getInstance($get(\"" + Page.Form.ClientID + "\")).add_init(function() {");
  911. }
  912. else
  913. sb.AppendLine ("Sys.Application.add_init(function() {");
  914. sb.Append ("\t");
  915. sb.AppendLine (scriptDescriptor.GetScript ());
  916. sb.AppendLine ("});");
  917. }
  918. string script = sb.ToString ();
  919. RegisterStartupScript (control, typeof (ScriptDescriptor), script, script, true);
  920. }
  921. public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags) {
  922. RegisterStartupScript ((Control) page, type, key, script, addScriptTags);
  923. }
  924. public static void RegisterStartupScript (Control control, Type type, string key, string script, bool addScriptTags) {
  925. Page page = control.Page;
  926. ScriptManager sm = GetCurrentInternal (page);
  927. if (sm == null)
  928. return;
  929. RegisterScript (ref sm._startupScriptBlocks, control, type, key, script, null, addScriptTags, RegisteredScriptType.ClientStartupScript);
  930. if (!sm.IsInAsyncPostBack)
  931. page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
  932. }
  933. public void RegisterScriptControl<TScriptControl> (TScriptControl scriptControl) where TScriptControl : Control, IScriptControl {
  934. if (scriptControl == null)
  935. throw new ArgumentNullException ("scriptControl");
  936. if (_registeredScriptControls == null)
  937. _registeredScriptControls = new List<IScriptControl> ();
  938. if (!_registeredScriptControls.Contains (scriptControl))
  939. _registeredScriptControls.Add (scriptControl);
  940. }
  941. public void RegisterExtenderControl<TExtenderControl> (TExtenderControl extenderControl, Control targetControl) where TExtenderControl : Control, IExtenderControl {
  942. if (extenderControl == null)
  943. throw new ArgumentNullException ("extenderControl");
  944. if (targetControl == null)
  945. throw new ArgumentNullException ("targetControl");
  946. if (_registeredExtenderControls == null)
  947. _registeredExtenderControls = new Dictionary<IExtenderControl, Control> ();
  948. if (!_registeredExtenderControls.ContainsKey (extenderControl))
  949. _registeredExtenderControls.Add (extenderControl, targetControl);
  950. }
  951. static void RegisterScript (ref List<RegisteredScript> scriptList, Control control, Type type, string key, string script, string url, bool addScriptTag, RegisteredScriptType scriptType) {
  952. if (scriptList == null)
  953. scriptList = new List<RegisteredScript> ();
  954. scriptList.Add (new RegisteredScript (control, type, key, script, url, addScriptTag, scriptType));
  955. }
  956. protected internal override void Render (HtmlTextWriter writer) {
  957. // MSDN: This method is used by control developers to extend the ScriptManager control.
  958. // Notes to Inheritors:
  959. // When overriding this method, call the base Render(HtmlTextWriter) method
  960. // so that PageRequestManager is rendered on the page.
  961. if (SupportsPartialRendering) {
  962. writer.WriteLine ("<script type=\"text/javascript\">");
  963. writer.WriteLine ("//<![CDATA[");
  964. writer.WriteLine ("Sys.WebForms.PageRequestManager._initialize('{0}', document.getElementById('{1}'));", UniqueID, Page.Form.ClientID);
  965. if (IsMultiForm)
  966. writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance($get(\"{0}\"))._updateControls([{1}], [{2}], [{3}], {4});",
  967. Page.Form.ClientID,
  968. FormatUpdatePanelIDs (_updatePanels, true),
  969. FormatListIDs (_asyncPostBackControls, true, false),
  970. FormatListIDs (_postBackControls, true, false),
  971. AsyncPostBackTimeout);
  972. else
  973. writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance()._updateControls([{0}], [{1}], [{2}], {3});",
  974. FormatUpdatePanelIDs (_updatePanels, true),
  975. FormatListIDs (_asyncPostBackControls, true, false),
  976. FormatListIDs (_postBackControls, true, false),
  977. AsyncPostBackTimeout);
  978. writer.WriteLine ("//]]");
  979. writer.WriteLine ("</script>");
  980. }
  981. base.Render (writer);
  982. }
  983. static string FormatUpdatePanelIDs (List<UpdatePanel> list, bool useSingleQuote) {
  984. if (list == null || list.Count == 0)
  985. return null;
  986. StringBuilder sb = new StringBuilder ();
  987. foreach (UpdatePanel panel in list) {
  988. if (!panel.Visible)
  989. continue;
  990. sb.AppendFormat ("{0}{1}{2}{0},", useSingleQuote ? "'" : String.Empty, panel.ChildrenAsTriggers ? "t" : "f", panel.UniqueID);
  991. }
  992. if (sb.Length > 0)
  993. sb.Length--;
  994. return sb.ToString ();
  995. }
  996. static string FormatListIDs<T> (List<T> list, bool useSingleQuote, bool skipInvisible) where T : Control
  997. {
  998. if (list == null || list.Count == 0)
  999. return null;
  1000. StringBuilder sb = new StringBuilder ();
  1001. for (int i = 0; i < list.Count; i++) {
  1002. if (skipInvisible && !list [i].Visible)
  1003. continue;
  1004. sb.AppendFormat ("{0}{1}{0},", useSingleQuote ? "'" : String.Empty, list [i].UniqueID);
  1005. }
  1006. if (sb.Length > 0)
  1007. sb.Length--;
  1008. return sb.ToString ();
  1009. }
  1010. public void SetFocus (Control control) {
  1011. if (control == null)
  1012. throw new ArgumentNullException ("control");
  1013. if (IsInAsyncPostBack) {
  1014. EnsureFocusClientScript ();
  1015. _controlIDToFocus = control.ClientID;
  1016. }
  1017. else
  1018. Page.SetFocus (control);
  1019. }
  1020. public void SetFocus (string clientID) {
  1021. if (String.IsNullOrEmpty (clientID))
  1022. throw new ArgumentNullException ("control");
  1023. if (IsInAsyncPostBack) {
  1024. EnsureFocusClientScript ();
  1025. _controlIDToFocus = clientID;
  1026. }
  1027. else
  1028. Page.SetFocus (clientID);
  1029. }
  1030. void EnsureFocusClientScript () {
  1031. #if TARGET_DOTNET
  1032. RegisterClientScriptResource (this, typeof (ClientScriptManager), "Focus.js");
  1033. #endif
  1034. }
  1035. #region IPostBackDataHandler Members
  1036. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection) {
  1037. return LoadPostData (postDataKey, postCollection);
  1038. }
  1039. void IPostBackDataHandler.RaisePostDataChangedEvent () {
  1040. RaisePostDataChangedEvent ();
  1041. }
  1042. #endregion
  1043. internal static void WriteCallbackException (ScriptManager current, TextWriter output, Exception ex, bool writeMessage)
  1044. {
  1045. #if TARGET_DOTNET
  1046. if (ex is HttpUnhandledException)
  1047. ex = ex.InnerException;
  1048. #endif
  1049. HttpException httpEx = ex as HttpException;
  1050. string message = current != null ? current.AsyncPostBackErrorMessage : null;
  1051. if (String.IsNullOrEmpty (message) && writeMessage) {
  1052. if ((current != null && current.IsDebuggingEnabled) || (current == null && RuntimeHelpers.DebuggingEnabled))
  1053. message = ex.ToString ();
  1054. else
  1055. message = ex.Message;
  1056. }
  1057. WriteCallbackOutput (output, error, httpEx == null ? "500" : httpEx.GetHttpCode ().ToString (), message);
  1058. }
  1059. static internal void WriteCallbackRedirect (TextWriter output, string redirectUrl) {
  1060. WriteCallbackOutput (output, pageRedirect, null, redirectUrl);
  1061. }
  1062. void RegisterPanelForRefresh (UpdatePanel panel)
  1063. {
  1064. if (_panelsToRefresh == null)
  1065. _panelsToRefresh = new List<UpdatePanel> ();
  1066. else if (_panelsToRefresh.Contains (panel))
  1067. return;
  1068. _panelsToRefresh.Add (panel);
  1069. }
  1070. internal void WriteCallbackPanel (TextWriter output, UpdatePanel panel, StringBuilder panelOutput)
  1071. {
  1072. WriteCallbackOutput (output, updatePanel, panel.ClientID, panelOutput);
  1073. }
  1074. internal void RegisterChildUpdatePanel (UpdatePanel updatePanel) {
  1075. if (_childUpdatePanels == null)
  1076. _childUpdatePanels = new List<UpdatePanel> ();
  1077. _childUpdatePanels.Add (updatePanel);
  1078. }
  1079. static void WriteCallbackOutput (TextWriter output, string type, string name, object value) {
  1080. string str = value as string;
  1081. StringBuilder sb = str == null ? value as StringBuilder : null;
  1082. int length = 0;
  1083. if (str != null)
  1084. length = str.Length;
  1085. else if (sb != null)
  1086. length = sb.Length;
  1087. //output.Write ("{0}|{1}|{2}|{3}|", value == null ? 0 : value.Length, type, name, value);
  1088. output.Write (length);
  1089. output.Write ('|');
  1090. output.Write (type);
  1091. output.Write ('|');
  1092. output.Write (name);
  1093. output.Write ('|');
  1094. for (int i = 0; i < length; i++)
  1095. if (str != null)
  1096. output.Write (str [i]);
  1097. else
  1098. output.Write (sb [i]);
  1099. output.Write ('|');
  1100. }
  1101. void RenderPageCallback (HtmlTextWriter output, Control container)
  1102. {
  1103. Page page = (Page) container;
  1104. // MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx
  1105. //
  1106. // Refreshing UpdatePanel Content
  1107. //
  1108. // When partial-page rendering is enabled, a control can perform a postback
  1109. // that updates the whole page or an asynchronous postback that updates the
  1110. // content of one or more UpdatePanel controls. Whether a control causes an
  1111. // asynchronous postback and updates an UpdatePanel control depends on the
  1112. // following:
  1113. //
  1114. // * If the UpdateMode property of the UpdatePanel control is set to Always,
  1115. // the UpdatePanel control's content is updated on every postback that
  1116. // originates from the page. This includes asynchronous postbacks from
  1117. // controls that are inside other UpdatePanel controls and postbacks from
  1118. // controls that are not inside UpdatePanel controls.
  1119. //
  1120. // * If the UpdateMode property is set to Conditional, the UpdatePanel
  1121. // control's content is updated in the following circumstances:
  1122. //
  1123. // o When you call the Update method of the UpdatePanel control
  1124. // explicitly.
  1125. // o When the UpdatePanel control is nested inside another UpdatePanel
  1126. // control, and the parent panel is updated.
  1127. // o When a postback is caused by a control that is defined as a
  1128. // trigger by using the Triggers property of the UpdatePanel
  1129. // control. In this scenario, the control explicitly triggers an
  1130. // update of the panel content. The control can be either inside or
  1131. // outside the UpdatePanel control that the trigger is associated
  1132. // with.
  1133. // o When the ChildrenAsTriggers property is set to true and a child
  1134. // control of the UpdatePanel control causes a postback. Child
  1135. // controls of nested UpdatePanel controls do not cause an update to
  1136. // the outer UpdatePanel control unless they are explicitly defined
  1137. // as triggers.
  1138. //
  1139. if (_updatePanels != null && _updatePanels.Count > 0) {
  1140. bool needsUpdate;
  1141. foreach (UpdatePanel panel in _updatePanels) {
  1142. if (panel.RequiresUpdate || (!String.IsNullOrEmpty (_panelToRefreshID) && String.Compare (_panelToRefreshID, panel.UniqueID, StringComparison.Ordinal) == 0))
  1143. needsUpdate = true;
  1144. else
  1145. needsUpdate = false;
  1146. panel.SetInPartialRendering (needsUpdate);
  1147. if (needsUpdate)
  1148. RegisterPanelForRefresh (panel);
  1149. }
  1150. }
  1151. page.Form.SetRenderMethodDelegate (RenderFormCallback);
  1152. HtmlTextParser parser = new HtmlTextParser (output);
  1153. page.Form.RenderControl (parser);
  1154. Dictionary <string, string> pageHiddenFields = parser.HiddenFields;
  1155. if (pageHiddenFields != null)
  1156. foreach (KeyValuePair <string, string> kvp in pageHiddenFields)
  1157. WriteCallbackOutput (output, hiddenField, kvp.Key, kvp.Value);
  1158. WriteCallbackOutput (output, asyncPostBackControlIDs, null, FormatListIDs (_asyncPostBackControls, false, false));
  1159. WriteCallbackOutput (output, postBackControlIDs, null, FormatListIDs (_postBackControls, false, false));
  1160. WriteCallbackOutput (output, updatePanelIDs, null, FormatUpdatePanelIDs (_updatePanels, false));
  1161. WriteCallbackOutput (output, childUpdatePanelIDs, null, FormatListIDs (_childUpdatePanels, false, true));
  1162. WriteCallbackOutput (output, panelsToRefreshIDs, null, FormatListIDs (_panelsToRefresh, false, true));
  1163. WriteCallbackOutput (output, asyncPostBackTimeout, null, AsyncPostBackTimeout.ToString ());
  1164. if (!IsMultiForm)
  1165. WriteCallbackOutput (output, pageTitle, null, Page.Title);
  1166. if (_dataItems != null)
  1167. foreach (Control control in _dataItems.Keys) {
  1168. DataItemEntry entry = _dataItems [control];
  1169. WriteCallbackOutput (output, entry.IsJsonSerialized ? dataItemJson : dataItem, control.ClientID, entry.DataItem);
  1170. }
  1171. WriteArrayDeclarations (output);
  1172. WriteExpandoAttributes (output);
  1173. WriteScriptBlocks (output, _clientScriptBlocks);
  1174. WriteScriptBlocks (output, _startupScriptBlocks);
  1175. WriteScriptBlocks (output, _onSubmitStatements);
  1176. WriteHiddenFields (output);
  1177. if (!String.IsNullOrEmpty (_controlIDToFocus))
  1178. WriteCallbackOutput (output, focus, null, _controlIDToFocus);
  1179. if (_disposeScripts != null)
  1180. for (int i = 0; i < _disposeScripts.Count; i++) {
  1181. RegisteredDisposeScript entry = _disposeScripts [i];
  1182. if ((_panelsToRefresh != null && _panelsToRefresh.IndexOf (entry.UpdatePanel) >= 0) || (_childUpdatePanels != null && _childUpdatePanels.IndexOf (entry.UpdatePanel) >= 0))
  1183. WriteCallbackOutput (output, scriptDispose, entry.UpdatePanel.ClientID, entry.Script);
  1184. }
  1185. }
  1186. private void WriteExpandoAttributes (HtmlTextWriter writer) {
  1187. if (_expandoAttributes != null) {
  1188. for (int i = 0; i < _expandoAttributes.Count; i++) {
  1189. RegisteredExpandoAttribute attr = _expandoAttributes [i];
  1190. if (HasBeenRendered (attr.Control)) {
  1191. string value;
  1192. if (attr.Encode) {
  1193. StringWriter sw = new StringWriter ();
  1194. Newtonsoft.Json.JavaScriptUtils.WriteEscapedJavaScriptString (attr.Value, sw);
  1195. value = sw.ToString ();
  1196. }
  1197. else
  1198. value = "\"" + attr.Value + "\"";
  1199. WriteCallbackOutput (writer, expando, "document.getElementById('" + attr.ControlId + "')['" + attr.Name + "']", value);
  1200. }
  1201. }
  1202. }
  1203. }
  1204. void WriteArrayDeclarations (HtmlTextWriter writer) {
  1205. if (_arrayDeclarations != null) {
  1206. for (int i = 0; i < _arrayDeclarations.Count; i++) {
  1207. RegisteredArrayDeclaration array = _arrayDeclarations [i];
  1208. if (Page == array.Control || HasBeenRendered (array.Control))
  1209. WriteCallbackOutput (writer, arrayDeclaration, array.Name, array.Value);
  1210. }
  1211. }
  1212. }
  1213. void WriteScriptBlocks (HtmlTextWriter output, List<RegisteredScript> scriptList) {
  1214. if (scriptList == null)
  1215. return;
  1216. var registeredScripts = new Dictionary <string, RegisteredScript> ();
  1217. Control control;
  1218. Page page = Page;
  1219. for (int i = 0; i < scriptList.Count; i++) {
  1220. RegisteredScript scriptEntry = scriptList [i];
  1221. if (registeredScripts.ContainsKey (scriptEntry.Key))
  1222. continue;
  1223. control = scriptEntry.Control;
  1224. if (page == control || HasBeenRendered (control)) {
  1225. registeredScripts.Add (scriptEntry.Key, scriptEntry);
  1226. switch (scriptEntry.ScriptType) {
  1227. case RegisteredScriptType.ClientScriptBlock:
  1228. if (scriptEntry.AddScriptTags)
  1229. WriteCallbackOutput (output, scriptBlock, scriptContentNoTags, scriptEntry.Script);
  1230. else
  1231. WriteCallbackOutput (output, scriptBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
  1232. break;
  1233. #if NET_3_5
  1234. case RegisteredScriptType.ClientStartupScript:
  1235. if (scriptEntry.AddScriptTags)
  1236. WriteCallbackOutput (output, scriptStartupBlock, scriptContentNoTags, scriptEntry.Script);
  1237. else
  1238. WriteCallbackOutput (output, scriptStartupBlock, scriptContentWithTags, SerializeScriptBlock (scriptEntry));
  1239. break;
  1240. #endif
  1241. case RegisteredScriptType.ClientScriptInclude:
  1242. WriteCallbackOutput (output, scriptBlock, scriptPath, scriptEntry.Url);
  1243. break;
  1244. case RegisteredScriptType.OnSubmitStatement:
  1245. WriteCallbackOutput (output, onSubmit, null, scriptEntry.Script);
  1246. break;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. void WriteHiddenFields (HtmlTextWriter output) {
  1252. if (_hiddenFields == null)
  1253. return;
  1254. Hashtable registeredFields = new Hashtable ();
  1255. for (int i = 0; i < _hiddenFields.Count; i++) {
  1256. RegisteredHiddenField field = _hiddenFields [i];
  1257. if (registeredFields.ContainsKey (field.Name))
  1258. continue;
  1259. if (Page == field.Control || HasBeenRendered (field.Control)) {
  1260. registeredFields.Add (field.Name, field);
  1261. WriteCallbackOutput (output, hiddenField, field.Name, field.InitialValue);
  1262. }
  1263. }
  1264. }
  1265. static string SerializeScriptBlock (RegisteredScript scriptEntry) {
  1266. try {
  1267. XmlTextReader reader = new XmlTextReader (new StringReader (scriptEntry.Script));
  1268. while (reader.Read ()) {
  1269. switch (reader.NodeType) {
  1270. case XmlNodeType.Element:
  1271. if (String.Compare ("script", reader.Name, StringComparison.OrdinalIgnoreCase) == 0) {
  1272. Dictionary<string, string> dic = new Dictionary<string, string> ();
  1273. while (reader.MoveToNextAttribute ()) {
  1274. dic.Add (reader.Name, reader.Value);
  1275. }
  1276. reader.MoveToContent ();
  1277. dic.Add ("text", reader.ReadInnerXml ());
  1278. return JavaScriptSerializer.DefaultSerializer.Serialize (dic);
  1279. }
  1280. break;
  1281. default:
  1282. continue;
  1283. }
  1284. }
  1285. }
  1286. catch {
  1287. }
  1288. 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));
  1289. }
  1290. void RenderFormCallback (HtmlTextWriter output, Control container)
  1291. {
  1292. output = ((HtmlTextParser) output).ResponseOutput;
  1293. HtmlForm form = (HtmlForm) container;
  1294. HtmlTextWriter writer = new HtmlDropWriter (output);
  1295. if (form.HasControls ()) {
  1296. foreach (Control control in form.Controls)
  1297. control.RenderControl (writer);
  1298. }
  1299. }
  1300. internal class AlternativeHtmlTextWriter : HtmlTextWriter
  1301. {
  1302. readonly HtmlTextWriter _responseOutput;
  1303. public HtmlTextWriter ResponseOutput {
  1304. get { return _responseOutput; }
  1305. }
  1306. public AlternativeHtmlTextWriter (TextWriter writer, HtmlTextWriter responseOutput)
  1307. : base (writer) {
  1308. _responseOutput = responseOutput;
  1309. }
  1310. }
  1311. sealed class HtmlTextParser : AlternativeHtmlTextWriter
  1312. {
  1313. bool _done;
  1314. public Dictionary <string, string> _hiddenFields;
  1315. public Dictionary <string, string> HiddenFields {
  1316. get { return _hiddenFields; }
  1317. }
  1318. public HtmlTextParser (HtmlTextWriter responseOutput)
  1319. : this (new TextParser (responseOutput), responseOutput) {
  1320. }
  1321. HtmlTextParser (TextParser parser, HtmlTextWriter responseOutput)
  1322. : base (parser, responseOutput)
  1323. {
  1324. parser.HiddenFieldParsed += new TextParser.TextParserHiddenFieldParsedEventHandler (OnHiddenFieldParsed);
  1325. }
  1326. void OnHiddenFieldParsed (TextParser sender, TextParserHiddenFieldParsedEventArgs args)
  1327. {
  1328. if (_hiddenFields == null)
  1329. _hiddenFields = new Dictionary <string, string> ();
  1330. _hiddenFields [args.Name] = args.Value;
  1331. }
  1332. public override void WriteAttribute (string name, string value) {
  1333. if (!_done && String.Compare ("action", name, StringComparison.OrdinalIgnoreCase) == 0) {
  1334. _done = true;
  1335. ScriptManager.WriteCallbackOutput (ResponseOutput, formAction, null, value);
  1336. return;
  1337. }
  1338. base.WriteAttribute (name, value);
  1339. }
  1340. }
  1341. sealed class TextParserHiddenFieldParsedEventArgs : EventArgs
  1342. {
  1343. public string Name {
  1344. get;
  1345. private set;
  1346. }
  1347. public string Value {
  1348. get;
  1349. private set;
  1350. }
  1351. public TextParserHiddenFieldParsedEventArgs (string name, string value)
  1352. {
  1353. this.Name = name;
  1354. this.Value = value;
  1355. }
  1356. }
  1357. sealed class TextParser : TextWriter
  1358. {
  1359. public delegate void TextParserHiddenFieldParsedEventHandler (TextParser sender, TextParserHiddenFieldParsedEventArgs args);
  1360. static object textParserHiddenFieldParsedEvent = new object ();
  1361. int _state;
  1362. char _charState = (char) 255;
  1363. const char nullCharState = (char) 255;
  1364. StringBuilder _sb = new StringBuilder ();
  1365. Dictionary<string, string> _currentField;
  1366. string _currentAttribute;
  1367. readonly HtmlTextWriter _responseOutput;
  1368. TextParserHiddenFieldParsedEventHandler _hiddenFieldParsedHandler;
  1369. public event TextParserHiddenFieldParsedEventHandler HiddenFieldParsed {
  1370. add { _hiddenFieldParsedHandler = value; }
  1371. remove { _hiddenFieldParsedHandler = null; }
  1372. }
  1373. public override Encoding Encoding {
  1374. get { return Encoding.UTF8; }
  1375. }
  1376. public TextParser (HtmlTextWriter responseOutput) {
  1377. _responseOutput = responseOutput;
  1378. }
  1379. public override void Write (char value) {
  1380. switch (_state) {
  1381. case 0:
  1382. ParseBeginTag (value);
  1383. break;
  1384. case 1:
  1385. ParseAttributeName (value);
  1386. break;
  1387. case 2:
  1388. ParseAttributeValue (value);
  1389. break;
  1390. }
  1391. }
  1392. private void ParseAttributeValue (char value) {
  1393. switch (value) {
  1394. case '>':
  1395. ResetState ();
  1396. break;
  1397. case '"':
  1398. _currentField.Add (_currentAttribute, _sb.ToString ());
  1399. _state = 1;
  1400. _sb.Length = 0;
  1401. ProbeWriteOutput ();
  1402. break;
  1403. default:
  1404. _sb.Append (value);
  1405. break;
  1406. }
  1407. }
  1408. private void ParseAttributeName (char value) {
  1409. switch (value) {
  1410. case '>':
  1411. ResetState ();
  1412. break;
  1413. case ' ':
  1414. case '=':
  1415. break;
  1416. case '"':
  1417. _currentAttribute = _sb.ToString ();
  1418. _state = 2;
  1419. _sb.Length = 0;
  1420. break;
  1421. default:
  1422. _sb.Append (value);
  1423. break;
  1424. }
  1425. }
  1426. void ParseBeginTag (char value) {
  1427. switch (_charState) {
  1428. case nullCharState:
  1429. if (value == '<')
  1430. _charState = value;
  1431. break;
  1432. case '<':
  1433. if (value == 'i')
  1434. _charState = value;
  1435. else
  1436. ResetState ();
  1437. break;
  1438. case 'i':
  1439. if (value == 'n')
  1440. _charState = value;
  1441. else
  1442. ResetState ();
  1443. break;
  1444. case 'n':
  1445. if (value == 'p')
  1446. _charState = value;
  1447. else
  1448. ResetState ();
  1449. break;
  1450. case 'p':
  1451. if (value == 'u')
  1452. _charState = value;
  1453. else
  1454. ResetState ();
  1455. break;
  1456. case 'u':
  1457. if (value == 't')
  1458. _charState = value;
  1459. else
  1460. ResetState ();
  1461. break;
  1462. case 't':
  1463. if (value == ' ') {
  1464. _state = 1;
  1465. _currentField = new Dictionary<string, string> ();
  1466. }
  1467. else
  1468. ResetState ();
  1469. break;
  1470. }
  1471. }
  1472. private void ResetState () {
  1473. _charState = nullCharState;
  1474. _state = 0;
  1475. _sb.Length = 0;
  1476. }
  1477. private void ProbeWriteOutput () {
  1478. if (!_currentField.ContainsKey ("name"))
  1479. return;
  1480. if (!_currentField.ContainsKey ("value"))
  1481. return;
  1482. string value = _currentField ["value"];
  1483. if (value == null)
  1484. return;
  1485. if (_hiddenFieldParsedHandler != null)
  1486. _hiddenFieldParsedHandler (this, new TextParserHiddenFieldParsedEventArgs (_currentField ["name"], HttpUtility.HtmlDecode (value)));
  1487. else
  1488. ScriptManager.WriteCallbackOutput (_responseOutput, hiddenField, _currentField ["name"], HttpUtility.HtmlDecode (value));
  1489. }
  1490. }
  1491. sealed class HtmlDropWriter : AlternativeHtmlTextWriter
  1492. {
  1493. public HtmlDropWriter (HtmlTextWriter responseOutput)
  1494. : base (new DropWriter (), responseOutput) {
  1495. }
  1496. }
  1497. sealed class DropWriter : TextWriter
  1498. {
  1499. public override Encoding Encoding {
  1500. get { return Encoding.UTF8; }
  1501. }
  1502. }
  1503. sealed class CultureInfoSerializer
  1504. {
  1505. readonly CultureInfo _ci;
  1506. public CultureInfoSerializer (CultureInfo ci) {
  1507. if (ci == null)
  1508. throw new ArgumentNullException ("ci");
  1509. _ci = ci;
  1510. }
  1511. public string name {
  1512. get { return _ci.Name; }
  1513. }
  1514. public NumberFormatInfo numberFormat {
  1515. get { return _ci.NumberFormat; }
  1516. }
  1517. public DateTimeFormatInfo dateTimeFormat {
  1518. get { return _ci.DateTimeFormat; }
  1519. }
  1520. }
  1521. sealed class ScriptReferenceEntry
  1522. {
  1523. readonly Control _control;
  1524. readonly ScriptReference _scriptReference;
  1525. readonly bool _loadBeforeUI;
  1526. public Control Control { get { return _control; } }
  1527. public ScriptReference ScriptReference { get { return _scriptReference; } }
  1528. public bool LoadScriptsBeforeUI { get { return _loadBeforeUI; } }
  1529. public ScriptReferenceEntry (Control control, ScriptReference scriptReference, bool loadBeforeUI) {
  1530. _control = control;
  1531. _scriptReference = scriptReference;
  1532. _loadBeforeUI = loadBeforeUI;
  1533. }
  1534. }
  1535. sealed class DataItemEntry
  1536. {
  1537. readonly string _dataItem;
  1538. readonly bool _isJsonSerialized;
  1539. public string DataItem { get { return _dataItem; } }
  1540. public bool IsJsonSerialized { get { return _isJsonSerialized; } }
  1541. public DataItemEntry (string dataItem, bool isJsonSerialized) {
  1542. _dataItem = dataItem;
  1543. _isJsonSerialized = isJsonSerialized;
  1544. }
  1545. }
  1546. internal void RegisterProxy (ScriptManagerProxy scriptManagerProxy) {
  1547. if (_proxies == null)
  1548. _proxies = new List<ScriptManagerProxy> ();
  1549. _proxies.Add (scriptManagerProxy);
  1550. }
  1551. #region IScriptManager Members
  1552. void IScriptManager.RegisterOnSubmitStatementExternal (Control control, Type type, string key, string script) {
  1553. RegisterOnSubmitStatement (control, type, key, script);
  1554. }
  1555. void IScriptManager.RegisterExpandoAttributeExternal (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
  1556. RegisterExpandoAttribute (control, controlId, attributeName, attributeValue, encode);
  1557. }
  1558. void IScriptManager.RegisterHiddenFieldExternal (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
  1559. RegisterHiddenField (control, hiddenFieldName, hiddenFieldInitialValue);
  1560. }
  1561. void IScriptManager.RegisterStartupScriptExternal (Control control, Type type, string key, string script, bool addScriptTags) {
  1562. RegisterStartupScript (control, type, key, script, addScriptTags);
  1563. }
  1564. void IScriptManager.RegisterArrayDeclarationExternal (Control control, string arrayName, string arrayValue) {
  1565. RegisterArrayDeclaration (control, arrayName, arrayValue);
  1566. }
  1567. void IScriptManager.RegisterClientScriptBlockExternal (Control control, Type type, string key, string script, bool addScriptTags) {
  1568. RegisterClientScriptBlock (control, type, key, script, addScriptTags);
  1569. }
  1570. void IScriptManager.RegisterClientScriptIncludeExternal (Control control, Type type, string key, string url) {
  1571. RegisterClientScriptInclude (control, type, key, url);
  1572. }
  1573. void IScriptManager.RegisterClientScriptResourceExternal (Control control, Type type, string resourceName) {
  1574. RegisterClientScriptResource (control, type, resourceName);
  1575. }
  1576. #endregion
  1577. }
  1578. }