[*] Hello Snoopy

.NET and Flash Blog
写了个用一个文件定义样式的DataGrid
支持样式类型为字符串或整型的设置,颜色设置暂不支持,不过这可以通过设置一个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 == nullreturn;

            
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 == nullcontinue
                
                
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:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Resources;
using System.Xml;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace YOYOSoft.Framework.Web.UI.Controls
{
    
/// <summary>
    
/// 定义本应用程序全局设置
    
/// </summary>

    internal class Globals
    
{
        
public Globals(){}
        
/// <summary>
        
/// 取得全局配置文件中的名称键值对
        
/// </summary>
        
/// <returns></returns>

        public static NameValueCollection GetNameValues(string filePath)
        
{
            HttpContext context 
= HttpContext.Current;
            NameValueCollection values;
            
string cacheKey = "Cache-ConfigFile-" + filePath;
            
if(context.Cache[cacheKey] == null)
            
{
                CacheDependency dp 
= new CacheDependency(filePath);
                values 
= new NameValueCollection();

                XmlDocument xml 
= new XmlDocument();
                
try
                
{
                    xml.Load(filePath);
                }

                
catch
                
{
                    
return null;
                }

                XmlNodeList nodes 
= xml.SelectSingleNode("root").ChildNodes;

                
                
foreach(XmlNode node in nodes)
                
{
                    
if(node.NodeType != XmlNodeType.Comment) 
                    
{
                        values.Add(node.Attributes[
"name"].Value, 
                            node.Attributes[
"value"].Value);
                    }

                }


                context.Cache.Insert(cacheKey,values,dp,
                    DateTime.MaxValue,TimeSpan.Zero);
            }

            
return (NameValueCollection)context.Cache[cacheKey];
        }

        
/// <summary>
        
/// 判断是否为整型
        
/// </summary>
        
/// <param name="input">要判断的字符串</param>
        
/// <returns></returns>

        public static bool IsInt(string input)
        
{
            
string regexString = @"^[\d]+$";

            
return Regex.IsMatch(input, regexString);
        }


    }

}


XML配置文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
    
<!-- General -->
    
<resource name="HeaderStyle.CssClass" value="grdHeaderStyle" comment="" />
    
<resource name="EditItemStyle.CssClass" value="grdEditItemStyle">
    
</resource>
    
<resource name="AlternatingItemStyle.CssClass" value="grdAlternatingItemStyle">
    
</resource>
    
<resource name="ItemStyle.CssClass" value="grdItemStyle">
    
</resource>
    
<resource name="FooterStyle.CssClass" value="grdFooterStyle">
    
</resource>
    
<resource name="PagerStyle.CssClass" value="grdPagerStyle" comment="">
    
</resource>
    
<resource name="CssClass" value="grdGlobalStyle" comment="DataGrid">
    
</resource>
    
<resource name="CellPadding" value="4">
    
</resource>
    
<resource name="CellSpacing" value="0">
    
</resource>
</root>

posted on 2005-03-09 16:22  HelloSnoopy  阅读(644)  评论(0编辑  收藏  举报