2
0

ImageButton.cs 3.8 KB

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