ImageButton.cs 3.7 KB

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