ScriptManager.cs 60 KB

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