LinkButton.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 LinkButton : BaseControl
  8. {
  9. /// <summary>
  10. /// 图像地址
  11. /// </summary>
  12. protected string ImageURL { get; set; }
  13. /// <summary>
  14. /// 超链接显示的文本
  15. /// </summary>
  16. protected string Text { get; set; }
  17. /// <summary>
  18. /// 超链接地址
  19. /// </summary>
  20. protected string Href { get; set; }
  21. /// <summary>
  22. /// 跳转方式
  23. /// </summary>
  24. protected TargetTypeEnum? Target { get; set; }
  25. /// <summary>
  26. /// 事件绑定
  27. /// </summary>
  28. protected Dictionary<MouseEventEnum, string> Function { get; set; }
  29. public LinkButton()
  30. {
  31. }
  32. public Dictionary<string, string> GetAttributesList()
  33. {
  34. Dictionary<string, string> attributesList = new Dictionary<string, string>();
  35. if (!string.IsNullOrEmpty(this.ID))
  36. attributesList.Add("id", this.ID);
  37. else
  38. attributesList.Add("id", string.Format("btn_{0}", Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6)));
  39. if (!string.IsNullOrEmpty(this.CssClass))
  40. attributesList.Add("class", this.CssClass);
  41. if (!string.IsNullOrEmpty(Href))
  42. attributesList.Add(
  43. "href",
  44. Href.ToString().IndexOf("://") == -1 ? string.Format("http://{0}", Href.ToString()) : Href.ToString());
  45. else
  46. attributesList.Add("href", "JavaScript:void(0);");
  47. if (Target.HasValue)
  48. {
  49. switch (Target)
  50. {
  51. case TargetTypeEnum.Blank:
  52. attributesList.Add("target", "_blank");
  53. break;
  54. case TargetTypeEnum.Parent:
  55. attributesList.Add("target", "_parent");
  56. break;
  57. case TargetTypeEnum.Top:
  58. attributesList.Add("target", "_top");
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. if (Function != null && Function.Count > 0)
  65. {
  66. foreach (KeyValuePair<MouseEventEnum, string> kv in Function)
  67. {
  68. string eventStr = "";
  69. switch (kv.Key)
  70. {
  71. case MouseEventEnum.OnClick:
  72. eventStr = "onclick";
  73. break;
  74. case MouseEventEnum.OnMouseOut:
  75. eventStr = "onmouseout";
  76. break;
  77. case MouseEventEnum.OnMouseOver:
  78. eventStr = "onmouseover";
  79. break;
  80. }
  81. attributesList.Add(eventStr, kv.Value);
  82. }
  83. }
  84. #region data-options
  85. Dictionary<string, string> optionsDic = new Dictionary<string, string>();
  86. //optionsDic.Add("id", ID);
  87. attributesList.Add("data-options", string.Join(",", optionsDic.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  88. #endregion
  89. return attributesList;
  90. }
  91. public static LinkButton CreateControl(LinkButtonOptions linkButtonOptions, IDictionary<string, string> attributes = null)
  92. {
  93. LinkButton linkButton = new LinkButton();
  94. if (attributes != null)
  95. {
  96. linkButton.Attributes = attributes;
  97. }
  98. linkButton.CssClass = linkButtonOptions.CssClass;
  99. linkButton.IsShow = linkButtonOptions.IsShow;
  100. linkButton.IsEnabled = linkButtonOptions.IsEnabled;
  101. linkButton.Function = linkButtonOptions.Function;
  102. linkButton.Href = linkButtonOptions.Href;
  103. linkButton.ID = linkButtonOptions.ID;
  104. linkButton.Target = linkButtonOptions.Target;
  105. linkButton.Text = linkButtonOptions.Text;
  106. linkButton.ImageURL = linkButtonOptions.ImageURL;
  107. return linkButton;
  108. }
  109. public override string Render()
  110. {
  111. StringBuilder sb = new StringBuilder();
  112. sb.Append("<li><a ");
  113. sb.Append(string.Join(" ", GetAttributesList().Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value))));
  114. sb.Append(" >");
  115. if (!string.IsNullOrEmpty(ImageURL))
  116. sb.AppendFormat("<img src=\"{0}\" alt=\"图片装载错误\" title=\"{1}\" />", this.ImageURL, this.Text);
  117. else if (!string.IsNullOrEmpty(Text))
  118. sb.AppendFormat("{0}", this.Text);
  119. else if (!string.IsNullOrEmpty(Href))
  120. sb.AppendFormat("{0}", this.Href);
  121. else
  122. throw new Exception("LinkButton必须最少指定Text或Href属性任意一项");
  123. sb.Append("</a></li>");
  124. return sb.ToString();
  125. }
  126. }
  127. }