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; }