123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Bowin.Web.Controls.Mvc
- {
- public class LinkButton : BaseControl
- {
- /// <summary>
- /// 图像地址
- /// </summary>
- protected string ImageURL { get; set; }
- /// <summary>
- /// 超链接显示的文本
- /// </summary>
- protected string Text { get; set; }
- /// <summary>
- /// 超链接地址
- /// </summary>
- protected string Href { get; set; }
- /// <summary>
- /// 跳转方式
- /// </summary>
- protected TargetTypeEnum? Target { get; set; }
- /// <summary>
- /// 事件绑定
- /// </summary>
- protected Dictionary<MouseEventEnum, string> Function { get; set; }
- public LinkButton()
- {
- }
- public Dictionary<string, string> GetAttributesList()
- {
- Dictionary<string, string> attributesList = new Dictionary<string, string>();
- if (!string.IsNullOrEmpty(this.ID))
- attributesList.Add("id", this.ID);
- else
- attributesList.Add("id", string.Format("btn_{0}", Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6)));
- if (!string.IsNullOrEmpty(this.CssClass))
- attributesList.Add("class", this.CssClass);
- if (!string.IsNullOrEmpty(Href))
- attributesList.Add(
- "href",
- Href.ToString().IndexOf("://") == -1 ? string.Format("http://{0}", Href.ToString()) : Href.ToString());
- else
- attributesList.Add("href", "JavaScript:void(0);");
- if (Target.HasValue)
- {
- switch (Target)
- {
- case TargetTypeEnum.Blank:
- attributesList.Add("target", "_blank");
- break;
- case TargetTypeEnum.Parent:
- attributesList.Add("target", "_parent");
- break;
- case TargetTypeEnum.Top:
- attributesList.Add("target", "_top");
- break;
- default:
- break;
- }
- }
- if (Function != null && Function.Count > 0)
- {
- foreach (KeyValuePair<MouseEventEnum, string> kv in Function)
- {
- string eventStr = "";
- switch (kv.Key)
- {
- case MouseEventEnum.OnClick:
- eventStr = "onclick";
- break;
- case MouseEventEnum.OnMouseOut:
- eventStr = "onmouseout";
- break;
- case MouseEventEnum.OnMouseOver:
- eventStr = "onmouseover";
- break;
- }
- attributesList.Add(eventStr, kv.Value);
- }
- }
- #region data-options
- Dictionary<string, string> optionsDic = new Dictionary<string, string>();
- //optionsDic.Add("id", ID);
- attributesList.Add("data-options", string.Join(",", optionsDic.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
- #endregion
- return attributesList;
- }
- public static LinkButton CreateControl(LinkButtonOptions linkButtonOptions, IDictionary<string, string> attributes = null)
- {
- LinkButton linkButton = new LinkButton();
- if (attributes != null)
- {
- linkButton.Attributes = attributes;
- }
- linkButton.CssClass = linkButtonOptions.CssClass;
- linkButton.IsShow = linkButtonOptions.IsShow;
- linkButton.IsEnabled = linkButtonOptions.IsEnabled;
- linkButton.Function = linkButtonOptions.Function;
- linkButton.Href = linkButtonOptions.Href;
- linkButton.ID = linkButtonOptions.ID;
- linkButton.Target = linkButtonOptions.Target;
- linkButton.Text = linkButtonOptions.Text;
- linkButton.ImageURL = linkButtonOptions.ImageURL;
- return linkButton;
- }
- public override string Render()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("<li><a ");
- sb.Append(string.Join(" ", GetAttributesList().Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value))));
- sb.Append(" >");
- if (!string.IsNullOrEmpty(ImageURL))
- sb.AppendFormat("<img src=\"{0}\" alt=\"图片装载错误\" title=\"{1}\" />", this.ImageURL, this.Text);
- else if (!string.IsNullOrEmpty(Text))
- sb.AppendFormat("{0}", this.Text);
- else if (!string.IsNullOrEmpty(Href))
- sb.AppendFormat("{0}", this.Href);
- else
- throw new Exception("LinkButton必须最少指定Text或Href属性任意一项");
- sb.Append("</a></li>");
- return sb.ToString();
- }
- }
- }
|