ImageButton.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // System.Web.UI.WebControls.ImageButton.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez ([email protected])
  6. //
  7. // (C) 2005-2010 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. {
  35. // CAS
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. // attributes
  39. [DefaultEvent("Click")]
  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. {
  44. static readonly object ClickEvent = new object ();
  45. static readonly object CommandEvent = new object ();
  46. int pos_x, pos_y;
  47. public ImageButton ()
  48. {
  49. }
  50. [DefaultValue(true)]
  51. [WebSysDescription ("")]
  52. [WebCategory ("Behavior")]
  53. [Themeable (false)]
  54. public virtual bool CausesValidation {
  55. get { return ViewState.GetBool ("CausesValidation", true); }
  56. set { ViewState ["CausesValidation"] = value; }
  57. }
  58. [Bindable(true)]
  59. [DefaultValue("")]
  60. [WebSysDescription ("")]
  61. [WebCategory ("Behavior")]
  62. [Themeable (false)]
  63. public string CommandArgument {
  64. get { return ViewState.GetString ("CommandArgument", String.Empty); }
  65. set { ViewState ["CommandArgument"] = value; }
  66. }
  67. [DefaultValue("")]
  68. [WebSysDescription ("")]
  69. [WebCategory ("Behavior")]
  70. [Themeable (false)]
  71. public string CommandName {
  72. get { return ViewState.GetString ("CommandName", String.Empty); }
  73. set { ViewState ["CommandName"] = value; }
  74. }
  75. [EditorBrowsable (EditorBrowsableState.Always)]
  76. [Browsable (true)]
  77. [DefaultValue (true)]
  78. [Bindable (true)]
  79. public virtual new bool Enabled {
  80. // Should there be any special code below? Doesn't look so...
  81. get { return base.Enabled; }
  82. set { base.Enabled = value; }
  83. }
  84. [Browsable (false)]
  85. [EditorBrowsable (EditorBrowsableState.Never)]
  86. [Themeable (false)]
  87. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  88. public override bool GenerateEmptyAlternateText {
  89. get { return false; }
  90. set { throw new NotSupportedException (); }
  91. }
  92. [DefaultValue ("")]
  93. [Themeable (false)]
  94. public virtual string OnClientClick {
  95. get { return ViewState.GetString ("OnClientClick", String.Empty); }
  96. set { ViewState ["OnClientClick"] = value; }
  97. }
  98. [Themeable (false)]
  99. [UrlProperty ("*.aspx")]
  100. [DefaultValue ("")]
  101. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  102. public virtual string PostBackUrl {
  103. get { return ViewState.GetString ("PostBackUrl", String.Empty); }
  104. set { ViewState["PostBackUrl"] = value; }
  105. }
  106. [Themeable (false)]
  107. [DefaultValue ("")]
  108. [WebSysDescription ("")]
  109. [WebCategory ("Behavior")]
  110. public virtual string ValidationGroup {
  111. get { return ViewState.GetString ("ValidationGroup", String.Empty); }
  112. set { ViewState ["ValidationGroup"] = value; }
  113. }
  114. [Browsable(false)]
  115. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  116. protected override HtmlTextWriterTag TagKey {
  117. get { return HtmlTextWriterTag.Input; }
  118. }
  119. // Gets or sets the value of the ImageButton control's AlternateText property. (MSDN)
  120. protected virtual string Text {
  121. get { return AlternateText; }
  122. set { AlternateText = value; }
  123. }
  124. #if NET_4_0
  125. public override bool SupportsDisabledAttribute {
  126. get { return RenderingCompatibilityLessThan40; }
  127. }
  128. #endif
  129. protected override void AddAttributesToRender (HtmlTextWriter writer)
  130. {
  131. Page page = Page;
  132. if (page != null)
  133. page.VerifyRenderingInServerForm (this);
  134. writer.AddAttribute (HtmlTextWriterAttribute.Type, "image", false);
  135. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  136. base.AddAttributesToRender (writer);
  137. string onclick = OnClientClick;
  138. if (!String.IsNullOrEmpty (onclick))
  139. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
  140. else
  141. onclick = String.Empty;
  142. if (HasAttributes && Attributes ["onclick"] != null) {
  143. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
  144. Attributes.Remove ("onclick");
  145. }
  146. if (page != null)
  147. onclick += GetClientScriptEventReference ();
  148. if (onclick.Length > 0)
  149. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
  150. }
  151. internal virtual string GetClientScriptEventReference ()
  152. {
  153. PostBackOptions options = GetPostBackOptions ();
  154. Page page = Page;
  155. if (options.PerformValidation || !String.IsNullOrEmpty (options.ActionUrl))
  156. return page != null ? page.ClientScript.GetPostBackEventReference (options, true) : String.Empty;
  157. else {
  158. if (page != null)
  159. page.ClientScript.RegisterForEventValidation (options);
  160. return String.Empty;
  161. }
  162. }
  163. protected virtual PostBackOptions GetPostBackOptions ()
  164. {
  165. PostBackOptions options = new PostBackOptions (this);
  166. Page page = Page;
  167. options.ActionUrl = (PostBackUrl.Length > 0 ?
  168. #if TARGET_J2EE
  169. CreateActionUrl (PostBackUrl)
  170. #else
  171. (page != null ? page.ResolveClientUrl (PostBackUrl) : null)
  172. #endif
  173. : null);
  174. options.Argument = String.Empty;
  175. options.ClientSubmit = true;
  176. options.RequiresJavaScriptProtocol = true;
  177. options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
  178. if (options.PerformValidation)
  179. options.ValidationGroup = ValidationGroup;
  180. else
  181. options.ValidationGroup = null;
  182. return options;
  183. }
  184. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  185. {
  186. string x, y;
  187. string unique = UniqueID;
  188. x = postCollection [unique + ".x"];
  189. y = postCollection [unique + ".y"];
  190. if (!String.IsNullOrEmpty (x) && !String.IsNullOrEmpty (y)) {
  191. pos_x = Int32.Parse(x);
  192. pos_y = Int32.Parse(y);
  193. Page.RegisterRequiresRaiseEvent (this);
  194. return true;
  195. } else {
  196. x = postCollection [unique];
  197. if (!String.IsNullOrEmpty (x)) {
  198. pos_x = Int32.Parse (x);
  199. pos_y = 0;
  200. Page.RegisterRequiresRaiseEvent (this);
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. protected virtual void RaisePostDataChangedEvent ()
  207. {
  208. }
  209. protected virtual void RaisePostBackEvent (string eventArgument)
  210. {
  211. ValidateEvent (UniqueID, String.Empty);
  212. if (CausesValidation) {
  213. Page page = Page;
  214. if (page != null)
  215. page.Validate (ValidationGroup);
  216. }
  217. OnClick (new ImageClickEventArgs (pos_x, pos_y));
  218. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  219. }
  220. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  221. {
  222. return LoadPostData (postDataKey, postCollection);
  223. }
  224. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  225. {
  226. RaisePostDataChangedEvent ();
  227. }
  228. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  229. {
  230. RaisePostBackEvent (eventArgument);
  231. }
  232. protected virtual void OnClick (ImageClickEventArgs e)
  233. {
  234. if (Events != null) {
  235. ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
  236. if (eh != null)
  237. eh (this, e);
  238. }
  239. }
  240. protected virtual void OnCommand (CommandEventArgs e)
  241. {
  242. if (Events != null) {
  243. CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
  244. if (eh != null)
  245. eh (this, e);
  246. }
  247. RaiseBubbleEvent (this, e);
  248. }
  249. protected internal override void OnPreRender (EventArgs e)
  250. {
  251. Page page = Page;
  252. if (page != null && IsEnabled)
  253. page.RegisterRequiresPostBack (this);
  254. }
  255. [WebSysDescription ("")]
  256. [WebCategory ("Action")]
  257. public event ImageClickEventHandler Click {
  258. add { Events.AddHandler (ClickEvent, value); }
  259. remove { Events.RemoveHandler (ClickEvent, value); }
  260. }
  261. [WebSysDescription ("")]
  262. [WebCategory ("Action")]
  263. public event CommandEventHandler Command {
  264. add { Events.AddHandler (CommandEvent, value); }
  265. remove { Events.RemoveHandler (CommandEvent, value); }
  266. }
  267. string IButtonControl.Text {
  268. get { return Text; }
  269. set { Text = value; }
  270. }
  271. event EventHandler IButtonControl.Click {
  272. add { Events.AddHandler (ClickEvent, value); }
  273. remove { Events.RemoveHandler (ClickEvent, value); }
  274. }
  275. }
  276. }