2
0

ClientScriptManager.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. //
  2. // System.Web.UI.ClientScriptManager.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. // Andreas Nahr ([email protected])
  8. // Lluis Sanchez ([email protected])
  9. //
  10. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  11. // (c) 2003 Novell, Inc. (http://www.novell.com)
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Collections;
  35. #if NET_2_0
  36. using System.Collections.Generic;
  37. #endif
  38. using System.Text;
  39. using System.Collections.Specialized;
  40. using System.Web.Util;
  41. using System.Globalization;
  42. namespace System.Web.UI
  43. {
  44. #if NET_2_0
  45. public sealed
  46. #else
  47. internal
  48. #endif
  49. class ClientScriptManager
  50. {
  51. Hashtable registeredArrayDeclares;
  52. ScriptEntry clientScriptBlocks;
  53. ScriptEntry startupScriptBlocks;
  54. internal Hashtable hiddenFields;
  55. ScriptEntry submitStatements;
  56. ScriptEntry scriptIncludes;
  57. Page page;
  58. #if NET_2_0
  59. List <int> eventValidationValues;
  60. Hashtable expandoAttributes;
  61. bool _hasRegisteredForEventValidationOnCallback;
  62. #endif
  63. internal ClientScriptManager (Page page)
  64. {
  65. this.page = page;
  66. }
  67. #if !NET_2_0
  68. public string GetPostBackClientEvent (Control control, string argument)
  69. {
  70. return GetPostBackEventReference (control, argument);
  71. }
  72. #endif
  73. public string GetPostBackClientHyperlink (Control control, string argument)
  74. {
  75. return "javascript:" + GetPostBackEventReference (control, argument);
  76. }
  77. #if NET_2_0
  78. public string GetPostBackClientHyperlink (Control control, string argument, bool registerForEventValidation)
  79. {
  80. if (registerForEventValidation)
  81. RegisterForEventValidation (control.UniqueID, argument);
  82. return "javascript:" + GetPostBackEventReference (control, argument);
  83. }
  84. #endif
  85. #if !NET_2_0
  86. internal
  87. #else
  88. public
  89. #endif
  90. string GetPostBackEventReference (Control control, string argument)
  91. {
  92. if (control == null)
  93. throw new ArgumentNullException ("control");
  94. page.RequiresPostBackScript ();
  95. #if NET_2_0
  96. return String.Format ("{0}.__doPostBack('{1}','{2}')", page.theForm, control.UniqueID, argument);
  97. #else
  98. return String.Format ("__doPostBack('{0}','{1}')", control.UniqueID, argument);
  99. #endif
  100. }
  101. #if NET_2_0
  102. public string GetPostBackEventReference (Control control, string argument, bool registerForEventValidation)
  103. {
  104. if (control == null)
  105. throw new ArgumentNullException ("control");
  106. if (registerForEventValidation)
  107. RegisterForEventValidation (control.UniqueID, argument);
  108. return GetPostBackEventReference (control, argument);
  109. }
  110. public string GetPostBackEventReference (PostBackOptions options, bool registerForEventValidation)
  111. {
  112. if (options == null)
  113. throw new ArgumentNullException ("options");
  114. if (registerForEventValidation)
  115. RegisterForEventValidation (options);
  116. return GetPostBackEventReference (options);
  117. }
  118. public string GetPostBackEventReference (PostBackOptions options)
  119. {
  120. if (options == null)
  121. throw new ArgumentNullException ("options");
  122. if (options.ActionUrl == null && options.ValidationGroup == null && !options.TrackFocus &&
  123. !options.AutoPostBack && !options.PerformValidation)
  124. {
  125. if (!options.ClientSubmit)
  126. return null;
  127. if (options.RequiresJavaScriptProtocol)
  128. return GetPostBackClientHyperlink (options.TargetControl, options.Argument);
  129. else
  130. return GetPostBackEventReference (options.TargetControl, options.Argument);
  131. }
  132. RegisterWebFormClientScript ();
  133. string actionUrl = options.ActionUrl;
  134. if (actionUrl != null)
  135. RegisterHiddenField (Page.PreviousPageID, page.Request.FilePath);
  136. if(options.TrackFocus)
  137. RegisterHiddenField (Page.LastFocusID, String.Empty);
  138. string prefix = options.RequiresJavaScriptProtocol ? "javascript:" : "";
  139. #if TARGET_J2EE
  140. // Allow the page to transform ActionUrl to a portlet action url
  141. if (actionUrl != null && page.PortletNamespace != null) {
  142. actionUrl = page.CreateActionUrl(actionUrl);
  143. prefix += "Portal";
  144. }
  145. #endif
  146. return String.Format ("{0}WebForm_DoPostback({1},{2},{3},{4},{5},{6},{7},{8},{9})",
  147. prefix,
  148. ClientScriptManager.GetScriptLiteral (options.TargetControl.UniqueID),
  149. ClientScriptManager.GetScriptLiteral (options.Argument),
  150. ClientScriptManager.GetScriptLiteral (actionUrl),
  151. ClientScriptManager.GetScriptLiteral (options.AutoPostBack),
  152. ClientScriptManager.GetScriptLiteral (options.PerformValidation),
  153. ClientScriptManager.GetScriptLiteral (options.TrackFocus),
  154. ClientScriptManager.GetScriptLiteral (options.ClientSubmit),
  155. ClientScriptManager.GetScriptLiteral (options.ValidationGroup),
  156. page.theForm
  157. );
  158. }
  159. internal void RegisterWebFormClientScript ()
  160. {
  161. if (IsClientScriptIncludeRegistered (typeof (Page), "webform"))
  162. return;
  163. RegisterClientScriptInclude (typeof (Page), "webform", GetWebResourceUrl (typeof (Page), "webform.js"));
  164. page.RequiresPostBackScript ();
  165. }
  166. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context)
  167. {
  168. return GetCallbackEventReference (control, argument, clientCallback, context, null, false);
  169. }
  170. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, bool useAsync)
  171. {
  172. return GetCallbackEventReference (control, argument, clientCallback, context, null, useAsync);
  173. }
  174. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
  175. {
  176. if (control == null)
  177. throw new ArgumentNullException ("control");
  178. if(!(control is ICallbackEventHandler))
  179. throw new InvalidOperationException ("The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent method.");
  180. return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, clientErrorCallback, useAsync);
  181. }
  182. public string GetCallbackEventReference (string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
  183. {
  184. RegisterWebFormClientScript ();
  185. return string.Format ("WebForm_DoCallback('{0}',{1},{2},{3},{4},{5},{6})", target, argument, clientCallback, context, ((clientErrorCallback == null) ? "null" : clientErrorCallback), (useAsync ? "true" : "false"), page.theForm);
  186. }
  187. #endif
  188. #if NET_2_0
  189. public
  190. #else
  191. internal
  192. #endif
  193. string GetWebResourceUrl(Type type, string resourceName)
  194. {
  195. if (type == null)
  196. throw new ArgumentNullException ("type");
  197. if (resourceName == null || resourceName.Length == 0)
  198. throw new ArgumentNullException ("type");
  199. return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
  200. }
  201. public bool IsClientScriptBlockRegistered (string key)
  202. {
  203. return IsScriptRegistered (clientScriptBlocks, GetType(), key);
  204. }
  205. public bool IsClientScriptBlockRegistered (Type type, string key)
  206. {
  207. return IsScriptRegistered (clientScriptBlocks, type, key);
  208. }
  209. public bool IsStartupScriptRegistered (string key)
  210. {
  211. return IsScriptRegistered (startupScriptBlocks, GetType(), key);
  212. }
  213. public bool IsStartupScriptRegistered (Type type, string key)
  214. {
  215. return IsScriptRegistered (startupScriptBlocks, type, key);
  216. }
  217. public bool IsOnSubmitStatementRegistered (string key)
  218. {
  219. return IsScriptRegistered (submitStatements, GetType(), key);
  220. }
  221. public bool IsOnSubmitStatementRegistered (Type type, string key)
  222. {
  223. return IsScriptRegistered (submitStatements, type, key);
  224. }
  225. public bool IsClientScriptIncludeRegistered (string key)
  226. {
  227. return IsScriptRegistered (scriptIncludes, GetType(), key);
  228. }
  229. public bool IsClientScriptIncludeRegistered (Type type, string key)
  230. {
  231. return IsScriptRegistered (scriptIncludes, type, key);
  232. }
  233. bool IsScriptRegistered (ScriptEntry scriptList, Type type, string key)
  234. {
  235. while (scriptList != null) {
  236. if (scriptList.Type == type && scriptList.Key == key)
  237. return true;
  238. scriptList = scriptList.Next;
  239. }
  240. return false;
  241. }
  242. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  243. {
  244. if (registeredArrayDeclares == null)
  245. registeredArrayDeclares = new Hashtable();
  246. if (!registeredArrayDeclares.ContainsKey (arrayName))
  247. registeredArrayDeclares.Add (arrayName, new ArrayList());
  248. ((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
  249. }
  250. void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, bool addScriptTags)
  251. {
  252. ScriptEntry last = null;
  253. ScriptEntry entry = scriptList;
  254. while (entry != null) {
  255. if (entry.Type == type && entry.Key == key)
  256. return;
  257. last = entry;
  258. entry = entry.Next;
  259. }
  260. if (addScriptTags) {
  261. script = "<script type=\"text/javascript\"" +
  262. #if !NET_2_0
  263. "language=\"javascript\"" +
  264. #endif
  265. ">\n<!--\n" + script + "\n// -->\n</script>";
  266. }
  267. entry = new ScriptEntry (type, key, script);
  268. if (last != null) last.Next = entry;
  269. else scriptList = entry;
  270. }
  271. internal void RegisterClientScriptBlock (string key, string script)
  272. {
  273. RegisterScript (ref clientScriptBlocks, GetType(), key, script, false);
  274. }
  275. public void RegisterClientScriptBlock (Type type, string key, string script)
  276. {
  277. RegisterClientScriptBlock (type, key, script, false);
  278. }
  279. public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags)
  280. {
  281. if (type == null)
  282. throw new ArgumentNullException ("type");
  283. RegisterScript (ref clientScriptBlocks, type, key, script, addScriptTags);
  284. }
  285. public void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  286. {
  287. if (hiddenFields == null)
  288. hiddenFields = new Hashtable ();
  289. if (!hiddenFields.ContainsKey (hiddenFieldName))
  290. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  291. }
  292. internal void RegisterOnSubmitStatement (string key, string script)
  293. {
  294. RegisterScript (ref submitStatements, GetType (), key, script, false);
  295. }
  296. public void RegisterOnSubmitStatement (Type type, string key, string script)
  297. {
  298. if (type == null)
  299. throw new ArgumentNullException ("type");
  300. RegisterScript (ref submitStatements, type, key, script, false);
  301. }
  302. internal void RegisterStartupScript (string key, string script)
  303. {
  304. RegisterScript (ref startupScriptBlocks, GetType(), key, script, false);
  305. }
  306. public void RegisterStartupScript (Type type, string key, string script)
  307. {
  308. RegisterStartupScript (type, key, script, false);
  309. }
  310. public void RegisterStartupScript (Type type, string key, string script, bool addScriptTags)
  311. {
  312. if (type == null)
  313. throw new ArgumentNullException ("type");
  314. RegisterScript (ref startupScriptBlocks, type, key, script, addScriptTags);
  315. }
  316. public void RegisterClientScriptInclude (string key, string url)
  317. {
  318. RegisterClientScriptInclude (GetType (), key, url);
  319. }
  320. public void RegisterClientScriptInclude (Type type, string key, string url)
  321. {
  322. if (type == null)
  323. throw new ArgumentNullException ("type");
  324. if (url == null || url.Length == 0)
  325. throw new ArgumentException ("url");
  326. RegisterScript (ref scriptIncludes, type, key, url, false);
  327. }
  328. #if NET_2_0
  329. public void RegisterClientScriptResource (Type type, string resourceName)
  330. {
  331. RegisterScript (ref scriptIncludes, type, "resource-" + resourceName, GetWebResourceUrl (type, resourceName), false);
  332. }
  333. public void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue)
  334. {
  335. RegisterExpandoAttribute (controlId, attributeName, attributeValue, true);
  336. }
  337. public void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode)
  338. {
  339. if (controlId == null)
  340. throw new ArgumentNullException ("controlId");
  341. if (attributeName == null)
  342. throw new ArgumentNullException ("attributeName");
  343. if (expandoAttributes == null)
  344. expandoAttributes = new Hashtable ();
  345. ListDictionary list = (ListDictionary)expandoAttributes [controlId];
  346. if (list == null) {
  347. list = new ListDictionary ();
  348. expandoAttributes [controlId] = list;
  349. }
  350. list.Add (attributeName, encode ? StrUtils.EscapeQuotesAndBackslashes (attributeValue) : attributeValue);
  351. }
  352. // Implemented following the description in http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx
  353. private int CalculateEventHash (string uniqueId, string argument)
  354. {
  355. int uniqueIdHash = uniqueId.GetHashCode ();
  356. int argumentHash = String.IsNullOrEmpty (argument) ? 0 : argument.GetHashCode ();
  357. return (uniqueIdHash ^ argumentHash);
  358. }
  359. public void RegisterForEventValidation (PostBackOptions options)
  360. {
  361. // MS.NET does not check for options == null, so we won't too...
  362. RegisterForEventValidation (options.TargetControl.UniqueID, options.Argument);
  363. }
  364. public void RegisterForEventValidation (string uniqueId)
  365. {
  366. RegisterForEventValidation (uniqueId, null);
  367. }
  368. public void RegisterForEventValidation (string uniqueId, string argument)
  369. {
  370. if (!page.EnableEventValidation)
  371. return;
  372. if (uniqueId == null || uniqueId.Length == 0)
  373. return;
  374. if (page.IsCallback)
  375. _hasRegisteredForEventValidationOnCallback = true;
  376. else if (page.LifeCycle < PageLifeCycle.Render)
  377. throw new InvalidOperationException ("RegisterForEventValidation may only be called from the Render method");
  378. if (eventValidationValues == null)
  379. eventValidationValues = new List <int> ();
  380. int hash = CalculateEventHash (uniqueId, argument);
  381. if (eventValidationValues.BinarySearch (hash) < 0)
  382. eventValidationValues.Add (hash);
  383. }
  384. public void ValidateEvent (string uniqueId)
  385. {
  386. ValidateEvent (uniqueId, null);
  387. }
  388. public void ValidateEvent (string uniqueId, string argument)
  389. {
  390. if (uniqueId == null || uniqueId.Length == 0)
  391. throw new ArgumentException ("must not be null or empty", "uniqueId");
  392. if (!page.EnableEventValidation)
  393. return;
  394. if (eventValidationValues == null)
  395. goto bad;
  396. int hash = CalculateEventHash (uniqueId, argument);
  397. if (eventValidationValues.BinarySearch (hash) < 0)
  398. goto bad;
  399. return;
  400. bad:
  401. throw new ArgumentException ("Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation=\"true\"/> in configuration or <%@ Page EnableEventValidation=\"true\" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.");
  402. }
  403. #endif
  404. void WriteScripts (HtmlTextWriter writer, ScriptEntry scriptList)
  405. {
  406. while (scriptList != null) {
  407. writer.WriteLine (scriptList.Script);
  408. scriptList = scriptList.Next;
  409. }
  410. }
  411. #if NET_2_0
  412. internal void RestoreEventValidationState (string fieldValue)
  413. {
  414. if (!page.EnableEventValidation || fieldValue == null || fieldValue.Length == 0)
  415. return;
  416. IStateFormatter fmt = page.GetFormatter ();
  417. int [] eventValues = (int []) fmt.Deserialize (fieldValue);
  418. #if TARGET_JVM // FIXME: No support yet for passing 'int[]' as 'T[]'
  419. eventValidationValues = new List<int> (eventValues.Length);
  420. for (int i = 0; i < eventValues.Length; i++)
  421. eventValidationValues.Add(eventValues[i]);
  422. #else
  423. eventValidationValues = new List<int> (eventValues);
  424. #endif
  425. eventValidationValues.Sort ();
  426. }
  427. internal void SaveEventValidationState ()
  428. {
  429. if (!page.EnableEventValidation)
  430. return;
  431. string eventValidation = GetEventValidationStateFormatted ();
  432. if (eventValidation == null)
  433. return;
  434. RegisterHiddenField (EventStateFieldName, eventValidation);
  435. }
  436. internal string GetEventValidationStateFormatted ()
  437. {
  438. if (eventValidationValues == null || eventValidationValues.Count == 0)
  439. return null;
  440. if(page.IsCallback && !_hasRegisteredForEventValidationOnCallback)
  441. return null;
  442. IStateFormatter fmt = page.GetFormatter ();
  443. int [] array = new int [eventValidationValues.Count];
  444. #if TARGET_JVM // FIXME: No support yet for passing 'int[]' as 'T[]'
  445. ((ICollection)eventValidationValues).CopyTo (array, 0);
  446. #else
  447. eventValidationValues.CopyTo (array);
  448. #endif
  449. return fmt.Serialize (array);
  450. }
  451. internal string EventStateFieldName
  452. {
  453. get { return "__EVENTVALIDATION"; }
  454. }
  455. internal void WriteExpandoAttributes (HtmlTextWriter writer)
  456. {
  457. if (expandoAttributes == null)
  458. return;
  459. writer.WriteLine ();
  460. WriteBeginScriptBlock (writer);
  461. foreach (string controlId in expandoAttributes.Keys) {
  462. writer.WriteLine ("var {0} = document.all ? document.all [\"{0}\"] : document.getElementById (\"{0}\");", controlId);
  463. ListDictionary attrs = (ListDictionary) expandoAttributes [controlId];
  464. foreach (string attributeName in attrs.Keys) {
  465. writer.WriteLine ("{0}.{1} = \"{2}\";", controlId, attributeName, attrs [attributeName]);
  466. }
  467. }
  468. WriteEndScriptBlock (writer);
  469. writer.WriteLine ();
  470. }
  471. #endif
  472. internal void WriteBeginScriptBlock (HtmlTextWriter writer)
  473. {
  474. writer.WriteLine ("<script"+
  475. #if !NET_2_0
  476. " language=\"javascript\""+
  477. #endif
  478. " type=\"text/javascript\">");
  479. writer.WriteLine ("<!--");
  480. }
  481. internal void WriteEndScriptBlock (HtmlTextWriter writer)
  482. {
  483. writer.WriteLine ("// -->");
  484. writer.WriteLine ("</script>");
  485. }
  486. internal void WriteHiddenFields (HtmlTextWriter writer)
  487. {
  488. if (hiddenFields == null)
  489. return;
  490. foreach (string key in hiddenFields.Keys) {
  491. string value = hiddenFields [key] as string;
  492. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", key, value);
  493. }
  494. hiddenFields = null;
  495. }
  496. internal void WriteClientScriptIncludes (HtmlTextWriter writer)
  497. {
  498. ScriptEntry entry = scriptIncludes;
  499. while (entry != null) {
  500. if (!entry.Rendered) {
  501. #if TARGET_J2EE
  502. if (!page.IsPortletRender)
  503. #endif
  504. writer.WriteLine ("\n<script src=\"{0}\" type=\"text/javascript\"></script>", entry.Script);
  505. #if TARGET_J2EE
  506. else {
  507. string scriptKey = "inc_" + entry.Key.GetHashCode ().ToString ("X");
  508. writer.WriteLine ("\n<script type=\"text/javascript\">");
  509. writer.WriteLine ("<!--");
  510. writer.WriteLine ("if (document.{0} == null) {{", scriptKey);
  511. writer.WriteLine ("\tdocument.{0} = true", scriptKey);
  512. writer.WriteLine ("\tdocument.write('<script src=\"{0}\" type=\"text/javascript\"><\\/script>'); }}", entry.Script);
  513. writer.WriteLine ("// -->");
  514. writer.WriteLine ("</script>");
  515. }
  516. #endif
  517. entry.Rendered = true;
  518. }
  519. entry = entry.Next;
  520. }
  521. }
  522. internal void WriteClientScriptBlocks (HtmlTextWriter writer)
  523. {
  524. WriteScripts (writer, clientScriptBlocks);
  525. }
  526. internal void WriteStartupScriptBlocks (HtmlTextWriter writer)
  527. {
  528. WriteScripts (writer, startupScriptBlocks);
  529. }
  530. internal void WriteArrayDeclares (HtmlTextWriter writer)
  531. {
  532. if (registeredArrayDeclares != null) {
  533. writer.WriteLine();
  534. WriteBeginScriptBlock (writer);
  535. IDictionaryEnumerator arrayEnum = registeredArrayDeclares.GetEnumerator();
  536. while (arrayEnum.MoveNext()) {
  537. writer.Write("\tvar ");
  538. writer.Write(arrayEnum.Key);
  539. writer.Write(" = new Array(");
  540. IEnumerator arrayListEnum = ((ArrayList) arrayEnum.Value).GetEnumerator();
  541. bool isFirst = true;
  542. while (arrayListEnum.MoveNext()) {
  543. if (isFirst)
  544. isFirst = false;
  545. else
  546. writer.Write(", ");
  547. writer.Write(arrayListEnum.Current);
  548. }
  549. writer.WriteLine(");");
  550. #if TARGET_J2EE
  551. // in addition, add a form array declaration
  552. if (page.IsPortletRender) {
  553. writer.Write ("\t" + page.theForm + ".");
  554. writer.Write (arrayEnum.Key);
  555. writer.Write (" = ");
  556. writer.Write (arrayEnum.Key);
  557. writer.WriteLine (";");
  558. }
  559. #endif
  560. }
  561. WriteEndScriptBlock (writer);
  562. writer.WriteLine ();
  563. }
  564. }
  565. #if NET_2_0
  566. internal string GetClientValidationEvent (string validationGroup) {
  567. string eventScript = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate('" + validationGroup + "');";
  568. #if TARGET_J2EE
  569. if (page.IsPortletRender)
  570. return "if (typeof(SetValidatorContext) == 'function') SetValidatorContext ('" + page.theForm + "'); " + eventScript;
  571. #endif
  572. return eventScript;
  573. }
  574. #endif
  575. internal string GetClientValidationEvent ()
  576. {
  577. string eventScript = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
  578. #if TARGET_J2EE
  579. if (page.IsPortletRender)
  580. return "if (typeof(SetValidatorContext) == 'function') SetValidatorContext ('" + page.theForm + "'); " + eventScript;
  581. #endif
  582. return eventScript;
  583. }
  584. internal string WriteSubmitStatements ()
  585. {
  586. if (submitStatements == null) return null;
  587. StringBuilder sb = new StringBuilder ();
  588. ScriptEntry entry = submitStatements;
  589. while (entry != null) {
  590. #if NET_2_0
  591. sb.Append (EnsureEndsWithSemicolon (entry.Script));
  592. #else
  593. sb.Append (entry.Script);
  594. #endif
  595. entry = entry.Next;
  596. }
  597. #if NET_2_0
  598. RegisterClientScriptBlock ("HtmlForm-OnSubmitStatemen",
  599. @"<script type=""text/javascript"">
  600. <!--
  601. " + page.theForm + @".WebForm_OnSubmit = function () {
  602. " + sb.ToString () + @"
  603. return true;
  604. }
  605. // -->
  606. </script>");
  607. return "javascript:return this.WebForm_OnSubmit();";
  608. #else
  609. return sb.ToString ();
  610. #endif
  611. }
  612. [MonoTODO ("optimize s.Replace")]
  613. internal static string GetScriptLiteral (object ob)
  614. {
  615. if (ob == null)
  616. return "null";
  617. else if (ob is string) {
  618. string s = (string)ob;
  619. s = s.Replace ("\\", "\\\\");
  620. s = s.Replace ("\"", "\\\"");
  621. return "\"" + s + "\"";
  622. } else if (ob is bool) {
  623. return ob.ToString ().ToLower (CultureInfo.InvariantCulture);
  624. } else {
  625. return ob.ToString ();
  626. }
  627. }
  628. class ScriptEntry
  629. {
  630. public Type Type;
  631. public string Key;
  632. public string Script;
  633. public ScriptEntry Next;
  634. public bool Rendered;
  635. public ScriptEntry (Type type, string key, string script)
  636. {
  637. Key = key;
  638. Type = type;
  639. Script = script;
  640. }
  641. }
  642. #if NET_2_0
  643. // helper method
  644. internal static string EnsureEndsWithSemicolon (string value) {
  645. if (value != null && value.Length > 0 && value [value.Length - 1] != ';')
  646. return value += ";";
  647. return value;
  648. }
  649. #endif
  650. }
  651. }