Metadata 之 KeyValueAttribute

1、继承DataTypeAttribute

 public class KeyValueCollectionAttribute : DataTypeAttribute
    {
        Dictionary<string, string> dic;
        public KeyValueCollectionAttribute(string dicStr)
            : base("KeyValueCollection")
        {
            string[] kvStrs = dicStr.Split('|');

            if (kvStrs.Length == 0)
            {
                throw new ArgumentException("不符合格式", "dicStr");
            }

            dic = new Dictionary<string, string>();

            foreach (string kv in kvStrs)
            {
                string[] tmp = kv.Split('-');

                if (tmp.Length != 2)
                    throw new ArgumentException("不符合格式", "dicStr");

                dic.Add(tmp[0], tmp[1]);
            }
        }

        public override bool IsValid(object value)
        {
            string v = value as string;

            if (string.IsNullOrEmpty(v))
                return true;

            if (dic.ContainsKey(v))
                return true;
            else
                return false;
        }

        public Dictionary<string, string> Dic
        {
            get
            {
                return dic;
            }
        }
    } 

 

2、进行MetaColumn扩展

 public static Dictionary<string, string> GetDic(this MetaColumn column)
        {
            var a = column.Attributes.OfType<KeyValueCollectionAttribute>().FirstOrDefault();
            if (a != null)
            {
                return a.Dic;
            }
            else
            {
                throw new Exception();
            }
        }

3、编写控件 KeyValueCollection.ascx

 .ascx文件

<%@ Control Language="C#" CodeBehind="KeyValueCollection.ascx.cs" Inherits="BtmsWebApp.KeyValueCollection" %>

<asp:Literal runat="server" ID="Literal1" Text="<%# KeyValueCollectionFieldValueString %>" />

cs 文件

 public partial class KeyValueCollection : FieldTemplateUserControl
    {
        public override Control DataControl
        {
            get
            {
                return Literal1;
            }
        }

        public string KeyValueCollectionFieldValueString
        {
            get
            {
                if (FieldValue == null)
                {
                    return FieldValueString;
                }

                var attrib = Column.Attributes.OfType<KeyValueCollectionAttribute>().SingleOrDefault();

                var key = FieldValue != null ? FieldValue.ToString() : string.Empty;
                if (attrib != null && attrib.Dic.ContainsKey(key))
                {
                    string value = attrib.Dic[key];
                    return FormatFieldValue(value);
                }

                return FieldValueString;
            }
        }
    }

 4、编写控件 KeyValueCollection_Edit.ascx

.ascx文件

<%@ Control Language="C#" CodeBehind="KeyValueCollection_Edit.ascx.cs" Inherits="BtmsWebApp.KeyValueCollection_Edit" %>

<asp:DropDownList ID="DropDownList1" runat="server" CssClass="droplist" />
 <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="droplist" ControlToValidate="DropDownList1" Display="Dynamic" Enabled="false" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="droplist" ControlToValidate="DropDownList1" Display="Dynamic" />

cs 文件

 public partial class KeyValueCollection_Edit : FieldTemplateUserControl
    {
        private Dictionary<string, string> _dic;

        private Dictionary<string, string> Dic
        {
            get
            {
                if (_dic == null)
                {
                    _dic = DynamicDataExtend.GetDic(Column);
                }

                return _dic;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.ToolTip = Column.GetDescription();

            if (DropDownList1.Items.Count == 0)
            {
                if ((Mode == DataBoundControlMode.Insert || Mode == DataBoundControlMode.Edit) && !Column.IsRequired && Column.GetAttribute<RequiredAttribute>() == null)
               
                {
                    DropDownList1.Items.Add(new ListItem("[没有设置]", String.Empty));
                }

                DynamicDataExtend.FillKeyValueListControl(DropDownList1, Dic);
            }
            if (this.Mode == DataBoundControlMode.Insert && this.Column.DefaultValue != null)
                DropDownList1.SelectedValue = this.Column.DefaultValue.ToString();
            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(DynamicValidator1);
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            if (Mode == DataBoundControlMode.Edit && FieldValue != null)
            {
                var key = FieldValue != null ? FieldValue.ToString() : string.Empty;
                ListItem item = DropDownList1.Items.FindByValue(key);
                if (item != null)
                {
                    DropDownList1.SelectedValue = key;
                }
            }
            else if (Mode == DataBoundControlMode.Insert && FieldValue != null)
            {
                var key = FieldValue != null ? FieldValue.ToString() : string.Empty;
                ListItem item = DropDownList1.Items.FindByValue(key);
                if (item != null)
                {
                    DropDownList1.SelectedValue = key;
                }
            }
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            string value = DropDownList1.SelectedValue;
            if (value == String.Empty)
            {
                value = null;
            }
            dictionary[Column.Name] = ConvertEditedValue(value);
        }

        public override Control DataControl
        {
            get
            {
                return DropDownList1;
            }
        }
    }

 

5、在MetaData 中的使用

 [KeyValueCollection("0-明文|1-固定密钥|2-变密钥")]

 public object MODE { get; set; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值