using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bowin.Web.Controls.Mvc
{
public class LinkButton : BaseControl
{
///
/// 图像地址
///
protected string ImageURL { get; set; }
///
/// 超链接显示的文本
///
protected string Text { get; set; }
///
/// 超链接地址
///
protected string Href { get; set; }
///
/// 跳转方式
///
protected TargetTypeEnum? Target { get; set; }
///
/// 事件绑定
///
protected Dictionary Function { get; set; }
public LinkButton()
{
}
public Dictionary GetAttributesList()
{
Dictionary attributesList = new Dictionary();
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 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 optionsDic = new Dictionary();
//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 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(" string.Format("{0}=\"{1}\"", x.Key, x.Value))));
sb.Append(" >");
if (!string.IsNullOrEmpty(ImageURL))
sb.AppendFormat("", 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("");
return sb.ToString();
}
}
}