HtmlButton.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Invoke(this, e);
  27. }
  28. }
  29. protected override void RenderAttributes (HtmlTextWriter writer)
  30. {
  31. if (Page != null && Events [EventServerClick] != null) {
  32. string script = Page.GetPostBackClientEvent (this, String.Empty);
  33. AttributeCollection coll = Attributes;
  34. if (coll ["language"] != null)
  35. coll.Remove ("language");
  36. writer.WriteAttribute ("language", "javascript");
  37. string onclick;
  38. if ((onclick = coll ["onclick"]) != null) {
  39. script = onclick + " " + script;
  40. coll.Remove ("onclick");
  41. }
  42. writer.WriteAttribute ("onclick", script);
  43. }
  44. base.RenderAttributes(writer);
  45. }
  46. void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument){
  47. if (CausesValidation){
  48. Page.Validate();
  49. }
  50. OnServerClick(EventArgs.Empty);
  51. }
  52. [WebCategory("Action")]
  53. [WebSysDescription("Fires when the control is clicked.")]
  54. public event EventHandler ServerClick{
  55. add{
  56. Events.AddHandler(EventServerClick, value);
  57. }
  58. remove{
  59. Events.RemoveHandler(EventServerClick, value);
  60. }
  61. }
  62. [DefaultValue(true)]
  63. [WebCategory("Behavior")]
  64. public bool CausesValidation{
  65. get{
  66. object attr = ViewState["CausesValidation"];
  67. if (attr != null){
  68. return (Boolean) attr;
  69. }
  70. return true;
  71. }
  72. set{
  73. ViewState["CausesValidation"] = (Boolean) value;
  74. }
  75. }
  76. } // class HtmlButton
  77. } // namespace System.Web.UI.HtmlControls