ImageButton.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // System.Web.UI.WebControls.ImageButton.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.ComponentModel;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [DefaultEvent("Click")]
  40. public class ImageButton: Image, IPostBackDataHandler, IPostBackEventHandler
  41. {
  42. private static readonly object ClickEvent = new object();
  43. private static readonly object CommandEvent = new object();
  44. private int x, y;
  45. public ImageButton(): base()
  46. {
  47. }
  48. [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
  49. [WebSysDescription ("Determines if validation is performed when clicked.")]
  50. public bool CausesValidation
  51. {
  52. get
  53. {
  54. object o = ViewState["CausesValidation"];
  55. if(o!=null)
  56. return (bool)o;
  57. return true;
  58. }
  59. set
  60. {
  61. ViewState["CausesValidation"] = value;
  62. }
  63. }
  64. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  65. [WebSysDescription ("An argument for the Command of this control.")]
  66. public string CommandArgument
  67. {
  68. get
  69. {
  70. object o = ViewState["CommandArgument"];
  71. if(o!=null)
  72. return (string)o;
  73. return String.Empty;
  74. }
  75. set
  76. {
  77. ViewState["CommandArgument"] = value;
  78. }
  79. }
  80. [DefaultValue (""), WebCategory ("Behavior")]
  81. [WebSysDescription ("The name of the Command of this control.")]
  82. public string CommandName
  83. {
  84. get
  85. {
  86. object o = ViewState["CommandName"];
  87. if(o!=null)
  88. return (string)o;
  89. return String.Empty;
  90. }
  91. set
  92. {
  93. ViewState["CommandName"] = value;
  94. }
  95. }
  96. [Browsable (false)]
  97. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  98. protected override HtmlTextWriterTag TagKey
  99. {
  100. get
  101. {
  102. return HtmlTextWriterTag.Input;
  103. }
  104. }
  105. [WebCategory ("Action")]
  106. [WebSysDescription ("Raised when the LinkButton is clicked.")]
  107. public event ImageClickEventHandler Click
  108. {
  109. add
  110. {
  111. Events.AddHandler(ClickEvent, value);
  112. }
  113. remove
  114. {
  115. Events.RemoveHandler(ClickEvent, value);
  116. }
  117. }
  118. [WebCategory ("Action")]
  119. [WebSysDescription ("Raised when a LinkButton Command is executed.")]
  120. public event CommandEventHandler Command
  121. {
  122. add
  123. {
  124. Events.AddHandler(CommandEvent, value);
  125. }
  126. remove
  127. {
  128. Events.RemoveHandler(CommandEvent, value);
  129. }
  130. }
  131. protected override void AddAttributesToRender(HtmlTextWriter writer)
  132. {
  133. writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
  134. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  135. if(Page != null && CausesValidation)
  136. {
  137. if(Page.Validators.Count > 0)
  138. {
  139. writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent(Page));
  140. writer.AddAttribute("language", "javascript");
  141. }
  142. }
  143. base.AddAttributesToRender(writer);
  144. }
  145. protected virtual void OnClick(ImageClickEventArgs e)
  146. {
  147. if(Events != null)
  148. {
  149. ImageClickEventHandler iceh = (ImageClickEventHandler)(Events[ClickEvent]);
  150. if(iceh != null)
  151. iceh(this, e);
  152. }
  153. }
  154. protected virtual void OnCommand(CommandEventArgs e)
  155. {
  156. if(Events != null)
  157. {
  158. CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);
  159. if(ceh != null)
  160. ceh(this, e);
  161. RaiseBubbleEvent(this, e);
  162. }
  163. }
  164. protected override void OnPreRender(EventArgs e)
  165. {
  166. if(Page != null)
  167. {
  168. Page.RegisterRequiresPostBack(this);
  169. }
  170. }
  171. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  172. {
  173. string xCoord = postCollection[UniqueID + ".x"];
  174. string yCoord = postCollection[UniqueID + ".y"];
  175. string id = postCollection[UniqueID];
  176. if(xCoord != null && yCoord != null && xCoord.Length > 0 && yCoord.Length > 0)
  177. {
  178. x = Int32.Parse(xCoord);
  179. y = Int32.Parse(yCoord);
  180. Page.RegisterRequiresRaiseEvent(this);
  181. } else if (id != null)
  182. {
  183. //
  184. // This is a workaround for bug #49819. It appears that the .x and .y
  185. // values are not being posted, and only the x value is being posted
  186. // with the ctrl's id as the key.
  187. //
  188. x = Int32.Parse (id);
  189. Page.RegisterRequiresRaiseEvent (this);
  190. }
  191. return false;
  192. }
  193. void IPostBackDataHandler.RaisePostDataChangedEvent()
  194. {
  195. }
  196. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  197. {
  198. if(CausesValidation)
  199. Page.Validate();
  200. OnClick(new ImageClickEventArgs(x, y));
  201. OnCommand(new CommandEventArgs(CommandName, CommandArgument));
  202. }
  203. }
  204. }