HtmlInputImage.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. //
  22. // System.Web.UI.HtmlControls.HtmlInputImage.cs
  23. //
  24. // Authors:
  25. // Jackson Harper ([email protected])
  26. //
  27. // (C) 2005 Novell, Inc.
  28. //
  29. // TODO: getting the .x and .y in LoadData doesn't work with mozilla
  30. //
  31. using System.Globalization;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.Security.Permissions;
  35. namespace System.Web.UI.HtmlControls {
  36. // CAS
  37. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. // attributes
  40. [DefaultEvent("ServerClick")]
  41. #if NET_2_0
  42. [SupportsEventValidation]
  43. #endif
  44. public class HtmlInputImage : HtmlInputControl, IPostBackDataHandler,
  45. IPostBackEventHandler {
  46. private static readonly object ServerClickEvent = new object ();
  47. private int clicked_x;
  48. private int clicked_y;
  49. public HtmlInputImage () : base ("image")
  50. {
  51. }
  52. [DefaultValue(true)]
  53. [WebSysDescription("")]
  54. [WebCategory("Behavior")]
  55. #if NET_2_0
  56. public virtual
  57. #else
  58. public
  59. #endif
  60. bool CausesValidation {
  61. get {
  62. return ViewState.GetBool ("CausesValidation", true);
  63. }
  64. set {
  65. ViewState ["CausesValidation"] = value;
  66. }
  67. }
  68. [DefaultValue("")]
  69. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  70. [WebSysDescription("")]
  71. [WebCategory("Appearance")]
  72. public string Align {
  73. get { return GetAtt ("align"); }
  74. set { SetAtt ("align", value); }
  75. }
  76. [DefaultValue("")]
  77. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  78. #if NET_2_0
  79. [Localizable (true)]
  80. #endif
  81. [WebSysDescription("")]
  82. [WebCategory("Appearance")]
  83. public string Alt {
  84. get { return GetAtt ("alt"); }
  85. set { SetAtt ("alt", value); }
  86. }
  87. [DefaultValue("")]
  88. [WebSysDescription("")]
  89. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  90. [WebCategory("Appearance")]
  91. #if NET_2_0
  92. [UrlProperty]
  93. #endif
  94. public string Src {
  95. get { return GetAtt ("src"); }
  96. set { SetAtt ("src", value); }
  97. }
  98. #if NET_2_0
  99. [DefaultValue("-1")]
  100. #else
  101. [DefaultValue("")]
  102. #endif
  103. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  104. [WebSysDescription("")]
  105. [WebCategory("Appearance")]
  106. public int Border {
  107. get {
  108. string border = Attributes ["border"];
  109. if (border == null)
  110. return -1;
  111. return Int32.Parse (border, CultureInfo.InvariantCulture);
  112. }
  113. set {
  114. if (value == -1) {
  115. Attributes.Remove ("border");
  116. return;
  117. }
  118. Attributes ["border"] = value.ToString (CultureInfo.InvariantCulture);
  119. }
  120. }
  121. bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
  122. {
  123. string x = postCollection [UniqueID + ".x"];
  124. string y = postCollection [UniqueID + ".y"];
  125. if (x != null && x.Length != 0 &&
  126. y != null && y.Length != 0) {
  127. clicked_x = Int32.Parse (x, CultureInfo.InvariantCulture);
  128. clicked_y = Int32.Parse (y, CultureInfo.InvariantCulture);
  129. Page.RegisterRequiresRaiseEvent (this);
  130. return true;
  131. }
  132. return false;
  133. }
  134. void RaisePostBackEventInternal (string eventArgument)
  135. {
  136. if (CausesValidation)
  137. #if NET_2_0
  138. Page.Validate (ValidationGroup);
  139. #else
  140. Page.Validate ();
  141. #endif
  142. OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
  143. }
  144. void RaisePostDataChangedEventInternal ()
  145. {
  146. /* no events to raise */
  147. }
  148. #if NET_2_0
  149. [DefaultValue ("")]
  150. public virtual string ValidationGroup
  151. {
  152. get {
  153. return ViewState.GetString ("ValidationGroup", "");
  154. }
  155. set {
  156. ViewState ["ValidationGroup"] = value;
  157. }
  158. }
  159. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  160. {
  161. return LoadPostDataInternal (postDataKey, postCollection);
  162. }
  163. protected virtual void RaisePostBackEvent (string eventArgument)
  164. {
  165. RaisePostBackEventInternal (eventArgument);
  166. }
  167. protected virtual void RaisePostDataChangedEvent ()
  168. {
  169. RaisePostDataChangedEventInternal ();
  170. }
  171. #endif
  172. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  173. NameValueCollection postCollection)
  174. {
  175. #if NET_2_0
  176. return LoadPostData (postDataKey, postCollection);
  177. #else
  178. return LoadPostDataInternal (postDataKey, postCollection);
  179. #endif
  180. }
  181. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  182. {
  183. #if NET_2_0
  184. RaisePostDataChangedEvent();
  185. #else
  186. RaisePostDataChangedEventInternal ();
  187. #endif
  188. }
  189. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  190. {
  191. #if NET_2_0
  192. RaisePostBackEvent (eventArgument);
  193. #else
  194. RaisePostBackEventInternal (eventArgument);
  195. #endif
  196. }
  197. #if NET_2_0
  198. protected internal
  199. #else
  200. protected
  201. #endif
  202. override void OnPreRender (EventArgs e)
  203. {
  204. base.OnPreRender (e);
  205. if (Page != null) {
  206. Page.RegisterRequiresPostBack (this);
  207. }
  208. }
  209. protected virtual void OnServerClick (ImageClickEventArgs e)
  210. {
  211. EventHandler handler = Events [ServerClickEvent] as EventHandler;
  212. if (handler != null)
  213. handler (this, e);
  214. }
  215. protected override void RenderAttributes (HtmlTextWriter writer)
  216. {
  217. #if NET_2_0
  218. if (Page != null)
  219. Page.ClientScript.RegisterForEventValidation (this.UniqueID);
  220. if (CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup)) {
  221. ClientScriptManager csm = Page.ClientScript;
  222. Attributes ["onclick"] += csm.GetClientValidationEvent (ValidationGroup);
  223. }
  224. #else
  225. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  226. ClientScriptManager csm = new ClientScriptManager (Page);
  227. writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
  228. }
  229. #endif
  230. base.RenderAttributes (writer);
  231. }
  232. private void SetAtt (string name, string value)
  233. {
  234. if ((value == null) || (value.Length == 0))
  235. Attributes.Remove (name);
  236. else
  237. Attributes [name] = value;
  238. }
  239. private string GetAtt (string name)
  240. {
  241. string res = Attributes [name];
  242. if (res == null)
  243. return String.Empty;
  244. return res;
  245. }
  246. [WebSysDescription("")]
  247. [WebCategory("Action")]
  248. public event ImageClickEventHandler ServerClick {
  249. add { Events.AddHandler (ServerClickEvent, value); }
  250. remove { Events.AddHandler (ServerClickEvent, value); }
  251. }
  252. }
  253. }