HtmlInputButton.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* System.Web.UI.HtmlControls
  2. * Authors
  3. * Leen Toelen ([email protected])
  4. */
  5. using System;
  6. using System.ComponentModel;
  7. using System.Globalization;
  8. using System.Web;
  9. using System.Web.UI;
  10. namespace System.Web.UI.HtmlControls{
  11. [DefaultEvent("ServerClick")]
  12. public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler{
  13. private static readonly object EventServerClick = new object ();
  14. public HtmlInputButton(): base ("button")
  15. {
  16. }
  17. public HtmlInputButton(string type): base(type){}
  18. protected override void OnPreRender (EventArgs e)
  19. {
  20. base.OnPreRender(e);
  21. if (Page != null && Events [EventServerClick] != null)
  22. Page.RequiresPostBackScript ();
  23. }
  24. protected override void RenderAttributes (HtmlTextWriter writer)
  25. {
  26. if (Page != null && CausesValidation) {
  27. string type = Type;
  28. Console.WriteLine(type);
  29. if (String.Compare (type, "button", true) == 0 || String.Compare (type, "submit", true) == 0) {
  30. string script = Page.GetPostBackClientEvent (this, String.Empty);
  31. if (script != null && Page.Validators.Count > 0){
  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. }
  44. }
  45. base.RenderAttributes (writer);
  46. }
  47. protected virtual void OnServerClick(EventArgs e){
  48. EventHandler handler = (EventHandler) Events[EventServerClick];
  49. if (handler != null){
  50. handler (this, e);
  51. }
  52. }
  53. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  54. {
  55. if(CausesValidation == true){
  56. Page.Validate();
  57. }
  58. OnServerClick(EventArgs.Empty);
  59. }
  60. [WebCategory("Action")]
  61. [WebSysDescription("Fires when the control is clicked.")]
  62. public event EventHandler ServerClick{
  63. add{
  64. Events.AddHandler(EventServerClick, value);
  65. }
  66. remove{
  67. Events.RemoveHandler(EventServerClick, value);
  68. }
  69. }
  70. [DefaultValue(true)]
  71. [WebCategory("Behavior")]
  72. public bool CausesValidation{
  73. get{
  74. object causesVal = ViewState["CausesValidation"];
  75. if (causesVal != null){
  76. return (Boolean) causesVal;
  77. }
  78. return true;
  79. }
  80. set{
  81. ViewState["CausesValidation"] = (Boolean) value;
  82. }
  83. }
  84. } // end of System.Web.UI.HtmlControls.HtmlInputButton
  85. } // namespace System.Web.UI.HtmlControls