ScriptManager.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. Page.ClientScript.GetPostBackEventReference (this, null);
  399. }
  400. }
  401. void OnPreRenderComplete (object sender, EventArgs e) {
  402. // Register Scripts
  403. List<ScriptReferenceEntry> scripts = new List<ScriptReferenceEntry> ();
  404. foreach (ScriptReferenceEntry script in GetScriptReferences ()) {
  405. OnResolveScriptReference (new ScriptReferenceEventArgs (script.ScriptReference));
  406. scripts.Add (script);
  407. }
  408. for (int i = 0; i < scripts.Count; i++)
  409. if (!IsInAsyncPostBack || HasBeenRendered (scripts [i].Control))
  410. RegisterScriptReference (scripts [i].ScriptReference);
  411. }
  412. static bool HasBeenRendered (Control control) {
  413. if(control==null)
  414. return false;
  415. UpdatePanel parent = control.Parent as UpdatePanel;
  416. if (parent != null && parent.RequiresUpdate)
  417. return true;
  418. return HasBeenRendered (control.Parent);
  419. }
  420. IEnumerable<ScriptReferenceEntry> GetScriptReferences () {
  421. ScriptReference script;
  422. script = new ScriptReference ("MicrosoftAjax.js", String.Empty);
  423. script.NotifyScriptLoaded = false;
  424. yield return new ScriptReferenceEntry (this, script);
  425. script = new ScriptReference ("MicrosoftAjaxWebForms.js", String.Empty);
  426. script.NotifyScriptLoaded = false;
  427. yield return new ScriptReferenceEntry (this, script);
  428. if (_scripts != null && _scripts.Count > 0) {
  429. for (int i = 0; i < _scripts.Count; i++) {
  430. yield return new ScriptReferenceEntry (this, _scripts [i]);
  431. }
  432. }
  433. if (_registeredScriptControls != null && _registeredScriptControls.Count > 0) {
  434. for (int i = 0; i < _registeredScriptControls.Count; i++) {
  435. IEnumerable<ScriptReference> scripts = _registeredScriptControls [i].GetScriptReferences ();
  436. if (scripts != null)
  437. foreach (ScriptReference s in scripts)
  438. yield return new ScriptReferenceEntry ((Control) _registeredScriptControls [i], s);
  439. }
  440. }
  441. if (_registeredExtenderControls != null && _registeredExtenderControls.Count > 0) {
  442. foreach (IExtenderControl ex in _registeredExtenderControls.Keys) {
  443. IEnumerable<ScriptReference> scripts = ex.GetScriptReferences ();
  444. if (scripts != null)
  445. foreach (ScriptReference s in scripts)
  446. yield return new ScriptReferenceEntry ((Control) ex, s);
  447. }
  448. }
  449. }
  450. protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e) {
  451. if (ResolveScriptReference != null)
  452. ResolveScriptReference (this, e);
  453. }
  454. protected virtual void RaisePostDataChangedEvent () {
  455. UpdatePanel up = Page.FindControl (_panelToRefreshID) as UpdatePanel;
  456. if (up != null && up.ChildrenAsTriggers)
  457. up.Update ();
  458. }
  459. public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue) {
  460. RegisterArrayDeclaration (control.Page, arrayName, arrayValue);
  461. }
  462. public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue) {
  463. ScriptManager sm = GetCurrent (page);
  464. if (sm.IsInAsyncPostBack)
  465. sm.RegisterArrayDeclaration (arrayName, arrayValue);
  466. else
  467. page.ClientScript.RegisterArrayDeclaration (arrayName, arrayValue);
  468. }
  469. void RegisterArrayDeclaration (string arrayName, string arrayValue) {
  470. if (_arrayDeclarations == null)
  471. _arrayDeclarations = new List<ArrayDeclaration> ();
  472. _arrayDeclarations.Add (new ArrayDeclaration (arrayName, arrayValue));
  473. }
  474. public void RegisterAsyncPostBackControl (Control control) {
  475. if (control == null)
  476. return;
  477. if (_asyncPostBackControls == null)
  478. _asyncPostBackControls = new List<Control> ();
  479. if (_asyncPostBackControls.Contains (control))
  480. return;
  481. _asyncPostBackControls.Add (control);
  482. }
  483. public static void RegisterClientScriptBlock (Control control, Type type, string key, string script, bool addScriptTags) {
  484. RegisterClientScriptBlock (control.Page, type, key, script, addScriptTags);
  485. }
  486. public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags) {
  487. ScriptManager sm = GetCurrent (page);
  488. if (sm.IsInAsyncPostBack)
  489. RegisterScript (ref sm._clientScriptBlocks, type, key, script, addScriptTags ? ScriptEntryType.ScriptContentNoTags : ScriptEntryType.ScriptContentWithTags);
  490. else
  491. page.ClientScript.RegisterClientScriptBlock (type, key, script, addScriptTags);
  492. }
  493. public static void RegisterClientScriptInclude (Control control, Type type, string key, string url) {
  494. RegisterClientScriptInclude (control.Page, type, key, url);
  495. }
  496. public static void RegisterClientScriptInclude (Page page, Type type, string key, string url) {
  497. ScriptManager sm = GetCurrent (page);
  498. if (sm.IsInAsyncPostBack)
  499. RegisterScript (ref sm._scriptIncludes, type, key, url, ScriptEntryType.ScriptPath);
  500. else
  501. page.ClientScript.RegisterClientScriptInclude (type, key, url);
  502. }
  503. public static void RegisterClientScriptResource (Control control, Type type, string resourceName) {
  504. RegisterClientScriptResource (control.Page, type, resourceName);
  505. }
  506. public static void RegisterClientScriptResource (Page page, Type type, string resourceName) {
  507. RegisterClientScriptInclude (page, type, "resource-" + resourceName, ScriptResourceHandler.GetResourceUrl (type, resourceName));
  508. }
  509. void RegisterScriptReference (ScriptReference script) {
  510. bool isDebugMode = IsDeploymentRetail ? false : (script.ScriptModeInternal == ScriptMode.Inherit ? IsDebuggingEnabled : (script.ScriptModeInternal == ScriptMode.Debug));
  511. string url;
  512. if (!String.IsNullOrEmpty (script.Path)) {
  513. url = GetScriptName (ResolveClientUrl (script.Path), isDebugMode, EnableScriptLocalization ? script.ResourceUICultures : null);
  514. }
  515. else if (!String.IsNullOrEmpty (script.Name)) {
  516. Assembly assembly;
  517. if (String.IsNullOrEmpty (script.Assembly))
  518. assembly = typeof (ScriptManager).Assembly;
  519. else
  520. assembly = Assembly.Load (script.Assembly);
  521. string name = GetScriptName (script.Name, isDebugMode, null);
  522. if (script.IgnoreScriptPath || String.IsNullOrEmpty (ScriptPath))
  523. url = ScriptResourceHandler.GetResourceUrl (assembly, name, script.NotifyScriptLoaded);
  524. else {
  525. AssemblyName an = assembly.GetName ();
  526. url = ResolveClientUrl (String.Concat (VirtualPathUtility.AppendTrailingSlash (ScriptPath), an.Name, '/', an.Version, '/', name));
  527. }
  528. }
  529. else {
  530. throw new InvalidOperationException ("Name and Path cannot both be empty.");
  531. }
  532. RegisterClientScriptInclude (this, typeof (ScriptManager), url, url);
  533. }
  534. static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures) {
  535. if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
  536. return releaseName;
  537. if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase))
  538. throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path. The path must end in '.js'.", releaseName));
  539. StringBuilder sb = new StringBuilder (releaseName);
  540. sb.Length -= 3;
  541. if (isDebugMode)
  542. sb.Append (".debug");
  543. string culture = Thread.CurrentThread.CurrentUICulture.Name;
  544. if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0)
  545. sb.AppendFormat (".{0}", culture);
  546. sb.Append (".js");
  547. return sb.ToString ();
  548. }
  549. public void RegisterDataItem (Control control, string dataItem) {
  550. RegisterDataItem (control, dataItem, false);
  551. }
  552. public void RegisterDataItem (Control control, string dataItem, bool isJsonSerialized) {
  553. if (!IsInAsyncPostBack)
  554. throw new InvalidOperationException ("RegisterDataItem can only be called during an async postback.");
  555. if (control == null)
  556. throw new ArgumentNullException ("control");
  557. if (_dataItems == null)
  558. _dataItems = new Dictionary<Control, DataItemEntry> ();
  559. if (_dataItems.ContainsKey (control))
  560. throw new ArgumentException (String.Format ("'{0}' already has a data item registered.", control.ID), "control");
  561. _dataItems.Add (control, new DataItemEntry (dataItem, isJsonSerialized));
  562. }
  563. public void RegisterDispose (Control control, string disposeScript) {
  564. throw new NotImplementedException ();
  565. }
  566. public static void RegisterExpandoAttribute (Control control, string controlId, string attributeName, string attributeValue, bool encode) {
  567. Page page = control.Page;
  568. ScriptManager sm = GetCurrent (page);
  569. if (sm.IsInAsyncPostBack)
  570. sm.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  571. else
  572. page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  573. }
  574. private void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode) {
  575. // seems MS do nothing.
  576. }
  577. public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue) {
  578. RegisterHiddenField (control.Page, hiddenFieldName, hiddenFieldInitialValue);
  579. }
  580. public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue) {
  581. ScriptManager sm = GetCurrent (page);
  582. if (sm.IsInAsyncPostBack)
  583. sm.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  584. else
  585. page.ClientScript.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  586. }
  587. void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue) {
  588. if (_hiddenFields == null)
  589. _hiddenFields = new Hashtable ();
  590. if (!_hiddenFields.ContainsKey (hiddenFieldName))
  591. _hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  592. }
  593. public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script) {
  594. RegisterOnSubmitStatement (control.Page, type, key, script);
  595. }
  596. public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script) {
  597. ScriptManager sm = GetCurrent (page);
  598. if (sm.IsInAsyncPostBack)
  599. RegisterScript (ref sm._onSubmitStatements, type, key, script, ScriptEntryType.OnSubmit);
  600. else
  601. page.ClientScript.RegisterOnSubmitStatement (type, key, script);
  602. }
  603. public void RegisterPostBackControl (Control control) {
  604. if (control == null)
  605. return;
  606. if (_postBackControls == null)
  607. _postBackControls = new List<Control> ();
  608. if (_postBackControls.Contains (control))
  609. return;
  610. _postBackControls.Add (control);
  611. }
  612. internal void RegisterUpdatePanel (UpdatePanel updatePanel) {
  613. if (_updatePanels == null)
  614. _updatePanels = new List<UpdatePanel> ();
  615. if (_updatePanels.Contains (updatePanel))
  616. return;
  617. _updatePanels.Add (updatePanel);
  618. }
  619. public void RegisterScriptDescriptors (IExtenderControl extenderControl) {
  620. if (extenderControl == null)
  621. return;
  622. if (_registeredExtenderControls == null || !_registeredExtenderControls.ContainsKey (extenderControl))
  623. return;
  624. Control targetControl = _registeredExtenderControls [extenderControl];
  625. RegisterScriptDescriptors (extenderControl.GetScriptDescriptors (targetControl));
  626. }
  627. public void RegisterScriptDescriptors (IScriptControl scriptControl) {
  628. if (scriptControl == null)
  629. return;
  630. if (_registeredScriptControls == null || !_registeredScriptControls.Contains (scriptControl))
  631. return;
  632. RegisterScriptDescriptors (scriptControl.GetScriptDescriptors ());
  633. }
  634. void RegisterScriptDescriptors (IEnumerable<ScriptDescriptor> scriptDescriptors) {
  635. if (scriptDescriptors == null)
  636. return;
  637. if (IsInAsyncPostBack && !IsInPartialRendering)
  638. return;
  639. StringBuilder sb = new StringBuilder ();
  640. foreach (ScriptDescriptor scriptDescriptor in scriptDescriptors) {
  641. sb.AppendLine ("Sys.Application.add_init(function() {");
  642. sb.AppendLine (scriptDescriptor.GetScript ());
  643. sb.AppendLine ("});");
  644. }
  645. string script = sb.ToString ();
  646. RegisterStartupScript (this, typeof (ExtenderControl), script, script, true);
  647. }
  648. public static void RegisterStartupScript (Control control, Type type, string key, string script, bool addScriptTags) {
  649. RegisterStartupScript (control.Page, type, key, script, addScriptTags);
  650. }
  651. public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags) {
  652. ScriptManager sm = GetCurrent (page);
  653. if (sm.IsInAsyncPostBack)
  654. RegisterScript (ref sm._startupScriptBlocks, type, key, script, addScriptTags ? ScriptEntryType.ScriptContentNoTags : ScriptEntryType.ScriptContentWithTags);
  655. else
  656. page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
  657. }
  658. public void RegisterScriptControl<TScriptControl> (TScriptControl scriptControl) where TScriptControl : Control, IScriptControl {
  659. if (scriptControl == null)
  660. throw new ArgumentNullException ("scriptControl");
  661. if (_registeredScriptControls == null)
  662. _registeredScriptControls = new List<IScriptControl> ();
  663. if (!_registeredScriptControls.Contains (scriptControl))
  664. _registeredScriptControls.Add (scriptControl);
  665. }
  666. public void RegisterExtenderControl<TExtenderControl> (TExtenderControl extenderControl, Control targetControl) where TExtenderControl : Control, IExtenderControl {
  667. if (extenderControl == null)
  668. throw new ArgumentNullException ("extenderControl");
  669. if (targetControl == null)
  670. throw new ArgumentNullException ("targetControl");
  671. if (_registeredExtenderControls == null)
  672. _registeredExtenderControls = new Dictionary<IExtenderControl, Control> ();
  673. if (!_registeredExtenderControls.ContainsKey (extenderControl))
  674. _registeredExtenderControls.Add (extenderControl, targetControl);
  675. }
  676. static void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, ScriptEntryType scriptEntryType) {
  677. ScriptEntry last = null;
  678. ScriptEntry entry = scriptList;
  679. while (entry != null) {
  680. if (entry.Type == type && entry.Key == key)
  681. return;
  682. last = entry;
  683. entry = entry.Next;
  684. }
  685. entry = new ScriptEntry (type, key, script, scriptEntryType);
  686. if (last != null)
  687. last.Next = entry;
  688. else
  689. scriptList = entry;
  690. }
  691. protected override void Render (HtmlTextWriter writer) {
  692. // MSDN: This method is used by control developers to extend the ScriptManager control.
  693. // Notes to Inheritors:
  694. // When overriding this method, call the base Render(HtmlTextWriter) method
  695. // so that PageRequestManager is rendered on the page.
  696. if (SupportsPartialRendering) {
  697. writer.WriteLine ("<script type=\"text/javascript\">");
  698. writer.WriteLine ("//<![CDATA[");
  699. writer.WriteLine ("Sys.WebForms.PageRequestManager._initialize('{0}', document.getElementById('{1}'));", UniqueID, Page.Form.ClientID);
  700. writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance()._updateControls([{0}], [{1}], [{2}], {3});", FormatUpdatePanelIDs (_updatePanels, true), FormatListIDs (_asyncPostBackControls, true), FormatListIDs (_postBackControls, true), AsyncPostBackTimeout);
  701. writer.WriteLine ("//]]");
  702. writer.WriteLine ("</script>");
  703. }
  704. base.Render (writer);
  705. }
  706. static string FormatUpdatePanelIDs (List<UpdatePanel> list, bool useSingleQuote) {
  707. if (list == null || list.Count == 0)
  708. return null;
  709. StringBuilder sb = new StringBuilder ();
  710. for (int i = 0; i < list.Count; i++) {
  711. sb.AppendFormat ("{0}{1}{2}{0},", useSingleQuote ? "'" : String.Empty, list [i].ChildrenAsTriggers ? "t" : "f", list [i].UniqueID);
  712. }
  713. if (sb.Length > 0)
  714. sb.Length--;
  715. return sb.ToString ();
  716. }
  717. static string FormatListIDs<T> (List<T> list, bool useSingleQuote) where T : Control {
  718. if (list == null || list.Count == 0)
  719. return null;
  720. StringBuilder sb = new StringBuilder ();
  721. for (int i = 0; i < list.Count; i++) {
  722. sb.AppendFormat ("{0}{1}{0},", useSingleQuote ? "'" : String.Empty, list [i].UniqueID);
  723. }
  724. if (sb.Length > 0)
  725. sb.Length--;
  726. return sb.ToString ();
  727. }
  728. public void SetFocus (Control control) {
  729. throw new NotImplementedException ();
  730. }
  731. public void SetFocus (string clientID) {
  732. throw new NotImplementedException ();
  733. }
  734. #region IPostBackDataHandler Members
  735. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection) {
  736. return LoadPostData (postDataKey, postCollection);
  737. }
  738. void IPostBackDataHandler.RaisePostDataChangedEvent () {
  739. RaisePostDataChangedEvent ();
  740. }
  741. #endregion
  742. static internal void WriteCallbackException (TextWriter output, Exception ex) {
  743. HttpException httpEx = ex as HttpException;
  744. WriteCallbackOutput (output, error, httpEx == null ? "500" : httpEx.GetHttpCode ().ToString (), ex.GetBaseException ().Message);
  745. }
  746. static internal void WriteCallbackRedirect (TextWriter output, string redirectUrl) {
  747. WriteCallbackOutput (output, pageRedirect, null, redirectUrl);
  748. }
  749. internal void WriteCallbackPanel (TextWriter output, UpdatePanel panel, StringBuilder panelOutput) {
  750. if (_panelsToRefresh == null)
  751. _panelsToRefresh = new List<UpdatePanel> ();
  752. _panelsToRefresh.Add (panel);
  753. WriteCallbackOutput (output, updatePanel, panel.ClientID, panelOutput);
  754. }
  755. internal void RegisterChildUpdatePanel (UpdatePanel updatePanel) {
  756. if (_childUpdatePanels == null)
  757. _childUpdatePanels = new List<UpdatePanel> ();
  758. _childUpdatePanels.Add (updatePanel);
  759. }
  760. static void WriteCallbackOutput (TextWriter output, string type, string name, object value) {
  761. string str = value as string;
  762. StringBuilder sb = value as StringBuilder;
  763. int length = 0;
  764. if (str != null)
  765. length = str.Length;
  766. else if (sb != null)
  767. length = sb.Length;
  768. //output.Write ("{0}|{1}|{2}|{3}|", value == null ? 0 : value.Length, type, name, value);
  769. output.Write (length);
  770. output.Write ('|');
  771. output.Write (type);
  772. output.Write ('|');
  773. output.Write (name);
  774. output.Write ('|');
  775. for (int i = 0; i < length; i++)
  776. if (str != null)
  777. output.Write (str [i]);
  778. else
  779. output.Write (sb [i]);
  780. output.Write ('|');
  781. }
  782. void RenderPageCallback (HtmlTextWriter output, Control container) {
  783. Page page = (Page) container;
  784. page.Form.SetRenderMethodDelegate (RenderFormCallback);
  785. HtmlTextParser parser = new HtmlTextParser (output);
  786. page.Form.RenderControl (parser);
  787. WriteCallbackOutput (output, asyncPostBackControlIDs, null, FormatListIDs (_asyncPostBackControls, false));
  788. WriteCallbackOutput (output, postBackControlIDs, null, FormatListIDs (_postBackControls, false));
  789. WriteCallbackOutput (output, updatePanelIDs, null, FormatUpdatePanelIDs (_updatePanels, false));
  790. WriteCallbackOutput (output, childUpdatePanelIDs, null, FormatListIDs (_childUpdatePanels, false));
  791. WriteCallbackOutput (output, panelsToRefreshIDs, null, FormatListIDs (_panelsToRefresh, false));
  792. WriteCallbackOutput (output, asyncPostBackTimeout, null, AsyncPostBackTimeout.ToString ());
  793. WriteCallbackOutput (output, pageTitle, null, Page.Title);
  794. if (_dataItems != null)
  795. foreach (Control control in _dataItems.Keys) {
  796. DataItemEntry entry = _dataItems [control];
  797. WriteCallbackOutput (output, entry.IsJsonSerialized ? dataItemJson : dataItem, control.ClientID, entry.DataItem);
  798. }
  799. WriteArrayDeclarations (output);
  800. WriteScriptBlocks (output, _clientScriptBlocks);
  801. WriteScriptBlocks (output, _scriptIncludes);
  802. WriteScriptBlocks (output, _startupScriptBlocks);
  803. WriteScriptBlocks (output, _onSubmitStatements);
  804. WriteHiddenFields (output);
  805. }
  806. void WriteArrayDeclarations (HtmlTextWriter writer) {
  807. if (_arrayDeclarations != null) {
  808. for (int i = 0; i < _arrayDeclarations.Count; i++) {
  809. ArrayDeclaration array = _arrayDeclarations [i];
  810. WriteCallbackOutput (writer, arrayDeclaration, array.ArrayName, array.ArrayValue);
  811. }
  812. }
  813. }
  814. void WriteScriptBlocks (HtmlTextWriter output, ScriptEntry scriptList) {
  815. while (scriptList != null) {
  816. switch (scriptList.ScriptEntryType) {
  817. case ScriptEntryType.ScriptContentNoTags:
  818. WriteCallbackOutput (output, scriptBlock, scriptContentNoTags, scriptList.Script);
  819. break;
  820. case ScriptEntryType.ScriptContentWithTags:
  821. string script = SerializeScriptBlock (scriptList);
  822. WriteCallbackOutput (output, scriptBlock, scriptContentWithTags, script);
  823. break;
  824. case ScriptEntryType.ScriptPath:
  825. WriteCallbackOutput (output, scriptBlock, scriptPath, scriptList.Script);
  826. break;
  827. case ScriptEntryType.OnSubmit:
  828. WriteCallbackOutput (output, onSubmit, null, scriptList.Script);
  829. break;
  830. }
  831. scriptList = scriptList.Next;
  832. }
  833. }
  834. void WriteHiddenFields (HtmlTextWriter output) {
  835. if (_hiddenFields == null)
  836. return;
  837. foreach (string key in _hiddenFields.Keys) {
  838. string value = _hiddenFields [key] as string;
  839. WriteCallbackOutput (output, hiddenField, key, value);
  840. }
  841. }
  842. [MonoTODO ()]
  843. static string SerializeScriptBlock (ScriptEntry scriptList) {
  844. 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));
  845. }
  846. void RenderFormCallback (HtmlTextWriter output, Control container) {
  847. output = ((HtmlTextParser) output).ResponseOutput;
  848. HtmlForm form = (HtmlForm) container;
  849. HtmlTextWriter writer = new HtmlDropWriter (output);
  850. if (form.HasControls ()) {
  851. for (int i = 0; i < form.Controls.Count; i++) {
  852. form.Controls [i].RenderControl (writer);
  853. }
  854. }
  855. }
  856. internal class AlternativeHtmlTextWriter : HtmlTextWriter
  857. {
  858. readonly HtmlTextWriter _responseOutput;
  859. public HtmlTextWriter ResponseOutput {
  860. get { return _responseOutput; }
  861. }
  862. public AlternativeHtmlTextWriter (TextWriter writer, HtmlTextWriter responseOutput)
  863. : base (writer) {
  864. _responseOutput = responseOutput;
  865. }
  866. }
  867. sealed class HtmlTextParser : AlternativeHtmlTextWriter
  868. {
  869. public HtmlTextParser (HtmlTextWriter responseOutput)
  870. : base (new TextParser (responseOutput), responseOutput) {
  871. }
  872. }
  873. sealed class TextParser : TextWriter
  874. {
  875. int _state;
  876. char _charState = (char) 255;
  877. const char nullCharState = (char) 255;
  878. StringBuilder _sb = new StringBuilder ();
  879. Dictionary<string, string> _currentField;
  880. string _currentAttribute;
  881. readonly HtmlTextWriter _responseOutput;
  882. public override Encoding Encoding {
  883. get { return Encoding.UTF8; }
  884. }
  885. public TextParser (HtmlTextWriter responseOutput) {
  886. _responseOutput = responseOutput;
  887. }
  888. public override void Write (char value) {
  889. switch (_state) {
  890. case 0:
  891. ParseBeginTag (value);
  892. break;
  893. case 1:
  894. ParseAttributeName (value);
  895. break;
  896. case 2:
  897. ParseAttributeValue (value);
  898. break;
  899. }
  900. }
  901. private void ParseAttributeValue (char value) {
  902. switch (value) {
  903. case '>':
  904. ResetState ();
  905. break;
  906. case '"':
  907. _currentField.Add (_currentAttribute, _sb.ToString ());
  908. _state = 1;
  909. _sb.Length = 0;
  910. ProbeWriteOutput ();
  911. break;
  912. default:
  913. _sb.Append (value);
  914. break;
  915. }
  916. }
  917. private void ParseAttributeName (char value) {
  918. switch (value) {
  919. case '>':
  920. ResetState ();
  921. break;
  922. case ' ':
  923. case '=':
  924. break;
  925. case '"':
  926. _currentAttribute = _sb.ToString ();
  927. _state = 2;
  928. _sb.Length = 0;
  929. break;
  930. default:
  931. _sb.Append (value);
  932. break;
  933. }
  934. }
  935. void ParseBeginTag (char value) {
  936. switch (_charState) {
  937. case nullCharState:
  938. if (value == '<')
  939. _charState = value;
  940. break;
  941. case '<':
  942. if (value == 'i')
  943. _charState = value;
  944. else
  945. ResetState ();
  946. break;
  947. case 'i':
  948. if (value == 'n')
  949. _charState = value;
  950. else
  951. ResetState ();
  952. break;
  953. case 'n':
  954. if (value == 'p')
  955. _charState = value;
  956. else
  957. ResetState ();
  958. break;
  959. case 'p':
  960. if (value == 'u')
  961. _charState = value;
  962. else
  963. ResetState ();
  964. break;
  965. case 'u':
  966. if (value == 't')
  967. _charState = value;
  968. else
  969. ResetState ();
  970. break;
  971. case 't':
  972. if (value == ' ') {
  973. _state = 1;
  974. _currentField = new Dictionary<string, string> ();
  975. }
  976. else
  977. ResetState ();
  978. break;
  979. }
  980. }
  981. private void ResetState () {
  982. _charState = nullCharState;
  983. _state = 0;
  984. _sb.Length = 0;
  985. }
  986. private void ProbeWriteOutput () {
  987. if (!_currentField.ContainsKey ("name"))
  988. return;
  989. if (!_currentField.ContainsKey ("value"))
  990. return;
  991. string value = _currentField ["value"];
  992. if (String.IsNullOrEmpty (value))
  993. return;
  994. ScriptManager.WriteCallbackOutput (_responseOutput, ScriptManager.hiddenField, _currentField ["name"], value);
  995. }
  996. }
  997. sealed class HtmlDropWriter : AlternativeHtmlTextWriter
  998. {
  999. public HtmlDropWriter (HtmlTextWriter responseOutput)
  1000. : base (new DropWriter (), responseOutput) {
  1001. }
  1002. }
  1003. sealed class DropWriter : TextWriter
  1004. {
  1005. public override Encoding Encoding {
  1006. get { return Encoding.UTF8; }
  1007. }
  1008. }
  1009. sealed class ScriptEntry
  1010. {
  1011. readonly public Type Type;
  1012. readonly public string Key;
  1013. readonly public string Script;
  1014. readonly public ScriptEntryType ScriptEntryType;
  1015. public ScriptEntry Next;
  1016. public ScriptEntry (Type type, string key, string script, ScriptEntryType scriptEntryType) {
  1017. Key = key;
  1018. Type = type;
  1019. Script = script;
  1020. ScriptEntryType = scriptEntryType;
  1021. }
  1022. }
  1023. enum ScriptEntryType
  1024. {
  1025. ScriptContentNoTags,
  1026. ScriptContentWithTags,
  1027. ScriptPath,
  1028. OnSubmit
  1029. }
  1030. sealed class ArrayDeclaration
  1031. {
  1032. readonly public string ArrayName;
  1033. readonly public string ArrayValue;
  1034. public ArrayDeclaration (string arrayName, string arrayValue) {
  1035. ArrayName = arrayName;
  1036. ArrayValue = arrayValue;
  1037. }
  1038. }
  1039. sealed class CultureInfoConverter : JavaScriptConverter
  1040. {
  1041. private CultureInfoConverter () { }
  1042. static readonly Type typeofCultureInfo = typeof (CultureInfo);
  1043. static CultureInfoConverter _instance = new CultureInfoConverter ();
  1044. public static IEnumerable<JavaScriptConverter> GetConverters () { yield return _instance; }
  1045. public override IEnumerable<Type> SupportedTypes {
  1046. get { yield return typeofCultureInfo; }
  1047. }
  1048. public override object Deserialize (IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) {
  1049. throw new NotSupportedException ();
  1050. }
  1051. public override IDictionary<string, object> Serialize (object obj, JavaScriptSerializer serializer) {
  1052. CultureInfo ci = (CultureInfo) obj;
  1053. if (ci == null)
  1054. return null;
  1055. Dictionary<string, object> d = new Dictionary<string, object> (StringComparer.Ordinal);
  1056. d.Add ("name", ci.Name);
  1057. d.Add ("numberFormat", ci.NumberFormat);
  1058. d.Add ("dateTimeFormat", ci.DateTimeFormat);
  1059. return d;
  1060. }
  1061. }
  1062. sealed class ScriptReferenceEntry
  1063. {
  1064. readonly Control _control;
  1065. readonly ScriptReference _scriptReference;
  1066. public Control Control { get { return _control; } }
  1067. public ScriptReference ScriptReference { get { return _scriptReference; } }
  1068. public ScriptReferenceEntry (Control control, ScriptReference scriptReference) {
  1069. _control = control;
  1070. _scriptReference = scriptReference;
  1071. }
  1072. }
  1073. sealed class DataItemEntry
  1074. {
  1075. readonly string _dataItem;
  1076. readonly bool _isJsonSerialized;
  1077. public string DataItem { get { return _dataItem; } }
  1078. public bool IsJsonSerialized { get { return _isJsonSerialized; } }
  1079. public DataItemEntry (string dataItem, bool isJsonSerialized) {
  1080. _dataItem = dataItem;
  1081. _isJsonSerialized = isJsonSerialized;
  1082. }
  1083. }
  1084. }
  1085. }