HtmlInputButton.cs 2.6 KB

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