2
0

HtmlInputImage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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, IPostBackEventHandler
  45. {
  46. static readonly object ServerClickEvent = new object ();
  47. int clicked_x;
  48. 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. ValidateEvent (UniqueID, String.Empty);
  170. RaisePostDataChangedEventInternal ();
  171. }
  172. #endif
  173. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  174. NameValueCollection postCollection)
  175. {
  176. #if NET_2_0
  177. return LoadPostData (postDataKey, postCollection);
  178. #else
  179. return LoadPostDataInternal (postDataKey, postCollection);
  180. #endif
  181. }
  182. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  183. {
  184. #if NET_2_0
  185. RaisePostDataChangedEvent();
  186. #else
  187. RaisePostDataChangedEventInternal ();
  188. #endif
  189. }
  190. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  191. {
  192. #if NET_2_0
  193. RaisePostBackEvent (eventArgument);
  194. #else
  195. RaisePostBackEventInternal (eventArgument);
  196. #endif
  197. }
  198. #if NET_2_0
  199. protected internal
  200. #else
  201. protected
  202. #endif
  203. override void OnPreRender (EventArgs e)
  204. {
  205. base.OnPreRender (e);
  206. if (Page != null && !Disabled) {
  207. Page.RegisterRequiresPostBack (this);
  208. #if NET_2_0
  209. Page.RegisterEnabledControl (this);
  210. #endif
  211. }
  212. }
  213. protected virtual void OnServerClick (ImageClickEventArgs e)
  214. {
  215. ImageClickEventHandler handler = Events [ServerClickEvent] as ImageClickEventHandler;
  216. if (handler != null)
  217. handler (this, e);
  218. }
  219. protected override void RenderAttributes (HtmlTextWriter writer)
  220. {
  221. #if NET_2_0
  222. if (Page != null)
  223. Page.ClientScript.RegisterForEventValidation (UniqueID);
  224. if (CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup)) {
  225. ClientScriptManager csm = Page.ClientScript;
  226. Attributes ["onclick"] += csm.GetClientValidationEvent (ValidationGroup);
  227. }
  228. #else
  229. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  230. ClientScriptManager csm = new ClientScriptManager (Page);
  231. writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
  232. }
  233. #endif
  234. PreProcessRelativeReference (writer,"src");
  235. base.RenderAttributes (writer);
  236. }
  237. void SetAtt (string name, string value)
  238. {
  239. if ((value == null) || (value.Length == 0))
  240. Attributes.Remove (name);
  241. else
  242. Attributes [name] = value;
  243. }
  244. string GetAtt (string name)
  245. {
  246. string res = Attributes [name];
  247. if (res == null)
  248. return String.Empty;
  249. return res;
  250. }
  251. [WebSysDescription("")]
  252. [WebCategory("Action")]
  253. public event ImageClickEventHandler ServerClick {
  254. add { Events.AddHandler (ServerClickEvent, value); }
  255. remove { Events.AddHandler (ServerClickEvent, value); }
  256. }
  257. }
  258. }