ClientScriptManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. using System.Text;
  36. namespace System.Web.UI
  37. {
  38. #if NET_2_0
  39. public sealed
  40. #else
  41. internal
  42. #endif
  43. class ClientScriptManager
  44. {
  45. Hashtable registeredArrayDeclares;
  46. ScriptEntry clientScriptBlocks;
  47. ScriptEntry startupScriptBlocks;
  48. Hashtable hiddenFields;
  49. ScriptEntry submitStatements;
  50. ScriptEntry scriptIncludes;
  51. Page page;
  52. internal ClientScriptManager (Page page)
  53. {
  54. this.page = page;
  55. }
  56. #if !NET_2_0
  57. public string GetPostBackClientEvent (Control control, string argument)
  58. {
  59. return GetPostBackEventReference (control, argument);
  60. }
  61. #endif
  62. public string GetPostBackClientHyperlink (Control control, string argument)
  63. {
  64. return "javascript:" + GetPostBackEventReference (control, argument);
  65. }
  66. public string GetPostBackEventReference (Control control, string argument)
  67. {
  68. page.RequiresPostBackScript ();
  69. return String.Format ("__doPostBack('{0}','{1}')", control.UniqueID, argument);
  70. }
  71. #if NET_2_0
  72. public string GetPostBackEventReference (PostBackOptions options)
  73. {
  74. if (options.ActionUrl == null && options.ValidationGroup == null && !options.TrackFocus &&
  75. !options.AutoPostBack && !options.PerformValidation)
  76. {
  77. if (!options.ClientSubmit)
  78. return null;
  79. if (options.RequiresJavaScriptProtocol)
  80. return GetPostBackClientHyperlink (options.TargetControl, options.Argument);
  81. else
  82. return GetPostBackEventReference (options.TargetControl, options.Argument);
  83. }
  84. if (!IsClientScriptIncludeRegistered (typeof(Page), "webform")) {
  85. RegisterClientScriptInclude (typeof(Page), "webform", GetWebResourceUrl (typeof(Page), "webform.js"));
  86. }
  87. if (options.ActionUrl != null)
  88. RegisterHiddenField (Page.PreviousPageID, page.Request.FilePath);
  89. if (options.ClientSubmit || options.ActionUrl != null)
  90. page.RequiresPostBackScript ();
  91. return String.Format ("{0}WebForm_DoPostback({1},{2},{3},{4},{5},{6},{7},{8})",
  92. options.RequiresJavaScriptProtocol ? "javascript:" : "",
  93. ClientScriptManager.GetScriptLiteral (options.TargetControl.UniqueID),
  94. ClientScriptManager.GetScriptLiteral (options.Argument),
  95. ClientScriptManager.GetScriptLiteral (options.ActionUrl),
  96. ClientScriptManager.GetScriptLiteral (options.AutoPostBack),
  97. ClientScriptManager.GetScriptLiteral (options.PerformValidation),
  98. ClientScriptManager.GetScriptLiteral (options.TrackFocus),
  99. ClientScriptManager.GetScriptLiteral (options.ClientSubmit),
  100. ClientScriptManager.GetScriptLiteral (options.ValidationGroup)
  101. );
  102. }
  103. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context)
  104. {
  105. return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, null, false);
  106. }
  107. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, bool useAsync)
  108. {
  109. return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, null, useAsync);
  110. }
  111. public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
  112. {
  113. return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, clientErrorCallback, useAsync);
  114. }
  115. public string GetCallbackEventReference (string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
  116. {
  117. page.RequiresPostBackScript ();
  118. if (!IsClientScriptIncludeRegistered (typeof(Page), "callback"))
  119. RegisterClientScriptInclude (typeof(Page), "callback", GetWebResourceUrl (typeof(Page), "callback.js"));
  120. return string.Format ("WebForm_DoCallback ('{0}', {1}, {2}, {3}, {4})", target, argument, clientCallback, context, clientErrorCallback);
  121. }
  122. #endif
  123. #if NET_2_0
  124. public
  125. #else
  126. internal
  127. #endif
  128. string GetWebResourceUrl(Type type, string resourceName)
  129. {
  130. if (type == null)
  131. throw new ArgumentNullException ("type");
  132. if (resourceName == null || resourceName.Length == 0)
  133. throw new ArgumentNullException ("type");
  134. return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
  135. }
  136. public bool IsClientScriptBlockRegistered (string key)
  137. {
  138. return IsScriptRegistered (clientScriptBlocks, GetType(), key);
  139. }
  140. public bool IsClientScriptBlockRegistered (Type type, string key)
  141. {
  142. return IsScriptRegistered (clientScriptBlocks, type, key);
  143. }
  144. public bool IsStartupScriptRegistered (string key)
  145. {
  146. return IsScriptRegistered (startupScriptBlocks, GetType(), key);
  147. }
  148. public bool IsStartupScriptRegistered (Type type, string key)
  149. {
  150. return IsScriptRegistered (startupScriptBlocks, type, key);
  151. }
  152. public bool IsOnSubmitStatementRegistered (string key)
  153. {
  154. return IsScriptRegistered (submitStatements, GetType(), key);
  155. }
  156. public bool IsOnSubmitStatementRegistered (Type type, string key)
  157. {
  158. return IsScriptRegistered (submitStatements, type, key);
  159. }
  160. public bool IsClientScriptIncludeRegistered (string key)
  161. {
  162. return IsScriptRegistered (scriptIncludes, GetType(), key);
  163. }
  164. public bool IsClientScriptIncludeRegistered (Type type, string key)
  165. {
  166. return IsScriptRegistered (scriptIncludes, type, key);
  167. }
  168. bool IsScriptRegistered (ScriptEntry scriptList, Type type, string key)
  169. {
  170. while (scriptList != null) {
  171. if (scriptList.Type == type && scriptList.Key == key)
  172. return true;
  173. scriptList = scriptList.Next;
  174. }
  175. return false;
  176. }
  177. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  178. {
  179. if (registeredArrayDeclares == null)
  180. registeredArrayDeclares = new Hashtable();
  181. if (!registeredArrayDeclares.ContainsKey (arrayName))
  182. registeredArrayDeclares.Add (arrayName, new ArrayList());
  183. ((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
  184. }
  185. void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, bool addScriptTags)
  186. {
  187. ScriptEntry last = null;
  188. ScriptEntry entry = scriptList;
  189. while (entry != null) {
  190. if (entry.Type == type && entry.Key == key)
  191. return;
  192. last = entry;
  193. entry = entry.Next;
  194. }
  195. if (addScriptTags)
  196. script = "<script language=javascript>\n<!--\n" + script + "\n// -->\n</script>";
  197. entry = new ScriptEntry (type, key, script);
  198. if (last != null) last.Next = entry;
  199. else scriptList = entry;
  200. }
  201. internal void RegisterClientScriptBlock (string key, string script)
  202. {
  203. RegisterScript (ref clientScriptBlocks, GetType(), key, script, false);
  204. }
  205. public void RegisterClientScriptBlock (Type type, string key, string script)
  206. {
  207. RegisterScript (ref clientScriptBlocks, type, key, script, false);
  208. }
  209. public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags)
  210. {
  211. RegisterScript (ref clientScriptBlocks, type, key, script, addScriptTags);
  212. }
  213. public void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  214. {
  215. if (hiddenFields == null)
  216. hiddenFields = new Hashtable ();
  217. if (!hiddenFields.ContainsKey (hiddenFieldName))
  218. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  219. }
  220. internal void RegisterOnSubmitStatement (string key, string script)
  221. {
  222. RegisterScript (ref submitStatements, GetType (), key, script, false);
  223. }
  224. public void RegisterOnSubmitStatement (Type type, string key, string script)
  225. {
  226. RegisterScript (ref submitStatements, type, key, script, false);
  227. }
  228. internal void RegisterStartupScript (string key, string script)
  229. {
  230. RegisterScript (ref startupScriptBlocks, GetType(), key, script, false);
  231. }
  232. public void RegisterStartupScript (Type type, string key, string script)
  233. {
  234. RegisterScript (ref startupScriptBlocks, type, key, script, false);
  235. }
  236. public void RegisterStartupScript (Type type, string key, string script, bool addScriptTags)
  237. {
  238. RegisterScript (ref startupScriptBlocks, type, key, script, addScriptTags);
  239. }
  240. public void RegisterClientScriptInclude (string key, string url)
  241. {
  242. RegisterScript (ref scriptIncludes, GetType(), key, url, false);
  243. }
  244. public void RegisterClientScriptInclude (Type type, string key, string url)
  245. {
  246. RegisterScript (ref scriptIncludes, type, key, url, false);
  247. }
  248. #if NET_2_0
  249. public void RegisterClientScriptResource (Type type, string resourceName)
  250. {
  251. RegisterScript (ref scriptIncludes, type, "resource-" + resourceName, GetWebResourceUrl (type, resourceName), false);
  252. }
  253. [MonoTODO]
  254. public void RegisterExpandoAttribute (string controlId,
  255. string name,
  256. string value)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. [MonoTODO]
  261. public void RegisterExpandoAttribute(string controlId,
  262. string attributeName,
  263. string attributeValue,
  264. bool encode)
  265. {
  266. throw new NotImplementedException ();
  267. }
  268. #endif
  269. void WriteScripts (HtmlTextWriter writer, ScriptEntry scriptList)
  270. {
  271. while (scriptList != null) {
  272. writer.WriteLine (scriptList.Script);
  273. scriptList = scriptList.Next;
  274. }
  275. }
  276. internal void WriteHiddenFields (HtmlTextWriter writer)
  277. {
  278. if (hiddenFields == null)
  279. return;
  280. foreach (string key in hiddenFields.Keys) {
  281. string value = hiddenFields [key] as string;
  282. writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, value);
  283. }
  284. hiddenFields = null;
  285. }
  286. internal void WriteClientScriptIncludes (HtmlTextWriter writer)
  287. {
  288. ScriptEntry entry = scriptIncludes;
  289. while (entry != null) {
  290. writer.WriteLine ("\n<script src=\"{0}\" type=\"text/javascript\"></script>", entry.Script);
  291. entry = entry.Next;
  292. }
  293. }
  294. internal void WriteClientScriptBlocks (HtmlTextWriter writer)
  295. {
  296. WriteScripts (writer, clientScriptBlocks);
  297. }
  298. internal void WriteStartupScriptBlocks (HtmlTextWriter writer)
  299. {
  300. WriteScripts (writer, startupScriptBlocks);
  301. }
  302. internal void WriteArrayDeclares (HtmlTextWriter writer)
  303. {
  304. if (registeredArrayDeclares != null) {
  305. writer.WriteLine();
  306. writer.WriteLine("<script language=\"javascript\">");
  307. writer.WriteLine("<!--");
  308. IDictionaryEnumerator arrayEnum = registeredArrayDeclares.GetEnumerator();
  309. while (arrayEnum.MoveNext()) {
  310. writer.Write("\tvar ");
  311. writer.Write(arrayEnum.Key);
  312. writer.Write(" = new Array(");
  313. IEnumerator arrayListEnum = ((ArrayList) arrayEnum.Value).GetEnumerator();
  314. bool isFirst = true;
  315. while (arrayListEnum.MoveNext()) {
  316. if (isFirst)
  317. isFirst = false;
  318. else
  319. writer.Write(", ");
  320. writer.Write(arrayListEnum.Current);
  321. }
  322. writer.WriteLine(");");
  323. }
  324. writer.WriteLine("// -->");
  325. writer.WriteLine("</script>");
  326. writer.WriteLine();
  327. }
  328. }
  329. #if NET_2_0
  330. internal string GetClientValidationEvent (string validationGroup) {
  331. return "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate('" + validationGroup + "');";
  332. }
  333. #endif
  334. internal string GetClientValidationEvent ()
  335. {
  336. return "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
  337. }
  338. internal string WriteSubmitStatements ()
  339. {
  340. if (submitStatements == null) return null;
  341. StringBuilder sb = new StringBuilder ();
  342. ScriptEntry entry = submitStatements;
  343. while (entry != null) {
  344. sb.Append (entry.Script);
  345. entry = entry.Next;
  346. }
  347. return sb.ToString ();
  348. }
  349. internal static string GetScriptLiteral (object ob)
  350. {
  351. if (ob == null)
  352. return "null";
  353. else if (ob is string) {
  354. string s = (string)ob;
  355. s = s.Replace ("\"", "\\\"");
  356. return "\"" + s + "\"";
  357. } else if (ob is bool) {
  358. return ob.ToString().ToLower();
  359. } else {
  360. return ob.ToString ();
  361. }
  362. }
  363. class ScriptEntry
  364. {
  365. public Type Type;
  366. public string Key;
  367. public string Script;
  368. public ScriptEntry Next;
  369. public ScriptEntry (Type type, string key, string script)
  370. {
  371. Key = key;
  372. Type = type;
  373. Script = script;
  374. }
  375. }
  376. #if NET_2_0
  377. // helper method
  378. internal static string EnsureEndsWithSemicolon (string value) {
  379. if (value != null && value.Length > 0 && !value.EndsWith (";"))
  380. return value += ";";
  381. return value;
  382. }
  383. #endif
  384. }
  385. }