HtmlButton.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* System.Web.UI.HtmlControls
  2. * Authors
  3. * Leen Toelen ([email protected])
  4. */
  5. using System;
  6. using System.ComponentModel;
  7. using System.Web;
  8. using System.Web.UI;
  9. namespace System.Web.UI.HtmlControls{
  10. [DefaultEvent("ServerClick")]
  11. public class HtmlButton : HtmlContainerControl, IPostBackEventHandler{
  12. private static readonly object EventServerClick = new object ();
  13. //Checked
  14. public HtmlButton(): base("button"){}
  15. protected override void OnPreRender (EventArgs e)
  16. {
  17. base.OnPreRender (e);
  18. if (Page != null && Events [EventServerClick] != null)
  19. Page.RequiresPostBackScript ();
  20. }
  21. //Checked
  22. protected virtual void OnServerClick(EventArgs e){
  23. EventHandler handler;
  24. handler = (EventHandler) Events[EventServerClick];
  25. if (handler != null)
  26. handler (this, e);
  27. }
  28. protected override void RenderAttributes (HtmlTextWriter writer)
  29. {
  30. if (Page != null && Events [EventServerClick] != null) {
  31. string script = Page.GetPostBackClientEvent (this, String.Empty);
  32. AttributeCollection coll = Attributes;
  33. if (coll ["language"] != null)
  34. coll.Remove ("language");
  35. writer.WriteAttribute ("language", "javascript");
  36. string onclick;
  37. if ((onclick = coll ["onclick"]) != null) {
  38. script = onclick + " " + script;
  39. coll.Remove ("onclick");
  40. }
  41. writer.WriteAttribute ("onclick", script);
  42. }
  43. base.RenderAttributes(writer);
  44. }
  45. void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument){
  46. if (CausesValidation){
  47. Page.Validate();
  48. }
  49. OnServerClick(EventArgs.Empty);
  50. }
  51. [WebCategory("Action")]
  52. [WebSysDescription("Fires when the control is clicked.")]
  53. public event EventHandler ServerClick{
  54. add{
  55. Events.AddHandler(EventServerClick, value);
  56. }
  57. remove{
  58. Events.RemoveHandler(EventServerClick, value);
  59. }
  60. }
  61. [DefaultValue(true)]
  62. [WebCategory("Behavior")]
  63. public bool CausesValidation{
  64. get{
  65. object attr = ViewState["CausesValidation"];
  66. if (attr != null){
  67. return (Boolean) attr;
  68. }
  69. return true;
  70. }
  71. set{
  72. ViewState["CausesValidation"] = (Boolean) value;
  73. }
  74. }
  75. } // class HtmlButton
  76. } // namespace System.Web.UI.HtmlControls