2 // System.Web.UI.ClientScriptManager.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // Lluis Sanchez (lluis@novell.com)
10 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
11 // (c) 2003 Novell, Inc. (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
39 namespace System.Web.UI
46 class ClientScriptManager
48 Hashtable registeredArrayDeclares;
49 ScriptEntry clientScriptBlocks;
50 ScriptEntry startupScriptBlocks;
51 Hashtable hiddenFields;
52 ScriptEntry submitStatements;
53 ScriptEntry scriptIncludes;
56 internal ClientScriptManager (Page page)
61 [Obsolete ("Use GetPostBackEventReference instead")]
62 public string GetPostBackClientEvent (Control control, string argument)
64 return GetPostBackEventReference (control, argument);
67 public string GetPostBackClientHyperlink (Control control, string argument)
69 return "javascript:" + GetPostBackEventReference (control, argument);
72 public string GetPostBackEventReference (Control control, string argument)
74 page.RequiresPostBackScript ();
75 return String.Format ("__doPostBack('{0}','{1}')", control.UniqueID, argument);
79 public string GetPostBackEventReference (PostBackOptions options)
81 if (options.ActionUrl == null && options.ValidationGroup == null && !options.TrackFocus &&
82 !options.AutoPostBack && !options.PerformValidation)
84 if (options.RequiresJavaScriptProtocol)
85 return GetPostBackClientHyperlink (options.TargetControl, options.Argument);
87 return GetPostBackEventReference (options.TargetControl, options.Argument);
90 if (!IsClientScriptIncludeRegistered (typeof(Page), "webform")) {
91 RegisterClientScriptInclude (typeof(Page), "webform", GetWebResourceUrl (typeof(Page), "webform.js"));
94 if (options.ActionUrl != null)
95 RegisterHiddenField (Page.PreviousPageID, page.Request.FilePath);
97 if (options.ClientSubmit || options.ActionUrl != null)
98 page.RequiresPostBackScript ();
100 return String.Format ("{0}WebForm_DoPostback({1},{2},{3},{4},{5},{6},{7},{8})",
101 options.RequiresJavaScriptProtocol ? "javascript:" : "",
102 ClientScriptManager.GetScriptLiteral (options.TargetControl.UniqueID),
103 ClientScriptManager.GetScriptLiteral (options.Argument),
104 ClientScriptManager.GetScriptLiteral (options.ActionUrl),
105 ClientScriptManager.GetScriptLiteral (options.AutoPostBack),
106 ClientScriptManager.GetScriptLiteral (options.PerformValidation),
107 ClientScriptManager.GetScriptLiteral (options.TrackFocus),
108 ClientScriptManager.GetScriptLiteral (options.ClientSubmit),
109 ClientScriptManager.GetScriptLiteral (options.ValidationGroup)
113 public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context)
115 return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, null, false);
118 public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, bool useAsync)
120 return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, null, useAsync);
123 public string GetCallbackEventReference (Control control, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
125 return GetCallbackEventReference (control.UniqueID, argument, clientCallback, context, clientErrorCallback, useAsync);
128 public string GetCallbackEventReference (string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync)
130 page.RequiresPostBackScript ();
132 if (!IsClientScriptIncludeRegistered (typeof(Page), "callback"))
133 RegisterClientScriptInclude (typeof(Page), "callback", GetWebResourceUrl (typeof(Page), "callback.js"));
135 return string.Format ("WebForm_DoCallback ('{0}', {1}, {2}, {3}, {4})", target, argument, clientCallback, context, clientErrorCallback);
144 string GetWebResourceUrl(Type type, string resourceName)
147 throw new ArgumentNullException ("type");
149 if (resourceName == null || resourceName.Length == 0)
150 throw new ArgumentNullException ("type");
152 return System.Web.Handlers.AssemblyResourceLoader.GetResourceUrl (type, resourceName);
156 public bool IsClientScriptBlockRegistered (string key)
158 return IsScriptRegistered (clientScriptBlocks, GetType(), key);
161 public bool IsClientScriptBlockRegistered (Type type, string key)
163 return IsScriptRegistered (clientScriptBlocks, type, key);
166 public bool IsStartupScriptRegistered (string key)
168 return IsScriptRegistered (startupScriptBlocks, GetType(), key);
171 public bool IsStartupScriptRegistered (Type type, string key)
173 return IsScriptRegistered (startupScriptBlocks, type, key);
176 public bool IsOnSubmitStatementRegistered (string key)
178 return IsScriptRegistered (submitStatements, GetType(), key);
181 public bool IsOnSubmitStatementRegistered (Type type, string key)
183 return IsScriptRegistered (submitStatements, type, key);
186 public bool IsClientScriptIncludeRegistered (string key)
188 return IsScriptRegistered (scriptIncludes, GetType(), key);
191 public bool IsClientScriptIncludeRegistered (Type type, string key)
193 return IsScriptRegistered (scriptIncludes, type, key);
196 bool IsScriptRegistered (ScriptEntry scriptList, Type type, string key)
198 while (scriptList != null) {
199 if (scriptList.Type == type && scriptList.Key == key)
201 scriptList = scriptList.Next;
206 public void RegisterArrayDeclaration (string arrayName, string arrayValue)
208 if (registeredArrayDeclares == null)
209 registeredArrayDeclares = new Hashtable();
211 if (!registeredArrayDeclares.ContainsKey (arrayName))
212 registeredArrayDeclares.Add (arrayName, new ArrayList());
214 ((ArrayList) registeredArrayDeclares[arrayName]).Add(arrayValue);
217 void RegisterScript (ref ScriptEntry scriptList, Type type, string key, string script, bool addScriptTags)
219 ScriptEntry last = null;
220 ScriptEntry entry = scriptList;
222 while (entry != null) {
223 if (entry.Type == type && entry.Key == key)
230 script = "<script language=javascript>\n<!--\n" + script + "\n// -->\n</script>";
232 entry = new ScriptEntry (type, key, script);
234 if (last != null) last.Next = entry;
235 else scriptList = entry;
238 internal void RegisterClientScriptBlock (string key, string script)
240 RegisterScript (ref clientScriptBlocks, GetType(), key, script, false);
243 public void RegisterClientScriptBlock (Type type, string key, string script)
245 RegisterScript (ref clientScriptBlocks, type, key, script, false);
248 public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags)
250 RegisterScript (ref clientScriptBlocks, type, key, script, addScriptTags);
253 public void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
255 if (hiddenFields == null)
256 hiddenFields = new Hashtable ();
258 if (!hiddenFields.ContainsKey (hiddenFieldName))
259 hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
262 internal void RegisterOnSubmitStatement (string key, string script)
264 RegisterScript (ref submitStatements, GetType (), key, script, false);
267 public void RegisterOnSubmitStatement (Type type, string key, string script)
269 RegisterScript (ref submitStatements, type, key, script, false);
272 internal void RegisterStartupScript (string key, string script)
274 RegisterScript (ref startupScriptBlocks, GetType(), key, script, false);
277 public void RegisterStartupScript (Type type, string key, string script)
279 RegisterScript (ref startupScriptBlocks, type, key, script, false);
282 public void RegisterStartupScript (Type type, string key, string script, bool addScriptTags)
284 RegisterScript (ref startupScriptBlocks, type, key, script, addScriptTags);
287 public void RegisterClientScriptInclude (string key, string url)
289 RegisterScript (ref scriptIncludes, GetType(), key, url, false);
292 public void RegisterClientScriptInclude (Type type, string key, string url)
294 RegisterScript (ref scriptIncludes, type, key, url, false);
299 public void RegisterClientScriptResource (Type type, string resourceName)
301 throw new NotImplementedException ();
305 public void RegisterExpandoAttribute (string controlId,
309 throw new NotImplementedException ();
313 public void RegisterExpandoAttribute(string controlId,
314 string attributeName,
315 string attributeValue,
318 throw new NotImplementedException ();
321 void WriteScripts (HtmlTextWriter writer, ScriptEntry scriptList)
323 while (scriptList != null) {
324 writer.WriteLine (scriptList.Script);
325 scriptList = scriptList.Next;
329 internal void WriteHiddenFields (HtmlTextWriter writer)
331 if (hiddenFields == null)
334 foreach (string key in hiddenFields.Keys) {
335 string value = hiddenFields [key] as string;
336 writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, value);
342 internal void WriteClientScriptIncludes (HtmlTextWriter writer)
344 ScriptEntry entry = scriptIncludes;
345 while (entry != null) {
346 writer.WriteLine ("\n<script src=\"{0}\" type=\"text/javascript\"></script>", entry.Script);
351 internal void WriteClientScriptBlocks (HtmlTextWriter writer)
353 WriteScripts (writer, clientScriptBlocks);
356 internal void WriteStartupScriptBlocks (HtmlTextWriter writer)
358 WriteScripts (writer, startupScriptBlocks);
361 internal void WriteArrayDeclares (HtmlTextWriter writer)
363 if (registeredArrayDeclares != null) {
365 writer.WriteLine("<script language=\"javascript\">");
366 writer.WriteLine("<!--");
367 IDictionaryEnumerator arrayEnum = registeredArrayDeclares.GetEnumerator();
368 while (arrayEnum.MoveNext()) {
369 writer.Write("\tvar ");
370 writer.Write(arrayEnum.Key);
371 writer.Write(" = new Array(");
372 IEnumerator arrayListEnum = ((ArrayList) arrayEnum.Value).GetEnumerator();
374 while (arrayListEnum.MoveNext()) {
379 writer.Write(arrayListEnum.Current);
381 writer.WriteLine(");");
383 writer.WriteLine("// -->");
384 writer.WriteLine("</script>");
389 internal string GetClientValidationEvent ()
391 return "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
395 internal string WriteSubmitStatements ()
397 if (submitStatements == null) return null;
399 StringBuilder sb = new StringBuilder ();
400 ScriptEntry entry = submitStatements;
401 while (entry != null) {
402 sb.Append (entry.Script);
405 return sb.ToString ();
408 internal static string GetScriptLiteral (object ob)
412 else if (ob is string) {
413 string s = (string)ob;
414 s = s.Replace ("\"", "\\\"");
415 return "\"" + s + "\"";
416 } else if (ob is bool) {
417 return ob.ToString().ToLower();
419 return ob.ToString ();
427 public string Script;
428 public ScriptEntry Next;
430 public ScriptEntry (Type type, string key, string script)