ScriptManager.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. //
  2. // ScriptManager.cs
  3. //
  4. // Author:
  5. // Igor Zelmanovich <[email protected]>
  6. //
  7. // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.ComponentModel;
  33. using System.Security.Permissions;
  34. using System.Collections.Specialized;
  35. using System.Collections;
  36. using System.Web.Handlers;
  37. using System.Reflection;
  38. using System.Web.Configuration;
  39. using System.Web.UI.HtmlControls;
  40. using System.IO;
  41. using System.Globalization;
  42. using System.Threading;
  43. using System.Web.Script.Serialization;
  44. using System.Web.Script.Services;
  45. namespace System.Web.UI
  46. {
  47. [ParseChildrenAttribute (true)]
  48. [DefaultPropertyAttribute ("Scripts")]
  49. [DesignerAttribute ("System.Web.UI.Design.ScriptManagerDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  50. [NonVisualControlAttribute]
  51. [PersistChildrenAttribute (false)]
  52. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  53. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  54. public class ScriptManager : Control, IPostBackDataHandler
  55. {
  56. // the keywords are used in fomatting async response
  57. const string updatePanel = "updatePanel";
  58. const string hiddenField = "hiddenField";
  59. const string arrayDeclaration = "arrayDeclaration";
  60. const string scriptBlock = "scriptBlock";
  61. const string expando = "expando";
  62. const string onSubmit = "onSubmit";
  63. const string asyncPostBackControlIDs = "asyncPostBackControlIDs";
  64. const string postBackControlIDs = "postBackControlIDs";
  65. const string updatePanelIDs = "updatePanelIDs";
  66. const string asyncPostBackTimeout = "asyncPostBackTimeout";
  67. const string childUpdatePanelIDs = "childUpdatePanelIDs";
  68. const string panelsToRefreshIDs = "panelsToRefreshIDs";
  69. const string formAction = "formAction";
  70. const string dataItem = "dataItem";
  71. const string dataItemJson = "dataItemJson";
  72. const string scriptDispose = "scriptDispose";
  73. const string pageRedirect = "pageRedirect";
  74. const string error = "error";
  75. const string pageTitle = "pageTitle";
  76. const string focus = "focus";
  77. const string scriptContentNoTags = "ScriptContentNoTags";
  78. const string scriptContentWithTags = "ScriptContentWithTags";
  79. const string scriptPath = "ScriptPath";
  80. static JavaScriptSerializer _cultureInfoSerializer;
  81. int _asyncPostBackTimeout = 90;
  82. List<Control> _asyncPostBackControls;
  83. List<Control> _postBackControls;
  84. List<UpdatePanel> _childUpdatePanels;
  85. List<UpdatePanel> _panelsToRefresh;
  86. List<UpdatePanel> _updatePanels;
  87. ScriptReferenceCollection _scripts;
  88. bool _isInAsyncPostBack;
  89. bool _isInPartialRendering;
  90. string _asyncPostBackSourceElementID;
  91. ScriptMode _scriptMode = ScriptMode.Auto;
  92. bool _enableScriptGlobalization;
  93. bool _enableScriptLocalization;
  94. string _scriptPath;
  95. ScriptEntry _clientScriptBlocks;
  96. ScriptEntry _startupScriptBlocks;
  97. ScriptEntry _scriptIncludes;
  98. ScriptEntry _onSubmitStatements;
  99. List<ArrayDeclaration> _arrayDeclarations;
  100. Hashtable _hiddenFields;
  101. List<IScriptControl> _registeredScriptControls;
  102. Dictionary<IExtenderControl, Control> _registeredExtenderControls;
  103. bool? _supportsPartialRendering;
  104. bool _enablePartialRendering = true;
  105. bool _init;
  106. string _panelToRefreshID;
  107. Dictionary<Control, DataItemEntry> _dataItems;
  108. bool _enablePageMethods;
  109. [DefaultValue (true)]
  110. [Category ("Behavior")]
  111. public bool AllowCustomErrorsRedirect {
  112. get {
  113. throw new NotImplementedException ();
  114. }
  115. set {
  116. throw new NotImplementedException ();
  117. }
  118. }
  119. [Category ("Behavior")]
  120. [DefaultValue ("")]
  121. public string AsyncPostBackErrorMessage {
  122. get {
  123. throw new NotImplementedException ();
  124. }
  125. set {
  126. throw new NotImplementedException ();
  127. }
  128. }
  129. [Browsable (false)]
  130. public string AsyncPostBackSourceElementID {
  131. get {
  132. if (_asyncPostBackSourceElementID == null)
  133. return String.Empty;
  134. return _asyncPostBackSourceElementID;
  135. }
  136. }
  137. [DefaultValue (90)]
  138. [Category ("Behavior")]
  139. public int AsyncPostBackTimeout {
  140. get {
  141. return _asyncPostBackTimeout;
  142. }
  143. set {
  144. _asyncPostBackTimeout = value;
  145. }
  146. }
  147. [Category ("Behavior")]
  148. [MergableProperty (false)]
  149. [DefaultValue ("")]
  150. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  151. [PersistenceMode (PersistenceMode.InnerProperty)]
  152. public AuthenticationServiceManager AuthenticationService {
  153. get {
  154. throw new NotImplementedException ();
  155. }
  156. }
  157. [Category ("Behavior")]
  158. [DefaultValue (false)]
  159. public bool EnablePageMethods {
  160. get {
  161. return _enablePageMethods;
  162. }
  163. set {
  164. _enablePageMethods = value;
  165. }
  166. }
  167. [DefaultValue (true)]
  168. [Category ("Behavior")]
  169. public bool EnablePartialRendering {
  170. get {
  171. return _enablePartialRendering;
  172. }
  173. set {
  174. if (_init)
  175. throw new InvalidOperationException ();
  176. _enablePartialRendering = value;
  177. }
  178. }
  179. [DefaultValue (false)]
  180. [Category ("Behavior")]
  181. public bool EnableScriptGlobalization {
  182. get {
  183. return _enableScriptGlobalization;
  184. }
  185. set {
  186. _enableScriptGlobalization = value;
  187. }
  188. }
  189. [Category ("Behavior")]
  190. [DefaultValue (false)]
  191. public bool EnableScriptLocalization {
  192. get {
  193. return _enableScriptLocalization;
  194. }
  195. set {
  196. _enableScriptLocalization = value;
  197. }
  198. }
  199. [Browsable (false)]
  200. public bool IsDebuggingEnabled {
  201. get {
  202. if (IsDeploymentRetail)
  203. return false;
  204. #if !TARGET_J2EE
  205. CompilationSection compilation = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  206. if (!compilation.Debug && (ScriptMode == ScriptMode.Auto || ScriptMode == ScriptMode.Inherit))
  207. return false;
  208. #endif
  209. if (ScriptMode == ScriptMode.Release)
  210. return false;
  211. return true;
  212. }
  213. }
  214. bool IsDeploymentRetail {
  215. get {
  216. #if TARGET_J2EE
  217. return false;
  218. #else
  219. DeploymentSection deployment = (DeploymentSection) WebConfigurationManager.GetSection ("system.web/deployment");
  220. return deployment.Retail;
  221. #endif
  222. }
  223. }
  224. [Browsable (false)]
  225. public bool IsInAsyncPostBack {
  226. get {
  227. return _isInAsyncPostBack;
  228. }
  229. }
  230. internal bool IsInPartialRendering {
  231. get {
  232. return _isInPartialRendering;
  233. }
  234. set {
  235. _isInPartialRendering = value;
  236. }
  237. }
  238. [Category ("Behavior")]
  239. [DefaultValue (true)]
  240. public bool LoadScriptsBeforeUI {
  241. get {
  242. throw new NotImplementedException ();
  243. }
  244. set {
  245. throw new NotImplementedException ();
  246. }
  247. }
  248. [PersistenceMode (PersistenceMode.InnerProperty)]
  249. [DefaultValue ("")]
  250. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  251. [Category ("Behavior")]
  252. [MergableProperty (false)]
  253. public ProfileServiceManager ProfileService {
  254. get {
  255. throw new NotImplementedException ();
  256. }
  257. }
  258. [Category ("Behavior")]
  259. #if TARGET_J2EE
  260. [MonoLimitation ("The 'Auto' value is the same as 'Debug'.")]
  261. #endif
  262. public ScriptMode ScriptMode {
  263. get {
  264. return _scriptMode;
  265. }
  266. set {
  267. if (value == ScriptMode.Inherit)
  268. value = ScriptMode.Auto;
  269. _scriptMode = value;
  270. }
  271. }
  272. [DefaultValue ("")]
  273. [Category ("Behavior")]
  274. public string ScriptPath {
  275. get {
  276. if (_scriptPath == null)
  277. return String.Empty;
  278. return _scriptPath;
  279. }
  280. set {
  281. _scriptPath = value;
  282. }
  283. }
  284. [PersistenceMode (PersistenceMode.InnerProperty)]
  285. [DefaultValue ("")]
  286. [Category ("Behavior")]
  287. [MergableProperty (false)]
  288. public ScriptReferenceCollection Scripts {
  289. get {
  290. if (_scripts == null)
  291. _scripts = new ScriptReferenceCollection ();
  292. return _scripts;
  293. }
  294. }
  295. [PersistenceMode (PersistenceMode.InnerProperty)]
  296. [DefaultValue ("")]
  297. [MergableProperty (false)]
  298. [Category ("Behavior")]
  299. public ServiceReferenceCollection Services {
  300. get {
  301. throw new NotImplementedException ();
  302. }
  303. }
  304. [DefaultValue (true)]
  305. [Browsable (false)]
  306. public bool SupportsPartialRendering {
  307. get {
  308. if (!_supportsPartialRendering.HasValue)
  309. _supportsPartialRendering = CheckSupportsPartialRendering ();
  310. return _supportsPartialRendering.Value;
  311. }
  312. set {
  313. if (_init)
  314. throw new InvalidOperationException ();
  315. if (!EnablePartialRendering && value)
  316. throw new InvalidOperationException ("The SupportsPartialRendering property cannot be set when EnablePartialRendering is false.");
  317. _supportsPartialRendering = value;
  318. }
  319. }
  320. bool CheckSupportsPartialRendering () {
  321. if (!EnablePartialRendering)
  322. return false;
  323. // TODO: consider browser capabilities
  324. return true;
  325. }
  326. [EditorBrowsable (EditorBrowsableState.Never)]
  327. [Browsable (false)]
  328. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  329. public override bool Visible {
  330. get {
  331. return true;
  332. }
  333. set {
  334. throw new NotImplementedException ();
  335. }
  336. }
  337. [Category ("Action")]
  338. public event EventHandler<AsyncPostBackErrorEventArgs> AsyncPostBackError;
  339. [Category ("Action")]
  340. public event EventHandler<ScriptReferenceEventArgs> ResolveScriptReference;
  341. static ScriptManager () {
  342. _cultureInfoSerializer = new JavaScriptSerializer ();
  343. _cultureInfoSerializer.RegisterConverters (CultureInfoConverter.GetConverters ());
  344. }
  345. public static ScriptManager GetCurrent (Page page) {
  346. HttpContext ctx = HttpContext.Current;
  347. if (ctx == null)
  348. return null;
  349. return (ScriptManager) ctx.Items [page];
  350. }
  351. static void SetCurrent (Page page, ScriptManager instance) {
  352. HttpContext ctx = HttpContext.Current;
  353. ctx.Items [page] = instance;
  354. }
  355. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection) {
  356. _isInAsyncPostBack = true;
  357. string arg = postCollection [postDataKey];
  358. if (!String.IsNullOrEmpty (arg)) {
  359. string [] args = arg.Split ('|');
  360. _panelToRefreshID = args [0];
  361. _asyncPostBackSourceElementID = args [1];
  362. return true;
  363. }
  364. return false;
  365. }
  366. protected internal virtual void OnAsyncPostBackError (AsyncPostBackErrorEventArgs e) {
  367. if (AsyncPostBackError != null)
  368. AsyncPostBackError (this, e);
  369. }
  370. protected override void OnInit (EventArgs e) {
  371. base.OnInit (e);
  372. if (GetCurrent (Page) != null)
  373. throw new InvalidOperationException ("Only one instance of a ScriptManager can be added to the page.");
  374. SetCurrent (Page, this);
  375. _init = true;
  376. }
  377. protected override void OnPreRender (EventArgs e) {
  378. base.OnPreRender (e);
  379. Page.PreRenderComplete += new EventHandler (OnPreRenderComplete);
  380. if (IsInAsyncPostBack) {
  381. Page.SetRenderMethodDelegate (RenderPageCallback);
  382. }
  383. else {
  384. if (EnableScriptGlobalization) {
  385. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  386. string script = String.Format ("var __cultureInfo = '{0}';", _cultureInfoSerializer.Serialize (culture));
  387. RegisterClientScriptBlock (this, typeof (ScriptManager), "ScriptGlobalization", script, true);
  388. }
  389. if (EnablePageMethods) {
  390. LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (Page.GetType (), Page.Request.FilePath);
  391. RegisterStartupScript (this, typeof (ScriptManager), "PageMethods", logicalTypeInfo.Proxy, true);
  392. }
  393. // Register startup script
  394. StringBuilder sb = new StringBuilder ();
  395. sb.AppendLine ();
  396. sb.AppendLine ("Sys.Application.initialize();");
  397. RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", sb.ToString (), true);
  398. }
  399. }
  400. void OnPreRenderComplete (object sender, EventArgs e) {
  401. // Register Scripts
  402. List<ScriptReferenceEntry> scripts = new List<ScriptReferenceEntry> ();
  403. foreach (ScriptReferenceEntry script in GetScriptReferences ()) {
  404. OnResolveScriptReference (new ScriptReferenceEventArgs (script.ScriptReference));
  405. scripts.Add (script);
  406. }
  407. for (int i = 0; i < scripts.Count; i++)
  408. if (!IsInAsyncPostBack || HasBeenRendered (scripts [i].Control))
  409. RegisterScriptReference (scripts [i].ScriptReference);
  410. }
  411. static bool HasBeenRendered (Control control) {
  412. if(control==null)
  413. return false;
  414. UpdatePanel parent = control.Parent as UpdatePanel;
  415. if (parent != null && parent.RequiresUpdate)
  416. return true;
  417. return HasBeenRendered (control.Parent);
  418. }
  419. IEnumerable<ScriptReferenceEntry> GetScriptReferences () {
  420. ScriptReference script;
  421. script = new ScriptReference ("MicrosoftAjax.js", String.Empty);
  422. script.NotifyScriptLoaded = false;
  423. yield return new ScriptReferenceEntry (this, script);
  424. script = new ScriptReference ("MicrosoftAjaxWebForms.js", String.Empty);
  425. script.NotifyScriptLoaded = false;
  426. yield return new ScriptReferenceEntry (this, script);
  427. if (_scripts != null && _scripts.Count > 0) {
  428. for (int i = 0; i < _scripts.Count; i++) {
  429. yield return new ScriptReferenceEntry (this, _scripts [i]);
  430. }
  431. }
  432. if (_registeredScriptControls != null && _registeredScriptControls.Count > 0) {
  433. for (int i = 0; i < _registeredScriptControls.Count; i++) {
  434. IEnumerable<ScriptReference> scripts = _registeredScriptControls [i].GetScriptReferences ();
  435. if (scripts != null)
  436. foreach (ScriptReference s in scripts)
  437. yield return new ScriptReferenceEntry ((Control) _registeredScriptControls [i], s);
  438. }
  439. }
  440. if (_registeredExtenderControls != null && _registeredExtenderControls.Count > 0) {
  441. foreach (IExtenderControl ex in _registeredExtenderControls.Keys) {
  442. IEnumerable<ScriptReference> scripts = ex.GetScriptReferences ();
  443. if (scripts != null)
  444. foreach (ScriptReference s in scripts)
  445. yield return new ScriptReferenceEntry ((Control) ex, s);
  446. }
  447. }
  448. }
  449. protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e) {
  450. if (ResolveScriptReference != null)
  451. ResolveScriptReference (this, e);
  452. }
  453. protected virtual void RaisePostDataChangedEvent () {
  454. UpdatePanel up = Page.FindControl (_panelToRefreshID) as UpdatePanel;
  455. if (up != null && up.ChildrenAsTriggers)
  456. up.Update ();
  457. }
  458. public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue) {
  459. RegisterArrayDeclaration (control.Page, arrayName, arrayValue);
  460. }
  461. public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue) {
  462. ScriptManager sm = GetCurrent (page);
  463. if (sm.IsInAsyncPostBack)
  464. sm.RegisterArrayDeclaration (arrayName, arrayValue);
  465. else
  466. page.ClientScript.RegisterArrayDeclaration (arrayName, arrayValue);
  467. }
  468. void RegisterArrayDeclaration (string arrayName, string arrayValue) {
  469. if (_arrayDeclarations == null)
  470. _arrayDeclarations = new List<ArrayDeclaration> ();
  471. _arrayDeclarations.Add (new ArrayDeclaration (arrayName, arrayValue));
  472. }
  473. public void RegisterAsyncPostBackControl (Control control) {
  474. if (control == null)
  475. return;
  476. if (_asyncPostBackControls == null)
  477. _asyncPostBackControls = new List<Control> ();
  478. if (_asyncPostBackControls.Contains (control))
  479. return;
  480. _asyncPostBackControls.Add (control);
  481. }
  482. public static void RegisterClientScriptBlock (Control control, Type type, string key, string script, bool addScriptTags) {
  483. RegisterClientScriptBlock (control.Page, type, key, script, addScriptTags);
  484. }
  485. public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags) {
  486. ScriptManager sm = GetCurrent (page);
  487. if (sm.IsInAsyncPostBack)
  488. RegisterScript (ref sm._clientScriptBlocks, type, key, script, addScriptTags ? ScriptEntryType.ScriptContentNoTags : ScriptEntryType.ScriptContentWithTags);
  489. else
  490. page.ClientScript.RegisterClientScriptBlock (type, key, script, addScriptTags);
  491. }
  492. public static void RegisterClientScriptInclude (Control control, Type type, string key, string url) {
  493. RegisterClientScriptInclude (control.Page, type, key, url);
  494. }
  495. public static void RegisterClientScriptInclude (Page page, Type type, string key, string url) {
  496. ScriptManager sm = GetCurrent (page);
  497. if (sm.IsInAsyncPostBack)
  498. RegisterScript (ref sm._scriptIncludes, type, key, url, ScriptEntryType.ScriptPath);
  499. else
  500. page.ClientScript.RegisterClientScriptInclude (type, key, url);
  501. }
  502. public static void RegisterClientScriptResource (Control control, Type type, string resourceName) {
  503. RegisterClientScriptResource (control.Page, type, resourceName);
  504. }
  505. public static void RegisterClientScriptResource (Page page, Type type, string resourceName) {
  506. RegisterClientScriptInclude (page, type, "resource-" + resourceName, ScriptResourceHandler.GetResourceUrl (type, resourceName));
  507. }
  508. void RegisterScriptReference (ScriptReference script) {
  509. bool isDebugMode = IsDeploymentRetail ? false : (script.ScriptModeInternal == ScriptMode.Inherit ? IsDebuggingEnabled : (script.ScriptModeInternal == ScriptMode.Debug));
  510. string url;
  511. if (!String.IsNullOrEmpty (script.Path)) {
  512. url = GetScriptName (ResolveClientUrl (script.Path), isDebugMode, EnableScriptLocalization ? script.ResourceUICultures : null);
  513. }
  514. else if (!String.IsNullOrEmpty (script.Name)) {
  515. Assembly assembly;
  516. if (String.IsNullOrEmpty (script.Assembly))
  517. assembly = typeof (ScriptManager).Assembly;
  518. else
  519. assembly = Assembly.Load (script.Assembly);
  520. string name = GetScriptName (script.Name, isDebugMode, null);
  521. if (script.IgnoreScriptPath || String.IsNullOrEmpty (ScriptPath))
  522. url = ScriptResourceHandler.GetResourceUrl (assembly, name, script.NotifyScriptLoaded);
  523. else {
  524. AssemblyName an = assembly.GetName ();
  525. url = ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (ScriptPath), an.Name, '/', an.Version, '/', name));
  526. }
  527. }
  528. else {
  529. throw new InvalidOperationException ("Name and Path cannot both be empty.");
  530. }
  531. RegisterClientScriptInclude (this, typeof (ScriptManager), url, url);
  532. }
  533. static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures) {
  534. if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
  535. return releaseName;
  536. if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase))
  537. throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path. The path must end in '.js'.", releaseName));
  538. StringBuilder sb = new StringBuilder (releaseName);
  539. sb.Length -= 3;
  540. if (isDebugMode)
  541. sb.Append (".debug");
  542. string culture = Thread.CurrentThread.CurrentUICulture.Name;
  543. if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0)
  544. sb.AppendFormat (".{0}", culture);
  545. sb.Append (".js");
  546. return sb.ToString ();
  547. }
  548. public void RegisterDataItem (Control control, string dataItem) {
  549. RegisterDataItem (control, dataItem, false);
  550. }
  551. public void RegisterDataItem (Control control, string dataItem, bool isJsonSerialized) {
  552. if (!IsInAsyncPostBack)
  553. throw new InvalidOperationException ("RegisterDataItem can only be called during an async postback.");
  554. if (control == null)
  555. throw new ArgumentNullException ("control");
  556. if (_dataItems == null)
  557. _dataItems = new Dictionary<Control, DataItemEntry> ();
  558. if (_dataItems.ContainsKey (control))
  559. throw new ArgumentException (String.Format ("'{0}' already has a data item registered.", control.ID), "control");
  560. _dataItems.Add (control, new DataItemEntry (dataItem, isJsonSerialized));
  561. }
  562. public void RegisterDispose (Control control, string disposeScript) {
  563. throw new NotImplementedException ();
  564. }
  565. public static void RegisterExpandoAttribute (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
  566. Page page = control.Page;
  567. ScriptManager sm = GetCurrent (page);
  568. if (sm.IsInAsyncPostBack)
  569. sm.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  570. else
  571. page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  572. }
  573. private void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode) {
  574. // seems MS do nothing.
  575. }
  576. public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
  577. RegisterHiddenField (control.Page, hiddenFieldName, hiddenFieldInitialValue);
  578. }
  579. public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue) {
  580. ScriptManager sm = GetCurrent (page);
  581. if (sm.IsInAsyncPostBack)
  582. sm.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  583. else
  584. page.ClientScript.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  585. }
  586. void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue) {
  587. if (_hiddenFields == null)
  588. _hiddenFields = new Hashtable ();
  589. if (!_hiddenFields.ContainsKey (hiddenFieldName))
  590. _hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  591. }
  592. public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script) {
  593. RegisterOnSubmitStatement (control.Page, type, key, script);
  594. }
  595. public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script) {
  596. ScriptManager sm = GetCurrent (page);
  597. if (sm.IsInAsyncPostBack)
  598. RegisterScript (ref sm._onSubmitStatements, type, key, script, ScriptEntryType.OnSubmit);
  599. else
  600. page.ClientScript.RegisterOnSubmitStatement (type, key, script);
  601. }
  602. public void RegisterPostBackControl (Control control) {
  603. if (control == null)
  604. return;
  605. if (_postBackControls == null)
  606. _postBackControls = new List<Control> ();
  607. if (_postBackControls.Contains (control))
  608. return;
  609. _postBackControls.Add (control);
  610. }
  611. internal void RegisterUpdatePanel (UpdatePanel updatePanel) {
  612. if (_updatePanels == null)
  613. _updatePanels = new List<UpdatePanel> ();
  614. if (_updatePanels.Contains (updatePanel))
  615. return;
  616. _updatePanels.Add (updatePanel);
  617. }
  618. public void RegisterScriptDescriptors (IExtenderControl extenderControl) {
  619. if (extenderControl == null)
  620. return;
  621. if (_registeredExtenderControls == null || !_registeredExtenderControls.ContainsKey (extenderControl))
  622. return;
  623. Control targetControl = _registeredExtenderControls [extenderControl];
  624. RegisterScriptDescriptors (extenderControl.GetScriptDescriptors (targetControl));
  625. }
  626. public void RegisterScriptDescriptors (IScriptControl scriptControl) {
  627. if (scriptControl == null)
  628. return;
  629. if (_registeredScriptControls == null || !_registeredScriptControls.Contains (scriptControl))
  630. return;
  631. RegisterScriptDescriptors (scriptControl.GetScriptDescriptors ());
  632. }
  633. void RegisterScriptDescriptors (IEnumerable<ScriptDescriptor> scriptDescriptors) {
  634. if (scriptDescriptors == null)
  635. return;
  636. if (IsInAsyncPostBack && !IsInPartialRendering)
  637. return;
  638. StringBuilder sb = new StringBuilder ();
  639. foreach (ScriptDescriptor scriptDescriptor in scriptDescriptors) {
  640. sb.AppendLine ("Sys.Application.add_init(function() {");
  641. sb.AppendLine (scriptDescriptor.GetScript ());
  642. sb.AppendLine ("});");
  643. }
  644. string script = sb.ToString ();
  645. RegisterStartupScript (this, typeof (ExtenderControl), script, script, true);
  646. }
  647. public static void RegisterStartupScript (Control control, Type type, string key, string script, bool addScriptTags) {
  648. RegisterStartupScript (control.Page, type, key, script, addScriptTags);
  649. }
  650. public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags) {
  651. ScriptManager sm = GetCurrent (page);
  652. if (sm.IsInAsyncPostBack)
  653. RegisterScript (ref sm._startupScriptBlocks, type, key, script, addScriptTags ? ScriptEntryType.ScriptContentNoTags : ScriptEntryType.ScriptContentWithTags);
  654. else
  655. page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
  656. }
  657. public void RegisterScriptControl<TScriptControl> (TScriptControl scriptControl) where TScriptControl : Control, IScriptControl {
  658. if (scriptControl == null)
  659. throw new ArgumentNullException ("scriptControl");
  660. if (_registeredScriptControls == null)
  661. _registeredScriptControls = new List<IScriptControl> ();
  662. if (!_registeredScriptControls.Contains (scriptControl))
  663. _registeredScriptControls.Add (scriptControl);
  664. }
  665. public void RegisterExtenderControl<TExtenderControl> (TExtenderControl extenderControl, Control targetControl) where TExtenderControl : Control, IExtenderControl {
  666. if (extenderControl == null)
  667. throw new ArgumentNullException ("extenderControl");
  668. if (targetControl == null)
  669. throw new ArgumentNullException ("targetControl");
  670. if (_registeredExtenderControls == null)
  671. _registeredExtenderControls = new Dictionary<IExtenderControl, Control> ();
  672. if (!_registeredExtenderControls.ContainsKey (extenderControl))
  673. _registeredExtenderControls.Add (extenderControl, targetControl);
  674. }
  675. static void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, ScriptEntryType scriptEntryType) {
  676. ScriptEntry last = null;
  677. ScriptEntry entry = scriptList;
  678. while (entry != null) {
  679. if (entry.Type == type && entry.Key == key)
  680. return;
  681. last = entry;
  682. entry = entry.Next;
  683. }
  684. entry = new ScriptEntry (type, key, script, scriptEntryType);
  685. if (last != null)
  686. last.Next = entry;
  687. else
  688. scriptList = entry;
  689. }
  690. protected override void Render (HtmlTextWriter writer) {
  691. // MSDN: This method is used by control developers to extend the ScriptManager control.
  692. // Notes to Inheritors:
  693. // When overriding this method, call the base Render(HtmlTextWriter) method
  694. // so that PageRequestManager is rendered on the page.
  695. if (SupportsPartialRendering) {
  696. writer.WriteLine ("<script type=\"text/javascript\">");
  697. writer.WriteLine ("//<![CDATA[");
  698. writer.WriteLine ("Sys.WebForms.PageRequestManager._initialize('{0}', document.getElementById('{1}'));", UniqueID, Page.Form.ClientID);
  699. writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance()._updateControls([{0}], [{1}], [{2}], {3});", FormatUpdatePanelIDs (_updatePanels, true), FormatListIDs (_asyncPostBackControls, true), FormatListIDs (_postBackControls, true), AsyncPostBackTimeout);
  700. writer.WriteLine ("//]]");
  701. writer.WriteLine ("</script>");
  702. }
  703. base.Render (writer);
  704. }
  705. static string FormatUpdatePanelIDs (List<UpdatePanel> list, bool useSingleQuote) {
  706. if (list == null || list.Count == 0)
  707. return null;
  708. StringBuilder sb = new StringBuilder ();
  709. for (int i = 0; i < list.Count; i++) {
  710. sb.AppendFormat ("{0}{1}{2}{0},", useSingleQuote ? "'" : String.Empty, list [i].ChildrenAsTriggers ? "t" : "f", list [i].UniqueID);
  711. }
  712. if (sb.Length > 0)
  713. sb.Length--;
  714. return sb.ToString ();
  715. }
  716. static string FormatListIDs<T> (List<T> list, bool useSingleQuote) where T : Control {
  717. if (list == null || list.Count == 0)
  718. return null;
  719. StringBuilder sb = new StringBuilder ();
  720. for (int i = 0; i < list.Count; i++) {
  721. sb.AppendFormat ("{0}{1}{0},", useSingleQuote ? "'" : String.Empty, list [i].UniqueID);
  722. }
  723. if (sb.Length > 0)
  724. sb.Length--;
  725. return sb.ToString ();
  726. }
  727. public void SetFocus (Control control) {
  728. throw new NotImplementedException ();
  729. }
  730. public void SetFocus (string clientID) {
  731. throw new NotImplementedException ();
  732. }
  733. #region IPostBackDataHandler Members
  734. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection) {
  735. return LoadPostData (postDataKey, postCollection);
  736. }
  737. void IPostBackDataHandler.RaisePostDataChangedEvent () {
  738. RaisePostDataChangedEvent ();
  739. }
  740. #endregion
  741. static internal void WriteCallbackException (TextWriter output, Exception ex) {
  742. HttpException httpEx = ex as HttpException;
  743. WriteCallbackOutput (output, error, httpEx == null ? "500" : httpEx.GetHttpCode ().ToString (), ex.GetBaseException ().Message);
  744. }
  745. static internal void WriteCallbackRedirect (TextWriter output, string redirectUrl) {
  746. WriteCallbackOutput (output, pageRedirect, null, redirectUrl);
  747. }
  748. internal void WriteCallbackPanel (TextWriter output, UpdatePanel panel, StringBuilder panelOutput) {
  749. if (_panelsToRefresh == null)
  750. _panelsToRefresh = new List<UpdatePanel> ();
  751. _panelsToRefresh.Add (panel);
  752. WriteCallbackOutput (output, updatePanel, panel.ClientID, panelOutput);
  753. }
  754. internal void RegisterChildUpdatePanel (UpdatePanel updatePanel) {
  755. if (_childUpdatePanels == null)
  756. _childUpdatePanels = new List<UpdatePanel> ();
  757. _childUpdatePanels.Add (updatePanel);
  758. }
  759. static void WriteCallbackOutput (TextWriter output, string type, string name, object value) {
  760. string str = value as string;
  761. StringBuilder sb = value as StringBuilder;
  762. int length = 0;
  763. if (str != null)
  764. length = str.Length;
  765. else if (sb != null)
  766. length = sb.Length;
  767. //output.Write ("{0}|{1}|{2}|{3}|", value == null ? 0 : value.Length, type, name, value);
  768. output.Write (length);
  769. output.Write ('|');
  770. output.Write (type);
  771. output.Write ('|');
  772. output.Write (name);
  773. output.Write ('|');
  774. for (int i = 0; i < length; i++)
  775. if (str != null)
  776. output.Write (str [i]);
  777. else
  778. output.Write (sb [i]);
  779. output.Write ('|');
  780. }
  781. void RenderPageCallback (HtmlTextWriter output, Control container) {
  782. Page page = (Page) container;
  783. page.Form.SetRenderMethodDelegate (RenderFormCallback);
  784. HtmlTextParser parser = new HtmlTextParser (output);
  785. page.Form.RenderControl (parser);
  786. WriteCallbackOutput (output, asyncPostBackControlIDs, null, FormatListIDs (_asyncPostBackControls, false));
  787. WriteCallbackOutput (output, postBackControlIDs, null, FormatListIDs (_postBackControls, false));
  788. WriteCallbackOutput (output, updatePanelIDs, null, FormatUpdatePanelIDs (_updatePanels, false));
  789. WriteCallbackOutput (output, childUpdatePanelIDs, null, FormatListIDs (_childUpdatePanels, false));
  790. WriteCallbackOutput (output, panelsToRefreshIDs, null, FormatListIDs (_panelsToRefresh, false));
  791. WriteCallbackOutput (output, asyncPostBackTimeout, null, AsyncPostBackTimeout.ToString ());
  792. WriteCallbackOutput (output, pageTitle, null, Page.Title);
  793. if (_dataItems != null)
  794. foreach (Control control in _dataItems.Keys) {
  795. DataItemEntry entry = _dataItems [control];
  796. WriteCallbackOutput (output, entry.IsJsonSerialized ? dataItemJson : dataItem, control.ClientID, entry.DataItem);
  797. }
  798. WriteArrayDeclarations (output);
  799. WriteScriptBlocks (output, _clientScriptBlocks);
  800. WriteScriptBlocks (output, _scriptIncludes);
  801. WriteScriptBlocks (output, _startupScriptBlocks);
  802. WriteScriptBlocks (output, _onSubmitStatements);
  803. WriteHiddenFields (output);
  804. }
  805. void WriteArrayDeclarations (HtmlTextWriter writer) {
  806. if (_arrayDeclarations != null) {
  807. for (int i = 0; i < _arrayDeclarations.Count; i++) {
  808. ArrayDeclaration array = _arrayDeclarations [i];
  809. WriteCallbackOutput (writer, arrayDeclaration, array.ArrayName, array.ArrayValue);
  810. }
  811. }
  812. }
  813. void WriteScriptBlocks (HtmlTextWriter output, ScriptEntry scriptList) {
  814. while (scriptList != null) {
  815. switch (scriptList.ScriptEntryType) {
  816. case ScriptEntryType.ScriptContentNoTags:
  817. WriteCallbackOutput (output, scriptBlock, scriptContentNoTags, scriptList.Script);
  818. break;
  819. case ScriptEntryType.ScriptContentWithTags:
  820. string script = SerializeScriptBlock (scriptList);
  821. WriteCallbackOutput (output, scriptBlock, scriptContentWithTags, script);
  822. break;
  823. case ScriptEntryType.ScriptPath:
  824. WriteCallbackOutput (output, scriptBlock, scriptPath, scriptList.Script);
  825. break;
  826. case ScriptEntryType.OnSubmit:
  827. WriteCallbackOutput (output, onSubmit, null, scriptList.Script);
  828. break;
  829. }
  830. scriptList = scriptList.Next;
  831. }
  832. }
  833. void WriteHiddenFields (HtmlTextWriter output) {
  834. if (_hiddenFields == null)
  835. return;
  836. foreach (string key in _hiddenFields.Keys) {
  837. string value = _hiddenFields [key] as string;
  838. WriteCallbackOutput (output, hiddenField, key, value);
  839. }
  840. }
  841. [MonoTODO ()]
  842. static string SerializeScriptBlock (ScriptEntry scriptList) {
  843. 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.", scriptList.Type, scriptList.Key, scriptList.Script));
  844. }
  845. void RenderFormCallback (HtmlTextWriter output, Control container) {
  846. output = ((HtmlTextParser) output).ResponseOutput;
  847. HtmlForm form = (HtmlForm) container;
  848. HtmlTextWriter writer = new HtmlDropWriter (output);
  849. if (form.HasControls ()) {
  850. for (int i = 0; i < form.Controls.Count; i++) {
  851. form.Controls [i].RenderControl (writer);
  852. }
  853. }
  854. }
  855. internal class AlternativeHtmlTextWriter : HtmlTextWriter
  856. {
  857. readonly HtmlTextWriter _responseOutput;
  858. public HtmlTextWriter ResponseOutput {
  859. get { return _responseOutput; }
  860. }
  861. public AlternativeHtmlTextWriter (TextWriter writer, HtmlTextWriter responseOutput)
  862. : base (writer) {
  863. _responseOutput = responseOutput;
  864. }
  865. }
  866. sealed class HtmlTextParser : AlternativeHtmlTextWriter
  867. {
  868. public HtmlTextParser (HtmlTextWriter responseOutput)
  869. : base (new TextParser (responseOutput), responseOutput) {
  870. }
  871. }
  872. sealed class TextParser : TextWriter
  873. {
  874. int _state;
  875. char _charState = (char) 255;
  876. const char nullCharState = (char) 255;
  877. StringBuilder _sb = new StringBuilder ();
  878. Dictionary<string, string> _currentField;
  879. string _currentAttribute;
  880. readonly HtmlTextWriter _responseOutput;
  881. public override Encoding Encoding {
  882. get { return Encoding.UTF8; }
  883. }
  884. public TextParser (HtmlTextWriter responseOutput) {
  885. _responseOutput = responseOutput;
  886. }
  887. public override void Write (char value) {
  888. switch (_state) {
  889. case 0:
  890. ParseBeginTag (value);
  891. break;
  892. case 1:
  893. ParseAttributeName (value);
  894. break;
  895. case 2:
  896. ParseAttributeValue (value);
  897. break;
  898. }
  899. }
  900. private void ParseAttributeValue (char value) {
  901. switch (value) {
  902. case '>':
  903. ResetState ();
  904. break;
  905. case '"':
  906. _currentField.Add (_currentAttribute, _sb.ToString ());
  907. _state = 1;
  908. _sb.Length = 0;
  909. ProbeWriteOutput ();
  910. break;
  911. default:
  912. _sb.Append (value);
  913. break;
  914. }
  915. }
  916. private void ParseAttributeName (char value) {
  917. switch (value) {
  918. case '>':
  919. ResetState ();
  920. break;
  921. case ' ':
  922. case '=':
  923. break;
  924. case '"':
  925. _currentAttribute = _sb.ToString ();
  926. _state = 2;
  927. _sb.Length = 0;
  928. break;
  929. default:
  930. _sb.Append (value);
  931. break;
  932. }
  933. }
  934. void ParseBeginTag (char value) {
  935. switch (_charState) {
  936. case nullCharState:
  937. if (value == '<')
  938. _charState = value;
  939. break;
  940. case '<':
  941. if (value == 'i')
  942. _charState = value;
  943. else
  944. ResetState ();
  945. break;
  946. case 'i':
  947. if (value == 'n')
  948. _charState = value;
  949. else
  950. ResetState ();
  951. break;
  952. case 'n':
  953. if (value == 'p')
  954. _charState = value;
  955. else
  956. ResetState ();
  957. break;
  958. case 'p':
  959. if (value == 'u')
  960. _charState = value;
  961. else
  962. ResetState ();
  963. break;
  964. case 'u':
  965. if (value == 't')
  966. _charState = value;
  967. else
  968. ResetState ();
  969. break;
  970. case 't':
  971. if (value == ' ') {
  972. _state = 1;
  973. _currentField = new Dictionary<string, string> ();
  974. }
  975. else
  976. ResetState ();
  977. break;
  978. }
  979. }
  980. private void ResetState () {
  981. _charState = nullCharState;
  982. _state = 0;
  983. _sb.Length = 0;
  984. }
  985. private void ProbeWriteOutput () {
  986. if (!_currentField.ContainsKey ("name"))
  987. return;
  988. if (!_currentField.ContainsKey ("value"))
  989. return;
  990. string value = _currentField ["value"];
  991. if (String.IsNullOrEmpty (value))
  992. return;
  993. ScriptManager.WriteCallbackOutput (_responseOutput, ScriptManager.hiddenField, _currentField ["name"], value);
  994. }
  995. }
  996. sealed class HtmlDropWriter : AlternativeHtmlTextWriter
  997. {
  998. public HtmlDropWriter (HtmlTextWriter responseOutput)
  999. : base (new DropWriter (), responseOutput) {
  1000. }
  1001. }
  1002. sealed class DropWriter : TextWriter
  1003. {
  1004. public override Encoding Encoding {
  1005. get { return Encoding.UTF8; }
  1006. }
  1007. }
  1008. sealed class ScriptEntry
  1009. {
  1010. readonly public Type Type;
  1011. readonly public string Key;
  1012. readonly public string Script;
  1013. readonly public ScriptEntryType ScriptEntryType;
  1014. public ScriptEntry Next;
  1015. public ScriptEntry (Type type, string key, string script, ScriptEntryType scriptEntryType) {
  1016. Key = key;
  1017. Type = type;
  1018. Script = script;
  1019. ScriptEntryType = scriptEntryType;
  1020. }
  1021. }
  1022. enum ScriptEntryType
  1023. {
  1024. ScriptContentNoTags,
  1025. ScriptContentWithTags,
  1026. ScriptPath,
  1027. OnSubmit
  1028. }
  1029. sealed class ArrayDeclaration
  1030. {
  1031. readonly public string ArrayName;
  1032. readonly public string ArrayValue;
  1033. public ArrayDeclaration (string arrayName, string arrayValue) {
  1034. ArrayName = arrayName;
  1035. ArrayValue = arrayValue;
  1036. }
  1037. }
  1038. sealed class CultureInfoConverter : JavaScriptConverter
  1039. {
  1040. private CultureInfoConverter () { }
  1041. static readonly Type typeofCultureInfo = typeof (CultureInfo);
  1042. static CultureInfoConverter _instance = new CultureInfoConverter ();
  1043. public static IEnumerable<JavaScriptConverter> GetConverters () { yield return _instance; }
  1044. public override IEnumerable<Type> SupportedTypes {
  1045. get { yield return typeofCultureInfo; }
  1046. }
  1047. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  1048. throw new NotSupportedException ();
  1049. }
  1050. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  1051. CultureInfo ci = (CultureInfo) obj;
  1052. if (ci == null)
  1053. return null;
  1054. Dictionary<string, object> d = new Dictionary<string, object> (StringComparer.Ordinal);
  1055. d.Add ("name", ci.Name);
  1056. d.Add ("numberFormat", ci.NumberFormat);
  1057. d.Add ("dateTimeFormat", ci.DateTimeFormat);
  1058. return d;
  1059. }
  1060. }
  1061. sealed class ScriptReferenceEntry
  1062. {
  1063. readonly Control _control;
  1064. readonly ScriptReference _scriptReference;
  1065. public Control Control { get { return _control; } }
  1066. public ScriptReference ScriptReference { get { return _scriptReference; } }
  1067. public ScriptReferenceEntry (Control control, ScriptReference scriptReference) {
  1068. _control = control;
  1069. _scriptReference = scriptReference;
  1070. }
  1071. }
  1072. sealed class DataItemEntry
  1073. {
  1074. readonly string _dataItem;
  1075. readonly bool _isJsonSerialized;
  1076. public string DataItem { get { return _dataItem; } }
  1077. public bool IsJsonSerialized { get { return _isJsonSerialized; } }
  1078. public DataItemEntry (string dataItem, bool isJsonSerialized) {
  1079. _dataItem = dataItem;
  1080. _isJsonSerialized = isJsonSerialized;
  1081. }
  1082. }
  1083. }
  1084. }