






















支持样式类型为字符串或整型的设置,颜色设置暂不支持,不过这可以通过设置一个cssClass解决。
使用时只要设置下设置ConfigFile文件路径即可。
第一次写自定义控件,有些还需改进。
enhancedatagrid.cs
using System;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
namespace YOYOSOFT.Framework.Web.UI.Controls
{
/// <summary>
/// EnhanceDataGrid 的摘要说明。
/// </summary>
[ToolboxData("<{0}:EnhanceDataGrid runat=server></{0}:EnhanceDataGrid>")]
public class EnhanceDataGrid : System.Web.UI.WebControls.DataGrid
{
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string ConfigFile
{
get{return _configFile.Trim();}
set{_configFile = value;}
}string _configFile = string.Empty;
protected override void OnPreRender(EventArgs e)
{
if(this.ConfigFile.Length != 0)
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(this.ConfigFile);
NameValueCollection values = Globals.GetNameValues(filePath);
SetStyle(values);
}
base.OnPreRender (e);
}
private void SetStyle(NameValueCollection values)
{
if(values == null) return;
string typeName;
Type t = base.GetType();
string[] types;
for(int i=0; i<values.Count; i++)
{
typeName = values.Keys[i];
types = typeName.Split('.');
PropertyInfo parentInfo = t.GetProperty(types[0]);
if(parentInfo == null) continue;
if(types.Length >= 2)
{
PropertyInfo childInfo = parentInfo.PropertyType.GetProperty(types[1]);
SetPropertyValue(childInfo,
parentInfo.GetValue(this,null),values[i]);
}
else
{
SetPropertyValue(parentInfo,this,values[i]);
}
}
}
/// <summary>
/// 设置属性值
/// </summary>
/// <param name="p">要赋值的属性</param>
/// <param name="obj">属性所在对象</param>
/// <param name="val">属性值</param>
private void SetPropertyValue(PropertyInfo p,object obj,string val)
{
if(p != null &&
(p.PropertyType == typeof(int) ||
p.PropertyType == typeof(string)))
{
if(Globals.IsInt(val))
p.SetValue(obj,int.Parse(val),null);
else
p.SetValue(obj,val,null);
}
}
}
}
globals.cs:
XML配置文件:
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。