ImageButton.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // System.Web.UI.WebControls.ImageButton.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections.Specialized;
  31. using System.ComponentModel;
  32. using System.Security.Permissions;
  33. namespace System.Web.UI.WebControls {
  34. // CAS
  35. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [DefaultEvent("Click")]
  39. #if NET_2_0
  40. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. [SupportsEventValidation]
  42. public class ImageButton : Image, IPostBackDataHandler, IPostBackEventHandler, IButtonControl {
  43. #else
  44. public class ImageButton : Image, IPostBackDataHandler, IPostBackEventHandler {
  45. #endif
  46. private static readonly object ClickEvent = new object ();
  47. private static readonly object CommandEvent = new object ();
  48. private int pos_x, pos_y;
  49. public ImageButton ()
  50. {
  51. }
  52. #if ONLY_1_1
  53. [Bindable(false)]
  54. #endif
  55. [DefaultValue(true)]
  56. [WebSysDescription ("")]
  57. [WebCategory ("Behavior")]
  58. #if NET_2_0
  59. [Themeable (false)]
  60. public virtual
  61. #else
  62. public
  63. #endif
  64. bool CausesValidation {
  65. get {
  66. return ViewState.GetBool ("CausesValidation", true);
  67. }
  68. set {
  69. ViewState ["CausesValidation"] = value;
  70. }
  71. }
  72. [Bindable(true)]
  73. [DefaultValue("")]
  74. [WebSysDescription ("")]
  75. [WebCategory ("Behavior")]
  76. #if NET_2_0
  77. [Themeable (false)]
  78. #endif
  79. public string CommandArgument {
  80. get {
  81. return ViewState.GetString ("CommandArgument", "");
  82. }
  83. set {
  84. ViewState ["CommandArgument"] = value;
  85. }
  86. }
  87. [DefaultValue("")]
  88. [WebSysDescription ("")]
  89. [WebCategory ("Behavior")]
  90. #if NET_2_0
  91. [Themeable (false)]
  92. #endif
  93. public string CommandName {
  94. get {
  95. return ViewState.GetString ("CommandName", "");
  96. }
  97. set {
  98. ViewState ["CommandName"] = value;
  99. }
  100. }
  101. #if NET_2_0
  102. [EditorBrowsable (EditorBrowsableState.Always)]
  103. [Browsable (true)]
  104. [DefaultValue (true)]
  105. [Bindable (true)]
  106. [MonoTODO]
  107. public virtual new bool Enabled
  108. {
  109. get {
  110. throw new NotImplementedException ();
  111. }
  112. set {
  113. throw new NotImplementedException ();
  114. }
  115. }
  116. [Browsable (false)]
  117. [EditorBrowsable (EditorBrowsableState.Never)]
  118. [Themeable (false)]
  119. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  120. [MonoTODO]
  121. public virtual new bool GenerateEmptyAlternateText
  122. {
  123. get {
  124. throw new NotImplementedException ();
  125. }
  126. set {
  127. throw new NotImplementedException ();
  128. }
  129. }
  130. [DefaultValue ("")]
  131. [Themeable (false)]
  132. public virtual string OnClientClick
  133. {
  134. get {
  135. return ViewState.GetString ("OnClientClick", "");
  136. }
  137. set {
  138. ViewState ["OnClientClick"] = value;
  139. }
  140. }
  141. [Themeable (false)]
  142. #if NET_2_0
  143. [UrlProperty ("*.aspx")]
  144. #else
  145. [UrlProperty]
  146. #endif
  147. [DefaultValue ("")]
  148. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  149. public virtual string PostBackUrl {
  150. get {
  151. return ViewState.GetString ("PostBackUrl", String.Empty);
  152. }
  153. set {
  154. ViewState["PostBackUrl"] = value;
  155. }
  156. }
  157. [Themeable (false)]
  158. [DefaultValue ("")]
  159. [WebSysDescription ("")]
  160. [WebCategory ("Behavior")]
  161. public virtual string ValidationGroup
  162. {
  163. get {
  164. return ViewState.GetString ("ValidationGroup", "");
  165. }
  166. set {
  167. ViewState ["ValidationGroup"] = value;
  168. }
  169. }
  170. #endif
  171. [Browsable(false)]
  172. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  173. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  174. protected virtual new
  175. #else
  176. protected override
  177. #endif
  178. HtmlTextWriterTag TagKey {
  179. get { return HtmlTextWriterTag.Input; }
  180. }
  181. #if NET_2_0
  182. // Gets or sets the value of the ImageButton control's AlternateText property. (MSDN)
  183. protected virtual string Text
  184. {
  185. get {
  186. return AlternateText;
  187. }
  188. set {
  189. AlternateText = value;
  190. }
  191. }
  192. #endif
  193. protected override void AddAttributesToRender (HtmlTextWriter writer)
  194. {
  195. if (Page != null)
  196. Page.VerifyRenderingInServerForm (this);
  197. writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
  198. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  199. #if NET_2_0
  200. string onclick = OnClientClick;
  201. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
  202. if (Attributes ["onclick"] != null) {
  203. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
  204. Attributes.Remove ("onclick");
  205. }
  206. if (Page != null) {
  207. PostBackOptions options = GetPostBackOptions ();
  208. onclick += Page.ClientScript.GetPostBackEventReference (options);
  209. }
  210. if (onclick.Length > 0)
  211. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
  212. #else
  213. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  214. ClientScriptManager csm = new ClientScriptManager (Page);
  215. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, csm.GetClientValidationEvent ());
  216. writer.AddAttribute ("language", "javascript");
  217. }
  218. #endif
  219. base.AddAttributesToRender (writer);
  220. }
  221. #if NET_2_0
  222. protected virtual PostBackOptions GetPostBackOptions ()
  223. {
  224. PostBackOptions options = new PostBackOptions (this);
  225. options.ActionUrl = (PostBackUrl.Length > 0 ? Page.ResolveClientUrl (PostBackUrl) : null);
  226. options.ValidationGroup = null;
  227. options.Argument = "";
  228. options.ClientSubmit = false;
  229. options.RequiresJavaScriptProtocol = false;
  230. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  231. if (options.PerformValidation)
  232. options.ValidationGroup = ValidationGroup;
  233. return options;
  234. }
  235. #endif
  236. #if NET_2_0
  237. protected virtual
  238. #endif
  239. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  240. {
  241. string x, y;
  242. string unique = UniqueID;
  243. x = postCollection [unique + ".x"];
  244. y = postCollection [unique + ".y"];
  245. if (x != null && x != "" && y != null && y != "") {
  246. pos_x = Int32.Parse(x);
  247. pos_y = Int32.Parse(y);
  248. Page.RegisterRequiresRaiseEvent (this);
  249. return true;
  250. } else {
  251. x = postCollection [unique];
  252. if (x != null && x != "") {
  253. pos_x = Int32.Parse (x);
  254. pos_y = 0;
  255. Page.RegisterRequiresRaiseEvent (this);
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. #if NET_2_0
  262. protected virtual
  263. #endif
  264. void RaisePostDataChangedEvent ()
  265. {
  266. }
  267. #if NET_2_0
  268. protected virtual
  269. #endif
  270. void RaisePostBackEvent (string eventArgument)
  271. {
  272. if (CausesValidation)
  273. #if NET_2_0
  274. Page.Validate (ValidationGroup);
  275. #else
  276. Page.Validate ();
  277. #endif
  278. OnClick (new ImageClickEventArgs (pos_x, pos_y));
  279. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  280. }
  281. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  282. {
  283. return LoadPostData (postDataKey, postCollection);
  284. }
  285. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  286. {
  287. RaisePostDataChangedEvent ();
  288. }
  289. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  290. {
  291. RaisePostBackEvent (eventArgument);
  292. }
  293. protected virtual void OnClick (ImageClickEventArgs e)
  294. {
  295. if (Events != null) {
  296. ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
  297. if (eh != null)
  298. eh (this, e);
  299. }
  300. }
  301. protected virtual void OnCommand (CommandEventArgs e)
  302. {
  303. if (Events != null) {
  304. CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
  305. if (eh != null)
  306. eh (this, e);
  307. }
  308. RaiseBubbleEvent (this, e);
  309. }
  310. #if NET_2_0
  311. protected internal
  312. #else
  313. protected
  314. #endif
  315. override void OnPreRender (EventArgs e)
  316. {
  317. if (Page != null)
  318. Page.RegisterRequiresPostBack (this);
  319. }
  320. [WebSysDescription ("")]
  321. [WebCategory ("Action")]
  322. public event ImageClickEventHandler Click
  323. {
  324. add {
  325. Events.AddHandler (ClickEvent, value);
  326. }
  327. remove {
  328. Events.RemoveHandler (ClickEvent, value);
  329. }
  330. }
  331. [WebSysDescription ("")]
  332. [WebCategory ("Action")]
  333. public event CommandEventHandler Command
  334. {
  335. add {
  336. Events.AddHandler (CommandEvent, value);
  337. }
  338. remove {
  339. Events.RemoveHandler (CommandEvent, value);
  340. }
  341. }
  342. #if NET_2_0
  343. string IButtonControl.Text
  344. {
  345. get {
  346. return Text;
  347. }
  348. set {
  349. Text = value;
  350. }
  351. }
  352. event EventHandler IButtonControl.Click
  353. {
  354. add {
  355. Events.AddHandler (ClickEvent, value);
  356. }
  357. remove {
  358. Events.RemoveHandler (ClickEvent, value);
  359. }
  360. }
  361. #endif
  362. }
  363. }