HtmlInputButton.cs 2.5 KB

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