ClientScriptManager.cs 13 KB

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