Button.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Web.Controls.Mvc
  6. {
  7. public class Button:BaseControl
  8. {
  9. public Button()
  10. {
  11. Enable = true;
  12. IsPlain = false;
  13. }
  14. public ButtonIcon? Icon { get; set; }
  15. public string Text { get; set; }
  16. public bool? Enable { get; set; }
  17. public bool? IsPlain { get; set; }
  18. public string OnClick { get; set; }
  19. public override string Render()
  20. {
  21. StringBuilder htmlBuilder = new StringBuilder();
  22. htmlBuilder.Append("<a href=\"javascript:void(0);\" ");
  23. htmlBuilder.Append(string.Join(" ", GetPropertyList().Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value))));
  24. htmlBuilder.AppendLine(" >");
  25. if (!string.IsNullOrEmpty(this.Text))
  26. {
  27. htmlBuilder.AppendLine(this.Text);
  28. }
  29. else
  30. {
  31. htmlBuilder.AppendLine("&nbsp;");
  32. }
  33. htmlBuilder.AppendLine("</a> ");
  34. return htmlBuilder.ToString();
  35. }
  36. protected IDictionary<string, string> GetPropertyList()
  37. {
  38. var dictPropertyList = new Dictionary<string, string>();
  39. if (!string.IsNullOrEmpty(this.ID))
  40. {
  41. dictPropertyList.Add("id", this.ID);
  42. }
  43. dictPropertyList.Add("class", string.Format("easyui-linkbutton {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  44. dictPropertyList.Add("href", "javascript:void(0);");
  45. if (!string.IsNullOrEmpty(this.OnClick))
  46. {
  47. dictPropertyList.Add("onclick", this.OnClick );
  48. }
  49. #region data-options
  50. var dictOptions = new Dictionary<string, string>();
  51. if (Icon.HasValue)
  52. {
  53. dictOptions.Add("iconCls", "'" + ButtonIconHelper.GetButtonIconClass(Icon.Value) + "'");
  54. }
  55. if (IsPlain.HasValue)
  56. {
  57. dictOptions.Add("plain", IsPlain.ToString().ToLower());
  58. }
  59. if (Enable.HasValue)
  60. {
  61. dictOptions.Add("disabled", (!Enable).ToString().ToLower());
  62. }
  63. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  64. #endregion data-options
  65. if (Enable.HasValue && !Enable.Value)
  66. {
  67. dictPropertyList.Add("disabled", "disabled");
  68. }
  69. return dictPropertyList;
  70. }
  71. public static Button CreateControl(ButtonOptions buttonOptions, IDictionary<string, string> attributes = null)
  72. {
  73. Button button = new Button();
  74. if (attributes != null)
  75. {
  76. button.Attributes = attributes;
  77. }
  78. button.CssClass = buttonOptions.CssClass;
  79. button.Icon = buttonOptions.Icon;
  80. button.ID = buttonOptions.ID;
  81. button.Text = buttonOptions.Text;
  82. button.IsPlain = buttonOptions.IsPlain;
  83. button.Enable = buttonOptions.Enable;
  84. button.OnClick = buttonOptions.OnClick;
  85. return button;
  86. }
  87. }
  88. }