ScriptManager.cs 41 KB

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