自定义Attribute简例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Reflection;
namespace WebComTest
{
[DefaultProperty("Num")]
[ToolboxData("<{0}:CustomPropertyControl runat=server></{0}:CustomPropertyControl>")]
public class CustomPropertyControl : WebControl
{
TextBox tb;
int intNum = 0;
[Category("Appearance")]
[NumValidate(0, 10)]
[Description("输入值范围(0~10)")]
public int Num
{
get
{
return intNum;
}
set
{
intNum = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
Table t = new Table();
t.CellPadding = 0;
t.CellSpacing = 0;
TableRow tr = new TableRow();
TableCell td_left = new TableCell();
tb = new TextBox();
tb.Text = this.intNum.ToString();
td_left.Controls.Add(tb);
tr.Controls.Add(td_left);
NumValidateAttribute numValidateAttribute = this.GetNumValidateArribute();
if (numValidateAttribute.ValidateResult(this.Num) == false)
{
TableCell td_right = new TableCell();
Label lb = new Label();
lb.ForeColor = System.Drawing.Color.Red;
lb.Text = "值输入范围必须在:" + numValidateAttribute.MinValue.ToString() + "~" + numValidateAttribute.MaxValue.ToString() + "之间!";
td_right.Controls.Add(lb);
tr.Controls.Add(td_right);
}
t.Controls.Add(tr);
t.RenderControl(writer);
}
private NumValidateAttribute GetNumValidateArribute()
{
System.Type type = this.GetType();//这里可根据需求修改。this可改为其它类
PropertyInfo property = type.GetProperty("Num");
object[] attrs = (object[])property.GetCustomAttributes(true);
foreach (Attribute attr in attrs)
{
if (attr is NumValidateAttribute)
{
return attr as NumValidateAttribute;
}
}
return null;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Reflection;
namespace WebComTest
{
[DefaultProperty("Num")]
[ToolboxData("<{0}:CustomPropertyControl runat=server></{0}:CustomPropertyControl>")]
public class CustomPropertyControl : WebControl
{
TextBox tb;
int intNum = 0;
[Category("Appearance")]
[NumValidate(0, 10)]
[Description("输入值范围(0~10)")]
public int Num
{
get
{
return intNum;
}
set
{
intNum = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
Table t = new Table();
t.CellPadding = 0;
t.CellSpacing = 0;
TableRow tr = new TableRow();
TableCell td_left = new TableCell();
tb = new TextBox();
tb.Text = this.intNum.ToString();
td_left.Controls.Add(tb);
tr.Controls.Add(td_left);
NumValidateAttribute numValidateAttribute = this.GetNumValidateArribute();
if (numValidateAttribute.ValidateResult(this.Num) == false)
{
TableCell td_right = new TableCell();
Label lb = new Label();
lb.ForeColor = System.Drawing.Color.Red;
lb.Text = "值输入范围必须在:" + numValidateAttribute.MinValue.ToString() + "~" + numValidateAttribute.MaxValue.ToString() + "之间!";
td_right.Controls.Add(lb);
tr.Controls.Add(td_right);
}
t.Controls.Add(tr);
t.RenderControl(writer);
}
private NumValidateAttribute GetNumValidateArribute()
{
System.Type type = this.GetType();//这里可根据需求修改。this可改为其它类
PropertyInfo property = type.GetProperty("Num");
object[] attrs = (object[])property.GetCustomAttributes(true);
foreach (Attribute attr in attrs)
{
if (attr is NumValidateAttribute)
{
return attr as NumValidateAttribute;
}
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebComTest
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class NumValidateAttribute : Attribute
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="intMinValue">最小值</param>
/// <param name="intMaxValue">最大值</param>
public NumValidateAttribute(int intMinValue, int intMaxValue)
{
this.intMinValue = intMinValue;
this.intMaxValue = intMaxValue;
}
private int intMinValue;
/// <summary>
/// 最大值
/// </summary>
public int MinValue
{
get
{
return intMinValue;
}
}
private int intMaxValue;
/// <summary>
/// 最小值
/// </summary>
public int MaxValue
{
get
{
return intMaxValue;
}
}
/// <summary>
/// 执行验证
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool ValidateResult(int value)
{
if (this.intMinValue <= value && value <= this.intMaxValue)
{
return true;
}
else
{
return false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebComTest
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class NumValidateAttribute : Attribute
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="intMinValue">最小值</param>
/// <param name="intMaxValue">最大值</param>
public NumValidateAttribute(int intMinValue, int intMaxValue)
{
this.intMinValue = intMinValue;
this.intMaxValue = intMaxValue;
}
private int intMinValue;
/// <summary>
/// 最大值
/// </summary>
public int MinValue
{
get
{
return intMinValue;
}
}
private int intMaxValue;
/// <summary>
/// 最小值
/// </summary>
public int MaxValue
{
get
{
return intMaxValue;
}
}
/// <summary>
/// 执行验证
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool ValidateResult(int value)
{
if (this.intMinValue <= value && value <= this.intMaxValue)
{
return true;
}
else
{
return false;
}
}
}
}