HtmlInputButton.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if (String.Compare (type, "button", true) == 0 || String.Compare (type, "submit", true) == 0) {
  29. string script = Page.GetPostBackClientEvent (this, String.Empty);
  30. if (script != null &&
  31. ((String.Compare (type, "button", true) == 0 && Events[EventServerClick] != null )||
  32. (String.Compare (type, "submit", true) == 0 && Page.Validators.Count > 0))){
  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. }
  45. }
  46. base.RenderAttributes (writer);
  47. }
  48. protected virtual void OnServerClick(EventArgs e){
  49. EventHandler handler = (EventHandler) Events[EventServerClick];
  50. if (handler != null){
  51. handler (this, e);
  52. }
  53. }
  54. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  55. {
  56. if(CausesValidation == true){
  57. Page.Validate();
  58. }
  59. OnServerClick(EventArgs.Empty);
  60. }
  61. [WebCategory("Action")]
  62. [WebSysDescription("Fires when the control is clicked.")]
  63. public event EventHandler ServerClick{
  64. add{
  65. Events.AddHandler(EventServerClick, value);
  66. }
  67. remove{
  68. Events.RemoveHandler(EventServerClick, value);
  69. }
  70. }
  71. [DefaultValue(true)]
  72. [WebCategory("Behavior")]
  73. public bool CausesValidation{
  74. get{
  75. object causesVal = ViewState["CausesValidation"];
  76. if (causesVal != null){
  77. return (Boolean) causesVal;
  78. }
  79. return true;
  80. }
  81. set{
  82. ViewState["CausesValidation"] = (Boolean) value;
  83. }
  84. }
  85. } // end of System.Web.UI.HtmlControls.HtmlInputButton
  86. } // namespace System.Web.UI.HtmlControls