ImageButton.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. static readonly object ClickEvent = new object ();
  47. static readonly object CommandEvent = new object ();
  48. 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. public virtual new bool Enabled
  107. {
  108. // Should there be any special code below? Doesn't look so...
  109. get { return base.Enabled; }
  110. set { base.Enabled = value; }
  111. }
  112. [Browsable (false)]
  113. [EditorBrowsable (EditorBrowsableState.Never)]
  114. [Themeable (false)]
  115. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  116. public override bool GenerateEmptyAlternateText
  117. {
  118. get { return false; }
  119. set { throw new NotSupportedException (); }
  120. }
  121. [DefaultValue ("")]
  122. [Themeable (false)]
  123. public virtual string OnClientClick
  124. {
  125. get {
  126. return ViewState.GetString ("OnClientClick", String.Empty);
  127. }
  128. set {
  129. ViewState ["OnClientClick"] = value;
  130. }
  131. }
  132. [Themeable (false)]
  133. #if NET_2_0
  134. [UrlProperty ("*.aspx")]
  135. #else
  136. [UrlProperty]
  137. #endif
  138. [DefaultValue ("")]
  139. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  140. public virtual string PostBackUrl {
  141. get {
  142. return ViewState.GetString ("PostBackUrl", String.Empty);
  143. }
  144. set {
  145. ViewState["PostBackUrl"] = value;
  146. }
  147. }
  148. [Themeable (false)]
  149. [DefaultValue ("")]
  150. [WebSysDescription ("")]
  151. [WebCategory ("Behavior")]
  152. public virtual string ValidationGroup
  153. {
  154. get {
  155. return ViewState.GetString ("ValidationGroup", "");
  156. }
  157. set {
  158. ViewState ["ValidationGroup"] = value;
  159. }
  160. }
  161. #endif
  162. [Browsable(false)]
  163. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  164. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  165. protected virtual new
  166. #else
  167. protected override
  168. #endif
  169. HtmlTextWriterTag TagKey {
  170. get { return HtmlTextWriterTag.Input; }
  171. }
  172. #if NET_2_0
  173. // Gets or sets the value of the ImageButton control's AlternateText property. (MSDN)
  174. protected virtual string Text
  175. {
  176. get {
  177. return AlternateText;
  178. }
  179. set {
  180. AlternateText = value;
  181. }
  182. }
  183. #endif
  184. protected override void AddAttributesToRender (HtmlTextWriter writer)
  185. {
  186. Page page = Page;
  187. if (page != null)
  188. page.VerifyRenderingInServerForm (this);
  189. writer.AddAttribute (HtmlTextWriterAttribute.Type, "image", false);
  190. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  191. base.AddAttributesToRender (writer);
  192. #if NET_2_0
  193. string onclick = OnClientClick;
  194. if (!String.IsNullOrEmpty (onclick))
  195. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
  196. else
  197. onclick = String.Empty;
  198. if (Attributes ["onclick"] != null) {
  199. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
  200. Attributes.Remove ("onclick");
  201. }
  202. if (page != null)
  203. onclick += GetClientScriptEventReference ();
  204. if (onclick.Length > 0)
  205. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
  206. #else
  207. if (CausesValidation && page != null && page.AreValidatorsUplevel ()) {
  208. ClientScriptManager csm = new ClientScriptManager (page);
  209. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, csm.GetClientValidationEvent ());
  210. writer.AddAttribute ("language", "javascript");
  211. }
  212. #endif
  213. }
  214. #if NET_2_0
  215. internal virtual string GetClientScriptEventReference ()
  216. {
  217. PostBackOptions options = GetPostBackOptions ();
  218. Page page = Page;
  219. if (options.PerformValidation || !String.IsNullOrEmpty (options.ActionUrl)) {
  220. return page != null ? page.ClientScript.GetPostBackEventReference (options, true) : String.Empty;
  221. } else {
  222. if (page != null)
  223. page.ClientScript.RegisterForEventValidation (options);
  224. return String.Empty;
  225. }
  226. }
  227. protected virtual PostBackOptions GetPostBackOptions ()
  228. {
  229. PostBackOptions options = new PostBackOptions (this);
  230. Page page = Page;
  231. options.ActionUrl = (PostBackUrl.Length > 0 ?
  232. #if TARGET_J2EE
  233. CreateActionUrl (PostBackUrl)
  234. #else
  235. (page != null ? page.ResolveClientUrl (PostBackUrl) : null)
  236. #endif
  237. : null);
  238. options.Argument = String.Empty;
  239. options.ClientSubmit = true;
  240. options.RequiresJavaScriptProtocol = true;
  241. options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
  242. if (options.PerformValidation)
  243. options.ValidationGroup = ValidationGroup;
  244. else
  245. options.ValidationGroup = null;
  246. return options;
  247. }
  248. #endif
  249. #if NET_2_0
  250. protected virtual
  251. #endif
  252. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  253. {
  254. string x, y;
  255. string unique = UniqueID;
  256. x = postCollection [unique + ".x"];
  257. y = postCollection [unique + ".y"];
  258. if (x != null && x != "" && y != null && y != "") {
  259. pos_x = Int32.Parse(x);
  260. pos_y = Int32.Parse(y);
  261. Page.RegisterRequiresRaiseEvent (this);
  262. return true;
  263. } else {
  264. x = postCollection [unique];
  265. if (x != null && x != "") {
  266. pos_x = Int32.Parse (x);
  267. pos_y = 0;
  268. Page.RegisterRequiresRaiseEvent (this);
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. #if NET_2_0
  275. protected virtual
  276. #endif
  277. void RaisePostDataChangedEvent ()
  278. {
  279. }
  280. #if NET_2_0
  281. protected virtual
  282. #endif
  283. void RaisePostBackEvent (string eventArgument)
  284. {
  285. #if NET_2_0
  286. ValidateEvent (UniqueID, String.Empty);
  287. #endif
  288. if (CausesValidation)
  289. #if NET_2_0
  290. Page.Validate (ValidationGroup);
  291. #else
  292. Page.Validate ();
  293. #endif
  294. OnClick (new ImageClickEventArgs (pos_x, pos_y));
  295. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  296. }
  297. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  298. {
  299. return LoadPostData (postDataKey, postCollection);
  300. }
  301. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  302. {
  303. RaisePostDataChangedEvent ();
  304. }
  305. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  306. {
  307. RaisePostBackEvent (eventArgument);
  308. }
  309. protected virtual void OnClick (ImageClickEventArgs e)
  310. {
  311. if (Events != null) {
  312. ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
  313. if (eh != null)
  314. eh (this, e);
  315. }
  316. }
  317. protected virtual void OnCommand (CommandEventArgs e)
  318. {
  319. if (Events != null) {
  320. CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
  321. if (eh != null)
  322. eh (this, e);
  323. }
  324. RaiseBubbleEvent (this, e);
  325. }
  326. #if NET_2_0
  327. protected internal
  328. #else
  329. protected
  330. #endif
  331. override void OnPreRender (EventArgs e)
  332. {
  333. if (Page != null && Enabled)
  334. Page.RegisterRequiresPostBack (this);
  335. }
  336. [WebSysDescription ("")]
  337. [WebCategory ("Action")]
  338. public event ImageClickEventHandler Click
  339. {
  340. add {
  341. Events.AddHandler (ClickEvent, value);
  342. }
  343. remove {
  344. Events.RemoveHandler (ClickEvent, value);
  345. }
  346. }
  347. [WebSysDescription ("")]
  348. [WebCategory ("Action")]
  349. public event CommandEventHandler Command
  350. {
  351. add {
  352. Events.AddHandler (CommandEvent, value);
  353. }
  354. remove {
  355. Events.RemoveHandler (CommandEvent, value);
  356. }
  357. }
  358. #if NET_2_0
  359. string IButtonControl.Text
  360. {
  361. get {
  362. return Text;
  363. }
  364. set {
  365. Text = value;
  366. }
  367. }
  368. event EventHandler IButtonControl.Click
  369. {
  370. add {
  371. Events.AddHandler (ClickEvent, value);
  372. }
  373. remove {
  374. Events.RemoveHandler (ClickEvent, value);
  375. }
  376. }
  377. #endif
  378. }
  379. }