ImageButton.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ImageButton
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Specialized;
  16. using System.Web;
  17. using System.Web.UI;
  18. using System.ComponentModel;
  19. namespace System.Web.UI.WebControls
  20. {
  21. [DefaultEvent("Click")]
  22. public class ImageButton: Image, IPostBackDataHandler, IPostBackEventHandler
  23. {
  24. private static readonly object ClickEvent = new object();
  25. private static readonly object CommandEvent = new object();
  26. private int x, y;
  27. public ImageButton(): base()
  28. {
  29. }
  30. public bool CausesValidation
  31. {
  32. get
  33. {
  34. object o = ViewState["CausesValidation"];
  35. if(o!=null)
  36. return (bool)o;
  37. return true;
  38. }
  39. set
  40. {
  41. ViewState["CausesValidation"] = value;
  42. }
  43. }
  44. public string CommandArgument
  45. {
  46. get
  47. {
  48. object o = ViewState["CommandArgument"];
  49. if(o!=null)
  50. return (string)o;
  51. return String.Empty;
  52. }
  53. set
  54. {
  55. ViewState["CommandArgument"] = value;
  56. }
  57. }
  58. public string CommandName
  59. {
  60. get
  61. {
  62. object o = ViewState["CommandName"];
  63. if(o!=null)
  64. return (string)o;
  65. return String.Empty;
  66. }
  67. set
  68. {
  69. ViewState["CommandName"] = value;
  70. }
  71. }
  72. protected override HtmlTextWriterTag TagKey
  73. {
  74. get
  75. {
  76. return HtmlTextWriterTag.Input;
  77. }
  78. }
  79. public event ImageClickEventHandler Click
  80. {
  81. add
  82. {
  83. Events.AddHandler(ClickEvent, value);
  84. }
  85. remove
  86. {
  87. Events.RemoveHandler(ClickEvent, value);
  88. }
  89. }
  90. public event CommandEventHandler Command
  91. {
  92. add
  93. {
  94. Events.AddHandler(CommandEvent, value);
  95. }
  96. remove
  97. {
  98. Events.RemoveHandler(CommandEvent, value);
  99. }
  100. }
  101. protected override void AddAttributesToRender(HtmlTextWriter writer)
  102. {
  103. writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
  104. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  105. if(Page != null && CausesValidation)
  106. {
  107. if(Page.Validators.Count > 0)
  108. {
  109. writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent(Page));
  110. writer.AddAttribute("language", "javascript");
  111. }
  112. }
  113. base.AddAttributesToRender(writer);
  114. }
  115. protected virtual void OnClick(ImageClickEventArgs e)
  116. {
  117. if(Events != null)
  118. {
  119. ImageClickEventHandler iceh = (ImageClickEventHandler)(Events[ClickEvent]);
  120. if(iceh != null)
  121. iceh(this, e);
  122. }
  123. }
  124. protected virtual void OnCommand(CommandEventArgs e)
  125. {
  126. if(Events != null)
  127. {
  128. CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);
  129. if(ceh != null)
  130. ceh(this, e);
  131. RaiseBubbleEvent(this, e);
  132. }
  133. }
  134. protected override void OnPreRender(EventArgs e)
  135. {
  136. if(Page != null)
  137. {
  138. Page.RegisterRequiresPostBack(this);
  139. }
  140. }
  141. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  142. {
  143. string xCoord = postCollection[UniqueID + ".x"];
  144. string yCoord = postCollection[UniqueID + ".y"];
  145. if(xCoord != null && yCoord != null && xCoord.Length > 0 && yCoord.Length > 0)
  146. {
  147. x = Int32.Parse(xCoord);
  148. y = Int32.Parse(yCoord);
  149. Page.RegisterRequiresRaiseEvent(this);
  150. }
  151. return false;
  152. }
  153. void IPostBackDataHandler.RaisePostDataChangedEvent()
  154. {
  155. }
  156. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  157. {
  158. if(CausesValidation)
  159. Page.Validate();
  160. OnClick(new ImageClickEventArgs(x, y));
  161. OnCommand(new CommandEventArgs(CommandName, CommandArgument));
  162. }
  163. }
  164. }