HtmlInputImage.cs 6.7 KB

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