ClientScriptManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. namespace System.Web.UI
  42. {
  43. #if NET_2_0
  44. public sealed
  45. #else
  46. internal
  47. #endif
  48. class ClientScriptManager
  49. {
  50. Hashtable registeredArrayDeclares;
  51. ScriptEntry clientScriptBlocks;
  52. ScriptEntry startupScriptBlocks;
  53. internal Hashtable hiddenFields;
  54. ScriptEntry submitStatements;
  55. ScriptEntry scriptIncludes;
  56. Page page;
  57. #if NET_2_0
  58. List <int> eventValidationValues;
  59. Hashtable expandoAttributes;
  60. bool _hasRegisteredForEventValidationOnCallback;
  61. #endif
  62. internal ClientScriptManager (Page page)
  63. {
  64. this.page = page;
  65. }
  66. #if !NET_2_0
  67. public string GetPostBackClientEvent (Control control, string argument)
  68. {
  69. return GetPostBackEventReference (control, argument);
  70. }
  71. #endif
  72. public string GetPostBackClientHyperlink (Control control, string argument)
  73. {
  74. return "javascript:" + GetPostBackEventReference (control, argument);
  75. }
  76. #if NET_2_0
  77. public string GetPostBackClientHyperlink (Control control, string argument, bool registerForEventValidation)
  78. {
  79. if (registerForEventValidation)
  80. RegisterForEventValidation (control.UniqueID, argument);
  81. return "javascript:" + GetPostBackEventReference (control, argument);
  82. }
  83. #endif
  84. #if !NET_2_0
  85. internal
  86. #else
  87. public
  88. #endif
  89. string GetPostBackEventReference (Control control, string argument)
  90. {
  91. if (control == null)
  92. throw new ArgumentNullException ("control");
  93. page.RequiresPostBackScript ();
  94. return String.Format ("__doPostBack('{0}','{1}')", control.UniqueID, argument);
  95. }
  96. #if NET_2_0
  97. public string GetPostBackEventReference (Control control, string argument, bool registerForEventValidation)
  98. {
  99. if (control == null)
  100. throw new ArgumentNullException ("control");
  101. if (registerForEventValidation)
  102. RegisterForEventValidation (control.UniqueID, argument);
  103. return GetPostBackEventReference (control, argument);
  104. }
  105. public string GetPostBackEventReference (PostBackOptions options, bool registerForEventValidation)
  106. {
  107. if (options == null)
  108. throw new ArgumentNullException ("options");
  109. if (registerForEventValidation)
  110. RegisterForEventValidation (options);
  111. return GetPostBackEventReference (options);
  112. }
  113. public string GetPostBackEventReference (PostBackOptions options)
  114. {
  115. if (options == null)
  116. throw new ArgumentNullException ("options");
  117. if (options.ActionUrl == null && options.ValidationGroup == null && !options.TrackFocus &&
  118. !options.AutoPostBack && !options.PerformValidation)
  119. {
  120. if (!options.ClientSubmit)
  121. return null;
  122. if (options.RequiresJavaScriptProtocol)
  123. return GetPostBackClientHyperlink (options.TargetControl, options.Argument);
  124. else
  125. return GetPostBackEventReference (options.TargetControl, options.Argument);
  126. }
  127. RegisterWebFormClientScript ();
  128. if (options.ActionUrl != null)
  129. RegisterHiddenField (Page.PreviousPageID, page.Request.FilePath);
  130. if (options.ClientSubmit || options.ActionUrl != null)
  131. page.RequiresPostBackScript ();
  132. return String.Format ("{0}WebForm_DoPostback({1},{2},{3},{4},{5},{6},{7},{8})",
  133. options.RequiresJavaScriptProtocol ? "javascript:" : "",
  134. ClientScriptManager.GetScriptLiteral (options.TargetControl.UniqueID),
  135. ClientScriptManager.GetScriptLiteral (options.Argument),
  136. ClientScriptManager.GetScriptLiteral (options.ActionUrl),
  137. ClientScriptManager.GetScriptLiteral (options.AutoPostBack),
  138. ClientScriptManager.GetScriptLiteral (options.PerformValidation),
  139. ClientScriptManager.GetScriptLiteral (options.TrackFocus),
  140. ClientScriptManager.GetScriptLiteral (options.ClientSubmit),
  141. ClientScriptManager.GetScriptLiteral (options.ValidationGroup)
  142. );
  143. }
  144. internal void RegisterWebFormClientScript ()
  145. {
  146. if (!IsClientScriptIncludeRegistered (typeof (Page), "webform"))
  147. RegisterClientScriptInclude (typeof (Page), "webform", GetWebResourceUrl (typeof (Page), "webform.js"));
  148. }
  149. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context)
  150. {
  151. return GetCallbackEventReference (control, argument, clientCallback, context, null, false);
  152. }
  153. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, bool useAsync)
  154. {
  155. return GetCallbackEventReference (control, argument, clientCallback, context, null, useAsync);
  156. }
  157. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
  158. {
  159. if (control == null)
  160. throw new ArgumentNullException ("control");
  161. if(!(control is ICallbackEventHandler))
  162. throw new InvalidOperationException ("The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent method.");
  163. return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, clientErrorCallback, useAsync);
  164. }
  165. public string GetCallbackEventReference (string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
  166. {
  167. page.RequiresPostBackScript ();
  168. RegisterWebFormClientScript ();
  169. return string.Format ("WebForm_DoCallback('{0}',{1},{2},{3},{4},{5})", target, argument, clientCallback, context, ((clientErrorCallback == null) ? "null" : clientErrorCallback), (useAsync ? "true" : "false"));
  170. }
  171. #endif
  172. #if NET_2_0
  173. public
  174. #else
  175. internal
  176. #endif
  177. string GetWebResourceUrl(Type type, string resourceName)
  178. {
  179. if (type == null)
  180. throw new ArgumentNullException ("type");
  181. if (resourceName == null || resourceName.Length == 0)
  182. throw new ArgumentNullException ("type");
  183. return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
  184. }
  185. public bool IsClientScriptBlockRegistered (string key)
  186. {
  187. return IsScriptRegistered (clientScriptBlocks, GetType(), key);
  188. }
  189. public bool IsClientScriptBlockRegistered (Type type, string key)
  190. {
  191. return IsScriptRegistered (clientScriptBlocks, type, key);
  192. }
  193. public bool IsStartupScriptRegistered (string key)
  194. {
  195. return IsScriptRegistered (startupScriptBlocks, GetType(), key);
  196. }
  197. public bool IsStartupScriptRegistered (Type type, string key)
  198. {
  199. return IsScriptRegistered (startupScriptBlocks, type, key);
  200. }
  201. public bool IsOnSubmitStatementRegistered (string key)
  202. {
  203. return IsScriptRegistered (submitStatements, GetType(), key);
  204. }
  205. public bool IsOnSubmitStatementRegistered (Type type, string key)
  206. {
  207. return IsScriptRegistered (submitStatements, type, key);
  208. }
  209. public bool IsClientScriptIncludeRegistered (string key)
  210. {
  211. return IsScriptRegistered (scriptIncludes, GetType(), key);
  212. }
  213. public bool IsClientScriptIncludeRegistered (Type type, string key)
  214. {
  215. return IsScriptRegistered (scriptIncludes, type, key);
  216. }
  217. bool IsScriptRegistered (ScriptEntry scriptList, Type type, string key)
  218. {
  219. while (scriptList != null) {
  220. if (scriptList.Type == type && scriptList.Key == key)
  221. return true;
  222. scriptList = scriptList.Next;
  223. }
  224. return false;
  225. }
  226. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  227. {
  228. if (registeredArrayDeclares == null)
  229. registeredArrayDeclares = new Hashtable();
  230. if (!registeredArrayDeclares.ContainsKey (arrayName))
  231. registeredArrayDeclares.Add (arrayName, new ArrayList());
  232. ((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
  233. page.RequiresPostBackScript ();
  234. }
  235. void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, bool addScriptTags)
  236. {
  237. ScriptEntry last = null;
  238. ScriptEntry entry = scriptList;
  239. while (entry != null) {
  240. if (entry.Type == type && entry.Key == key)
  241. return;
  242. last = entry;
  243. entry = entry.Next;
  244. }
  245. if (addScriptTags)
  246. script = "<script language=javascript>\n<!--\n" + script + "\n// -->\n</script>";
  247. entry = new ScriptEntry (type, key, script);
  248. if (last != null) last.Next = entry;
  249. else scriptList = entry;
  250. }
  251. internal void RegisterClientScriptBlock (string key, string script)
  252. {
  253. RegisterScript (ref clientScriptBlocks, GetType(), key, script, false);
  254. }
  255. public void RegisterClientScriptBlock (Type type, string key, string script)
  256. {
  257. RegisterClientScriptBlock (type, key, script, false);
  258. }
  259. public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags)
  260. {
  261. if (type == null)
  262. throw new ArgumentNullException ("type");
  263. RegisterScript (ref clientScriptBlocks, type, key, script, addScriptTags);
  264. }
  265. public void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  266. {
  267. if (hiddenFields == null)
  268. hiddenFields = new Hashtable ();
  269. if (!hiddenFields.ContainsKey (hiddenFieldName))
  270. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  271. }
  272. internal void RegisterOnSubmitStatement (string key, string script)
  273. {
  274. RegisterScript (ref submitStatements, GetType (), key, script, false);
  275. }
  276. public void RegisterOnSubmitStatement (Type type, string key, string script)
  277. {
  278. if (type == null)
  279. throw new ArgumentNullException ("type");
  280. RegisterScript (ref submitStatements, type, key, script, false);
  281. }
  282. internal void RegisterStartupScript (string key, string script)
  283. {
  284. RegisterScript (ref startupScriptBlocks, GetType(), key, script, false);
  285. }
  286. public void RegisterStartupScript (Type type, string key, string script)
  287. {
  288. RegisterStartupScript (type, key, script, false);
  289. }
  290. public void RegisterStartupScript (Type type, string key, string script, bool addScriptTags)
  291. {
  292. if (type == null)
  293. throw new ArgumentNullException ("type");
  294. RegisterScript (ref startupScriptBlocks, type, key, script, addScriptTags);
  295. }
  296. public void RegisterClientScriptInclude (string key, string url)
  297. {
  298. RegisterClientScriptInclude (GetType (), key, url);
  299. }
  300. public void RegisterClientScriptInclude (Type type, string key, string url)
  301. {
  302. if (type == null)
  303. throw new ArgumentNullException ("type");
  304. if (url == null || url.Length == 0)
  305. throw new ArgumentException ("url");
  306. RegisterScript (ref scriptIncludes, type, key, url, false);
  307. }
  308. #if NET_2_0
  309. public void RegisterClientScriptResource (Type type, string resourceName)
  310. {
  311. RegisterScript (ref scriptIncludes, type, "resource-" + resourceName, GetWebResourceUrl (type, resourceName), false);
  312. }
  313. public void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue)
  314. {
  315. RegisterExpandoAttribute (controlId, attributeName, attributeValue, true);
  316. }
  317. public void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode)
  318. {
  319. if (controlId == null)
  320. throw new ArgumentNullException ("controlId");
  321. if (attributeName == null)
  322. throw new ArgumentNullException ("attributeName");
  323. if (expandoAttributes == null)
  324. expandoAttributes = new Hashtable ();
  325. ListDictionary list = (ListDictionary)expandoAttributes [controlId];
  326. if (list == null) {
  327. list = new ListDictionary ();
  328. expandoAttributes [controlId] = list;
  329. }
  330. list.Add (attributeName, encode ? StrUtils.EscapeQuotesAndBackslashes (attributeValue) : attributeValue);
  331. }
  332. // Implemented following the description in http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx
  333. private int CalculateEventHash (string uniqueId, string argument)
  334. {
  335. int uniqueIdHash = uniqueId.GetHashCode ();
  336. int argumentHash = String.IsNullOrEmpty (argument) ? 0 : argument.GetHashCode ();
  337. return (uniqueIdHash ^ argumentHash);
  338. }
  339. public void RegisterForEventValidation (PostBackOptions options)
  340. {
  341. // MS.NET does not check for options == null, so we won't too...
  342. RegisterForEventValidation (options.TargetControl.UniqueID, options.Argument);
  343. }
  344. public void RegisterForEventValidation (string uniqueId)
  345. {
  346. RegisterForEventValidation (uniqueId, null);
  347. }
  348. public void RegisterForEventValidation (string uniqueId, string argument)
  349. {
  350. if (!page.EnableEventValidation)
  351. return;
  352. if (uniqueId == null || uniqueId.Length == 0)
  353. return;
  354. if (page.IsCallback)
  355. _hasRegisteredForEventValidationOnCallback = true;
  356. else if (page.LifeCycle < PageLifeCycle.Render)
  357. throw new InvalidOperationException ("RegisterForEventValidation may only be called from the Render method");
  358. if (eventValidationValues == null)
  359. eventValidationValues = new List <int> ();
  360. int hash = CalculateEventHash (uniqueId, argument);
  361. if (eventValidationValues.BinarySearch (hash) < 0)
  362. eventValidationValues.Add (hash);
  363. }
  364. public void ValidateEvent (string uniqueId)
  365. {
  366. ValidateEvent (uniqueId, null);
  367. }
  368. public void ValidateEvent (string uniqueId, string argument)
  369. {
  370. if (uniqueId == null || uniqueId.Length == 0)
  371. throw new ArgumentException ("must not be null or empty", "uniqueId");
  372. if (!page.EnableEventValidation)
  373. return;
  374. if (eventValidationValues == null)
  375. goto bad;
  376. int hash = CalculateEventHash (uniqueId, argument);
  377. if (eventValidationValues.BinarySearch (hash) < 0)
  378. goto bad;
  379. return;
  380. bad:
  381. 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.");
  382. }
  383. #endif
  384. void WriteScripts (HtmlTextWriter writer, ScriptEntry scriptList)
  385. {
  386. while (scriptList != null) {
  387. writer.WriteLine (scriptList.Script);
  388. scriptList = scriptList.Next;
  389. }
  390. }
  391. #if NET_2_0
  392. internal void RestoreEventValidationState (string fieldValue)
  393. {
  394. if (!page.EnableEventValidation || fieldValue == null || fieldValue.Length == 0)
  395. return;
  396. IStateFormatter fmt = page.GetFormatter ();
  397. int [] eventValues = (int []) fmt.Deserialize (fieldValue);
  398. #if TARGET_JVM // FIXME: No support yet for passing 'int[]' as 'T[]'
  399. eventValidationValues = new List<int> (eventValues.Length);
  400. for (int i = 0; i < eventValues.Length; i++)
  401. eventValidationValues.Add(eventValues[i]);
  402. #else
  403. eventValidationValues = new List<int> (eventValues);
  404. #endif
  405. eventValidationValues.Sort ();
  406. }
  407. internal void SaveEventValidationState ()
  408. {
  409. if (!page.EnableEventValidation)
  410. return;
  411. string eventValidation = GetEventValidationStateFormatted ();
  412. if (eventValidation == null)
  413. return;
  414. RegisterHiddenField (EventStateFieldName, eventValidation);
  415. }
  416. internal string GetEventValidationStateFormatted ()
  417. {
  418. if (eventValidationValues == null || eventValidationValues.Count == 0)
  419. return null;
  420. if(page.IsCallback && !_hasRegisteredForEventValidationOnCallback)
  421. return null;
  422. IStateFormatter fmt = page.GetFormatter ();
  423. int [] array = new int [eventValidationValues.Count];
  424. #if TARGET_JVM // FIXME: No support yet for passing 'int[]' as 'T[]'
  425. ((ICollection)eventValidationValues).CopyTo (array, 0);
  426. #else
  427. eventValidationValues.CopyTo (array);
  428. #endif
  429. return fmt.Serialize (array);
  430. }
  431. internal string EventStateFieldName
  432. {
  433. get { return "__EVENTVALIDATION"; }
  434. }
  435. internal void WriteExpandoAttributes (HtmlTextWriter writer)
  436. {
  437. if (expandoAttributes == null)
  438. return;
  439. writer.WriteLine ();
  440. writer.WriteLine ("<script type=\"text/javascript\">");
  441. writer.WriteLine ("<!--");
  442. foreach (string controlId in expandoAttributes.Keys) {
  443. writer.WriteLine ("var {0} = document.all ? document.all [\"{0}\"] : document.getElementById (\"{0}\");", controlId);
  444. ListDictionary attrs = (ListDictionary) expandoAttributes [controlId];
  445. foreach (string attributeName in attrs.Keys) {
  446. writer.WriteLine ("{0}.{1} = \"{2}\";", controlId, attributeName, attrs [attributeName]);
  447. }
  448. }
  449. writer.WriteLine ("// -->");
  450. writer.WriteLine ("</script>");
  451. writer.WriteLine ();
  452. }
  453. #endif
  454. internal void WriteHiddenFields (HtmlTextWriter writer)
  455. {
  456. if (hiddenFields == null)
  457. return;
  458. foreach (string key in hiddenFields.Keys) {
  459. string value = hiddenFields [key] as string;
  460. writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", key, value);
  461. }
  462. hiddenFields = null;
  463. }
  464. internal void WriteClientScriptIncludes (HtmlTextWriter writer)
  465. {
  466. ScriptEntry entry = scriptIncludes;
  467. while (entry != null) {
  468. if (!entry.Rendered) {
  469. #if TARGET_J2EE
  470. if (!page.IsPortletRender)
  471. #endif
  472. writer.WriteLine ("\n<script src=\"{0}\" type=\"text/javascript\"></script>", entry.Script);
  473. #if TARGET_J2EE
  474. else {
  475. string scriptKey = "inc_" + entry.Key.GetHashCode ().ToString ("X");
  476. writer.WriteLine ("\n<script type=\"text/javascript\">");
  477. writer.WriteLine ("<!--");
  478. writer.WriteLine ("if (document.{0} == null) {{", scriptKey);
  479. writer.WriteLine ("\tdocument.{0} = true", scriptKey);
  480. writer.WriteLine ("\tdocument.write('<script src=\"{0}\" type=\"text/javascript\"><\\/script>'); }}", entry.Script);
  481. writer.WriteLine ("// -->");
  482. writer.WriteLine ("</script>");
  483. }
  484. #endif
  485. entry.Rendered = true;
  486. }
  487. entry = entry.Next;
  488. }
  489. }
  490. internal void WriteClientScriptBlocks (HtmlTextWriter writer)
  491. {
  492. WriteScripts (writer, clientScriptBlocks);
  493. }
  494. internal void WriteStartupScriptBlocks (HtmlTextWriter writer)
  495. {
  496. WriteScripts (writer, startupScriptBlocks);
  497. }
  498. internal void WriteArrayDeclares (HtmlTextWriter writer)
  499. {
  500. if (registeredArrayDeclares != null) {
  501. writer.WriteLine();
  502. writer.WriteLine("<script language=\"javascript\">");
  503. writer.WriteLine("<!--");
  504. IDictionaryEnumerator arrayEnum = registeredArrayDeclares.GetEnumerator();
  505. while (arrayEnum.MoveNext()) {
  506. writer.Write("\tvar ");
  507. writer.Write(arrayEnum.Key);
  508. writer.Write(" = new Array(");
  509. IEnumerator arrayListEnum = ((ArrayList) arrayEnum.Value).GetEnumerator();
  510. bool isFirst = true;
  511. while (arrayListEnum.MoveNext()) {
  512. if (isFirst)
  513. isFirst = false;
  514. else
  515. writer.Write(", ");
  516. writer.Write(arrayListEnum.Current);
  517. }
  518. writer.WriteLine(");");
  519. #if TARGET_J2EE
  520. // in addition, add a form array declaration
  521. if (page.IsPortletRender) {
  522. writer.Write ("\t" + page.theForm + ".");
  523. writer.Write (arrayEnum.Key);
  524. writer.Write (" = ");
  525. writer.Write (arrayEnum.Key);
  526. writer.WriteLine (";");
  527. }
  528. #endif
  529. }
  530. writer.WriteLine("// -->");
  531. writer.WriteLine("</script>");
  532. writer.WriteLine();
  533. }
  534. }
  535. #if NET_2_0
  536. internal string GetClientValidationEvent (string validationGroup) {
  537. string eventScript = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate('" + validationGroup + "');";
  538. #if TARGET_J2EE
  539. if (page.IsPortletRender)
  540. return "if (typeof(SetValidatorContext) == 'function') SetValidatorContext ('" + page.theForm + "'); " + eventScript;
  541. #endif
  542. return eventScript;
  543. }
  544. #endif
  545. internal string GetClientValidationEvent ()
  546. {
  547. string eventScript = "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
  548. #if TARGET_J2EE
  549. if (page.IsPortletRender)
  550. return "if (typeof(SetValidatorContext) == 'function') SetValidatorContext ('" + page.theForm + "'); " + eventScript;
  551. #endif
  552. return eventScript;
  553. }
  554. internal string WriteSubmitStatements ()
  555. {
  556. if (submitStatements == null) return null;
  557. StringBuilder sb = new StringBuilder ();
  558. ScriptEntry entry = submitStatements;
  559. while (entry != null) {
  560. sb.Append (entry.Script);
  561. entry = entry.Next;
  562. }
  563. return sb.ToString ();
  564. }
  565. internal static string GetScriptLiteral (object ob)
  566. {
  567. if (ob == null)
  568. return "null";
  569. else if (ob is string) {
  570. string s = (string)ob;
  571. s = s.Replace ("\"", "\\\"");
  572. return "\"" + s + "\"";
  573. } else if (ob is bool) {
  574. return ob.ToString().ToLower();
  575. } else {
  576. return ob.ToString ();
  577. }
  578. }
  579. class ScriptEntry
  580. {
  581. public Type Type;
  582. public string Key;
  583. public string Script;
  584. public ScriptEntry Next;
  585. public bool Rendered;
  586. public ScriptEntry (Type type, string key, string script)
  587. {
  588. Key = key;
  589. Type = type;
  590. Script = script;
  591. }
  592. }
  593. #if NET_2_0
  594. // helper method
  595. internal static string EnsureEndsWithSemicolon (string value) {
  596. if (value != null && value.Length > 0 && !value.EndsWith (";"))
  597. return value += ";";
  598. return value;
  599. }
  600. #endif
  601. }
  602. }