ScriptManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. namespace System.Web.UI
  41. {
  42. [ParseChildrenAttribute (true)]
  43. [DefaultPropertyAttribute ("Scripts")]
  44. [DesignerAttribute ("System.Web.UI.Design.ScriptManagerDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  45. [NonVisualControlAttribute]
  46. [PersistChildrenAttribute (false)]
  47. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  48. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  49. public class ScriptManager : Control, IPostBackDataHandler
  50. {
  51. // the keywords are used in fomatting async response
  52. const string updatePanel = "updatePanel";
  53. const string hiddenField = "hiddenField";
  54. const string arrayDeclaration = "arrayDeclaration";
  55. const string scriptBlock = "scriptBlock";
  56. const string expando = "expando";
  57. const string onSubmit = "onSubmit";
  58. const string asyncPostBackControlIDs = "asyncPostBackControlIDs";
  59. const string postBackControlIDs = "postBackControlIDs";
  60. const string updatePanelIDs = "updatePanelIDs";
  61. const string asyncPostBackTimeout = "asyncPostBackTimeout";
  62. const string childUpdatePanelIDs = "childUpdatePanelIDs";
  63. const string panelsToRefreshIDs = "panelsToRefreshIDs";
  64. const string formAction = "formAction";
  65. const string dataItem = "dataItem";
  66. const string dataItemJson = "dataItemJson";
  67. const string scriptDispose = "scriptDispose";
  68. const string pageRedirect = "pageRedirect";
  69. const string error = "error";
  70. const string pageTitle = "pageTitle";
  71. const string focus = "focus";
  72. int _asyncPostBackTimeout = 90;
  73. List<Control> _asyncPostBackControls;
  74. List<Control> _postBackControls;
  75. ScriptReferenceCollection _scripts;
  76. bool _isInAsyncPostBack;
  77. string _asyncPostBackSourceElementID;
  78. ScriptMode _scriptMode = ScriptMode.Auto;
  79. HtmlTextWriter _output;
  80. [DefaultValue (true)]
  81. [Category ("Behavior")]
  82. public bool AllowCustomErrorsRedirect {
  83. get {
  84. throw new NotImplementedException ();
  85. }
  86. set {
  87. throw new NotImplementedException ();
  88. }
  89. }
  90. [Category ("Behavior")]
  91. [DefaultValue ("")]
  92. public string AsyncPostBackErrorMessage {
  93. get {
  94. throw new NotImplementedException ();
  95. }
  96. set {
  97. throw new NotImplementedException ();
  98. }
  99. }
  100. [Browsable (false)]
  101. public string AsyncPostBackSourceElementID {
  102. get {
  103. if(_asyncPostBackSourceElementID==null)
  104. return String.Empty;
  105. return _asyncPostBackSourceElementID;
  106. }
  107. }
  108. [DefaultValue (90)]
  109. [Category ("Behavior")]
  110. public int AsyncPostBackTimeout {
  111. get {
  112. return _asyncPostBackTimeout;
  113. }
  114. set {
  115. _asyncPostBackTimeout = value;
  116. }
  117. }
  118. [Category ("Behavior")]
  119. [MergableProperty (false)]
  120. [DefaultValue ("")]
  121. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  122. [PersistenceMode (PersistenceMode.InnerProperty)]
  123. public AuthenticationServiceManager AuthenticationService {
  124. get {
  125. throw new NotImplementedException ();
  126. }
  127. }
  128. [Category ("Behavior")]
  129. [DefaultValue (false)]
  130. public bool EnablePageMethods {
  131. get {
  132. throw new NotImplementedException ();
  133. }
  134. set {
  135. throw new NotImplementedException ();
  136. }
  137. }
  138. [DefaultValue (true)]
  139. [Category ("Behavior")]
  140. public bool EnablePartialRendering {
  141. get {
  142. throw new NotImplementedException ();
  143. }
  144. set {
  145. throw new NotImplementedException ();
  146. }
  147. }
  148. [DefaultValue (false)]
  149. [Category ("Behavior")]
  150. public bool EnableScriptGlobalization {
  151. get {
  152. throw new NotImplementedException ();
  153. }
  154. set {
  155. throw new NotImplementedException ();
  156. }
  157. }
  158. [Category ("Behavior")]
  159. [DefaultValue (false)]
  160. public bool EnableScriptLocalization {
  161. get {
  162. throw new NotImplementedException ();
  163. }
  164. set {
  165. throw new NotImplementedException ();
  166. }
  167. }
  168. [Browsable (false)]
  169. public bool IsDebuggingEnabled {
  170. get {
  171. DeploymentSection deployment = (DeploymentSection) WebConfigurationManager.GetSection ("system.web/deployment");
  172. if (deployment.Retail)
  173. return false;
  174. CompilationSection compilation = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  175. if (!compilation.Debug && (ScriptMode == ScriptMode.Auto || ScriptMode == ScriptMode.Inherit))
  176. return false;
  177. if (ScriptMode == ScriptMode.Release)
  178. return false;
  179. return true;
  180. }
  181. }
  182. [Browsable (false)]
  183. public bool IsInAsyncPostBack {
  184. get {
  185. return _isInAsyncPostBack;
  186. }
  187. }
  188. [Category ("Behavior")]
  189. [DefaultValue (true)]
  190. public bool LoadScriptsBeforeUI {
  191. get {
  192. throw new NotImplementedException ();
  193. }
  194. set {
  195. throw new NotImplementedException ();
  196. }
  197. }
  198. [PersistenceMode (PersistenceMode.InnerProperty)]
  199. [DefaultValue ("")]
  200. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  201. [Category ("Behavior")]
  202. [MergableProperty (false)]
  203. public ProfileServiceManager ProfileService {
  204. get {
  205. throw new NotImplementedException ();
  206. }
  207. }
  208. [Category ("Behavior")]
  209. public ScriptMode ScriptMode {
  210. get {
  211. return _scriptMode;
  212. }
  213. set {
  214. if (value == ScriptMode.Inherit)
  215. value = ScriptMode.Auto;
  216. _scriptMode = value;
  217. }
  218. }
  219. [DefaultValue ("")]
  220. [Category ("Behavior")]
  221. public string ScriptPath {
  222. get {
  223. throw new NotImplementedException ();
  224. }
  225. set {
  226. throw new NotImplementedException ();
  227. }
  228. }
  229. [PersistenceMode (PersistenceMode.InnerProperty)]
  230. [DefaultValue ("")]
  231. [Category ("Behavior")]
  232. [MergableProperty (false)]
  233. public ScriptReferenceCollection Scripts {
  234. get {
  235. if (_scripts == null)
  236. _scripts = new ScriptReferenceCollection ();
  237. return _scripts;
  238. }
  239. }
  240. [PersistenceMode (PersistenceMode.InnerProperty)]
  241. [DefaultValue ("")]
  242. [MergableProperty (false)]
  243. [Category ("Behavior")]
  244. public ServiceReferenceCollection Services {
  245. get {
  246. throw new NotImplementedException ();
  247. }
  248. }
  249. [DefaultValue (true)]
  250. [Browsable (false)]
  251. public bool SupportsPartialRendering {
  252. get {
  253. throw new NotImplementedException ();
  254. }
  255. set {
  256. throw new NotImplementedException ();
  257. }
  258. }
  259. [EditorBrowsable (EditorBrowsableState.Never)]
  260. [Browsable (false)]
  261. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  262. public override bool Visible {
  263. get {
  264. return true;
  265. }
  266. set {
  267. throw new NotImplementedException ();
  268. }
  269. }
  270. [Category ("Action")]
  271. public event EventHandler<AsyncPostBackErrorEventArgs> AsyncPostBackError;
  272. [Category ("Action")]
  273. public event EventHandler<ScriptReferenceEventArgs> ResolveScriptReference;
  274. public static ScriptManager GetCurrent (Page page)
  275. {
  276. HttpContext ctx = HttpContext.Current;
  277. if (ctx == null)
  278. return null;
  279. return (ScriptManager) ctx.Items [page];
  280. }
  281. static void SetCurrent (Page page, ScriptManager instance) {
  282. HttpContext ctx = HttpContext.Current;
  283. ctx.Items [page] = instance;
  284. }
  285. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  286. {
  287. _isInAsyncPostBack = true;
  288. _asyncPostBackSourceElementID = postCollection [postDataKey];
  289. return false;
  290. }
  291. protected internal virtual void OnAsyncPostBackError (AsyncPostBackErrorEventArgs e)
  292. {
  293. if (AsyncPostBackError != null)
  294. AsyncPostBackError (this, e);
  295. }
  296. protected override void OnInit (EventArgs e)
  297. {
  298. base.OnInit (e);
  299. if (GetCurrent (Page) != null)
  300. throw new InvalidOperationException ("Only one instance of a ScriptManager can be added to the page.");
  301. SetCurrent (Page, this);
  302. }
  303. protected override void OnPreRender (EventArgs e)
  304. {
  305. base.OnPreRender (e);
  306. if (IsInAsyncPostBack) {
  307. Page.SetRenderMethodDelegate (RenderPageCallback);
  308. }
  309. // Register Scripts
  310. foreach (ScriptReference script in GetScriptReferences ()) {
  311. OnResolveScriptReference (new ScriptReferenceEventArgs (script));
  312. RegisterScriptReference (script);
  313. }
  314. // Register startup script
  315. RegisterStartupScript (this, typeof (ScriptManager), "Sys.Application.initialize();", "Sys.Application.initialize();", true);
  316. }
  317. IEnumerable GetScriptReferences () {
  318. ScriptReference script;
  319. script = new ScriptReference ("MicrosoftAjax.js", String.Empty);
  320. yield return script;
  321. script = new ScriptReference ("MicrosoftAjaxWebForms.js", String.Empty);
  322. yield return script;
  323. if (_scripts != null && _scripts.Count > 0) {
  324. for (int i = 0; i < _scripts.Count; i++) {
  325. yield return _scripts [i];
  326. }
  327. }
  328. }
  329. protected virtual void OnResolveScriptReference (ScriptReferenceEventArgs e)
  330. {
  331. if (ResolveScriptReference != null)
  332. ResolveScriptReference (this, e);
  333. }
  334. protected virtual void RaisePostDataChangedEvent ()
  335. {
  336. throw new NotImplementedException ();
  337. }
  338. public static void RegisterArrayDeclaration (Control control, string arrayName, string arrayValue)
  339. {
  340. throw new NotImplementedException ();
  341. }
  342. public static void RegisterArrayDeclaration (Page page, string arrayName, string arrayValue)
  343. {
  344. throw new NotImplementedException ();
  345. }
  346. public void RegisterAsyncPostBackControl (Control control)
  347. {
  348. if(control==null)
  349. return;
  350. if (_asyncPostBackControls == null)
  351. _asyncPostBackControls = new List<Control> ();
  352. if (_asyncPostBackControls.Contains (control))
  353. return;
  354. _asyncPostBackControls.Add (control);
  355. }
  356. public static void RegisterClientScriptBlock (Control control, Type type, string key, string script, bool addScriptTags)
  357. {
  358. throw new NotImplementedException ();
  359. }
  360. public static void RegisterClientScriptBlock (Page page, Type type, string key, string script, bool addScriptTags)
  361. {
  362. throw new NotImplementedException ();
  363. }
  364. public static void RegisterClientScriptInclude (Control control, Type type, string key, string url)
  365. {
  366. RegisterClientScriptInclude (control.Page, type, key, url);
  367. }
  368. public static void RegisterClientScriptInclude (Page page, Type type, string key, string url)
  369. {
  370. page.ClientScript.RegisterClientScriptInclude (type, key, url);
  371. }
  372. public static void RegisterClientScriptResource (Control control, Type type, string resourceName)
  373. {
  374. RegisterClientScriptResource (control.Page, type, resourceName);
  375. }
  376. public static void RegisterClientScriptResource (Page page, Type type, string resourceName)
  377. {
  378. page.ClientScript.RegisterClientScriptResource (type, resourceName);
  379. }
  380. void RegisterScriptReference (ScriptReference script) {
  381. // TODO: consider 'retail' attribute of the 'deployment' configuration element in Web.config,
  382. // IsDebuggingEnabled and ScriptMode properties to determine whether to render debug scripts.
  383. string url;
  384. if (!String.IsNullOrEmpty (script.Path)) {
  385. url = script.Path;
  386. }
  387. else if (!String.IsNullOrEmpty (script.Name)) {
  388. Assembly assembly;
  389. if (String.IsNullOrEmpty (script.Assembly))
  390. assembly = typeof (ScriptManager).Assembly;
  391. else
  392. assembly = Assembly.Load (script.Assembly);
  393. url = ScriptResourceHandler.GetResourceUrl (assembly, script.Name);
  394. }
  395. else {
  396. throw new InvalidOperationException ("Name and Path cannot both be empty.");
  397. }
  398. RegisterClientScriptInclude (this, typeof (ScriptManager), url, url);
  399. }
  400. public void RegisterDataItem (Control control, string dataItem)
  401. {
  402. throw new NotImplementedException ();
  403. }
  404. public void RegisterDataItem (Control control, string dataItem, bool isJsonSerialized)
  405. {
  406. throw new NotImplementedException ();
  407. }
  408. public void RegisterDispose (Control control, string disposeScript)
  409. {
  410. throw new NotImplementedException ();
  411. }
  412. public static void RegisterExpandoAttribute (Control control, string controlId, string attributeName, string attributeValue, bool encode)
  413. {
  414. throw new NotImplementedException ();
  415. }
  416. public static void RegisterHiddenField (Control control, string hiddenFieldName, string hiddenFieldInitialValue)
  417. {
  418. throw new NotImplementedException ();
  419. }
  420. public static void RegisterHiddenField (Page page, string hiddenFieldName, string hiddenFieldInitialValue)
  421. {
  422. throw new NotImplementedException ();
  423. }
  424. public static void RegisterOnSubmitStatement (Control control, Type type, string key, string script)
  425. {
  426. throw new NotImplementedException ();
  427. }
  428. public static void RegisterOnSubmitStatement (Page page, Type type, string key, string script)
  429. {
  430. throw new NotImplementedException ();
  431. }
  432. public void RegisterPostBackControl (Control control)
  433. {
  434. if (control == null)
  435. return;
  436. if (_postBackControls == null)
  437. _postBackControls = new List<Control> ();
  438. if (_postBackControls.Contains (control))
  439. return;
  440. _postBackControls.Add (control);
  441. }
  442. public void RegisterScriptDescriptors (IExtenderControl extenderControl)
  443. {
  444. throw new NotImplementedException ();
  445. }
  446. public void RegisterScriptDescriptors (IScriptControl scriptControl)
  447. {
  448. throw new NotImplementedException ();
  449. }
  450. public static void RegisterStartupScript (Control control, Type type, string key, string script, bool addScriptTags)
  451. {
  452. RegisterStartupScript (control.Page, type, key, script, addScriptTags);
  453. }
  454. public static void RegisterStartupScript (Page page, Type type, string key, string script, bool addScriptTags)
  455. {
  456. page.ClientScript.RegisterStartupScript (type, key, script, addScriptTags);
  457. }
  458. protected override void Render (HtmlTextWriter writer)
  459. {
  460. // MSDN: This method is used by control developers to extend the ScriptManager control.
  461. // Notes to Inheritors:
  462. // When overriding this method, call the base Render(HtmlTextWriter) method
  463. // so that PageRequestManager is rendered on the page.
  464. writer.WriteLine ("<script type=\"text/javascript\">");
  465. writer.WriteLine ("//<![CDATA[");
  466. writer.WriteLine ("Sys.WebForms.PageRequestManager._initialize('{0}', document.getElementById('{1}'));", UniqueID, Page.Form.ClientID);
  467. writer.WriteLine ("Sys.WebForms.PageRequestManager.getInstance()._updateControls([{0}], [{1}], [{2}], {3});", null, FormatListIDs (_asyncPostBackControls), FormatListIDs (_postBackControls), AsyncPostBackTimeout);
  468. writer.WriteLine ("//]]");
  469. writer.WriteLine ("</script>");
  470. base.Render (writer);
  471. }
  472. static string FormatListIDs(List<Control> list)
  473. {
  474. if (list == null || list.Count == 0)
  475. return null;
  476. StringBuilder sb = new StringBuilder ();
  477. for (int i = 0; i < list.Count; i++) {
  478. sb.AppendFormat ("'{0}',", list [i].UniqueID);
  479. }
  480. if (sb.Length > 0)
  481. sb.Length--;
  482. return sb.ToString ();
  483. }
  484. public void SetFocus (Control control)
  485. {
  486. throw new NotImplementedException ();
  487. }
  488. public void SetFocus (string clientID)
  489. {
  490. throw new NotImplementedException ();
  491. }
  492. #region IPostBackDataHandler Members
  493. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  494. {
  495. return LoadPostData (postDataKey, postCollection);
  496. }
  497. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  498. {
  499. RaisePostDataChangedEvent ();
  500. }
  501. #endregion
  502. void RenderPageCallback (HtmlTextWriter output, Control container) {
  503. Page page = (Page) container;
  504. _output = output;
  505. page.Form.SetRenderMethodDelegate (RenderFormCallback);
  506. page.Form.RenderControl (output);
  507. }
  508. void RenderFormCallback (HtmlTextWriter output, Control container) {
  509. HtmlForm form = (HtmlForm) container;
  510. }
  511. }
  512. }