HtmlInputImage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. using System.Web.Util;
  36. namespace System.Web.UI.HtmlControls {
  37. // CAS
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. // attributes
  41. [DefaultEvent("ServerClick")]
  42. #if NET_2_0
  43. [SupportsEventValidation]
  44. #endif
  45. public class HtmlInputImage : HtmlInputControl, IPostBackDataHandler, IPostBackEventHandler
  46. {
  47. static readonly object ServerClickEvent = new object ();
  48. int clicked_x;
  49. int clicked_y;
  50. public HtmlInputImage () : base ("image")
  51. {
  52. }
  53. [DefaultValue(true)]
  54. [WebSysDescription("")]
  55. [WebCategory("Behavior")]
  56. #if NET_2_0
  57. public virtual
  58. #else
  59. public
  60. #endif
  61. bool CausesValidation {
  62. get {
  63. return ViewState.GetBool ("CausesValidation", true);
  64. }
  65. set {
  66. ViewState ["CausesValidation"] = value;
  67. }
  68. }
  69. [DefaultValue("")]
  70. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  71. [WebSysDescription("")]
  72. [WebCategory("Appearance")]
  73. public string Align {
  74. get { return GetAtt ("align"); }
  75. set { SetAtt ("align", value); }
  76. }
  77. [DefaultValue("")]
  78. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  79. #if NET_2_0
  80. [Localizable (true)]
  81. #endif
  82. [WebSysDescription("")]
  83. [WebCategory("Appearance")]
  84. public string Alt {
  85. get { return GetAtt ("alt"); }
  86. set { SetAtt ("alt", value); }
  87. }
  88. [DefaultValue("")]
  89. [WebSysDescription("")]
  90. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  91. [WebCategory("Appearance")]
  92. #if NET_2_0
  93. [UrlProperty]
  94. #endif
  95. public string Src {
  96. get { return GetAtt ("src"); }
  97. set { SetAtt ("src", value); }
  98. }
  99. #if NET_2_0
  100. [DefaultValue("-1")]
  101. #else
  102. [DefaultValue("")]
  103. #endif
  104. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  105. [WebSysDescription("")]
  106. [WebCategory("Appearance")]
  107. public int Border {
  108. get {
  109. string border = Attributes ["border"];
  110. if (border == null)
  111. return -1;
  112. return Int32.Parse (border, Helpers.InvariantCulture);
  113. }
  114. set {
  115. if (value == -1) {
  116. Attributes.Remove ("border");
  117. return;
  118. }
  119. Attributes ["border"] = value.ToString (Helpers.InvariantCulture);
  120. }
  121. }
  122. bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
  123. {
  124. string x = postCollection [UniqueID + ".x"];
  125. string y = postCollection [UniqueID + ".y"];
  126. if (x != null && x.Length != 0 &&
  127. y != null && y.Length != 0) {
  128. clicked_x = Int32.Parse (x, Helpers.InvariantCulture);
  129. clicked_y = Int32.Parse (y, Helpers.InvariantCulture);
  130. Page.RegisterRequiresRaiseEvent (this);
  131. return true;
  132. }
  133. return false;
  134. }
  135. void RaisePostBackEventInternal (string eventArgument)
  136. {
  137. if (CausesValidation)
  138. #if NET_2_0
  139. Page.Validate (ValidationGroup);
  140. #else
  141. Page.Validate ();
  142. #endif
  143. OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
  144. }
  145. void RaisePostDataChangedEventInternal ()
  146. {
  147. /* no events to raise */
  148. }
  149. #if NET_2_0
  150. [DefaultValue ("")]
  151. public virtual string ValidationGroup
  152. {
  153. get {
  154. return ViewState.GetString ("ValidationGroup", "");
  155. }
  156. set {
  157. ViewState ["ValidationGroup"] = value;
  158. }
  159. }
  160. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  161. {
  162. return LoadPostDataInternal (postDataKey, postCollection);
  163. }
  164. protected virtual void RaisePostBackEvent (string eventArgument)
  165. {
  166. RaisePostBackEventInternal (eventArgument);
  167. }
  168. protected virtual void RaisePostDataChangedEvent ()
  169. {
  170. ValidateEvent (UniqueID, String.Empty);
  171. RaisePostDataChangedEventInternal ();
  172. }
  173. #endif
  174. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  175. NameValueCollection postCollection)
  176. {
  177. #if NET_2_0
  178. return LoadPostData (postDataKey, postCollection);
  179. #else
  180. return LoadPostDataInternal (postDataKey, postCollection);
  181. #endif
  182. }
  183. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  184. {
  185. #if NET_2_0
  186. RaisePostDataChangedEvent();
  187. #else
  188. RaisePostDataChangedEventInternal ();
  189. #endif
  190. }
  191. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  192. {
  193. #if NET_2_0
  194. RaisePostBackEvent (eventArgument);
  195. #else
  196. RaisePostBackEventInternal (eventArgument);
  197. #endif
  198. }
  199. #if NET_2_0
  200. protected internal
  201. #else
  202. protected
  203. #endif
  204. override void OnPreRender (EventArgs e)
  205. {
  206. base.OnPreRender (e);
  207. if (Page != null && !Disabled) {
  208. Page.RegisterRequiresPostBack (this);
  209. #if NET_2_0
  210. Page.RegisterEnabledControl (this);
  211. #endif
  212. }
  213. }
  214. protected virtual void OnServerClick (ImageClickEventArgs e)
  215. {
  216. ImageClickEventHandler handler = Events [ServerClickEvent] as ImageClickEventHandler;
  217. if (handler != null)
  218. handler (this, e);
  219. }
  220. protected override void RenderAttributes (HtmlTextWriter writer)
  221. {
  222. #if NET_2_0
  223. if (Page != null)
  224. Page.ClientScript.RegisterForEventValidation (UniqueID);
  225. if (CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup)) {
  226. ClientScriptManager csm = Page.ClientScript;
  227. Attributes ["onclick"] += csm.GetClientValidationEvent (ValidationGroup);
  228. }
  229. #else
  230. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  231. ClientScriptManager csm = new ClientScriptManager (Page);
  232. writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
  233. }
  234. #endif
  235. PreProcessRelativeReference (writer,"src");
  236. base.RenderAttributes (writer);
  237. }
  238. void SetAtt (string name, string value)
  239. {
  240. if ((value == null) || (value.Length == 0))
  241. Attributes.Remove (name);
  242. else
  243. Attributes [name] = value;
  244. }
  245. string GetAtt (string name)
  246. {
  247. string res = Attributes [name];
  248. if (res == null)
  249. return String.Empty;
  250. return res;
  251. }
  252. [WebSysDescription("")]
  253. [WebCategory("Action")]
  254. public event ImageClickEventHandler ServerClick {
  255. add { Events.AddHandler (ServerClickEvent, value); }
  256. remove { Events.AddHandler (ServerClickEvent, value); }
  257. }
  258. }
  259. }