Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. reflection;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Using system. Web. UI;
Using system. Windows. forms;
Namespace customcontrol
{
Public class custompropertycontrol: webcontrol
{
System. Web. UI. webcontrols. textbox TB;
Int intnum = 0;
[Category ("appearance")]
[Numvalidate (0, 10)]
[Description ("input value range (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 system. Web. UI. webcontrols. Textbox ();
TB. Text = This. intnum. tostring ();
Td_left.controls.add (TB );
Tr. Controls. Add (td_left );
Numvalidateattribute = This. getnumvalidatearribute ();
If (numvalidateattribute. validateresult (this. Num) = false)
{
Tablecell td_right = new tablecell ();
System. Web. UI. webcontrols. Label lB = new system. Web. UI. webcontrols. Label ();
LB. forecolor = system. Drawing. color. Red;
LB. Text = "value input range must be:" + 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 ();
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;
}
}
[Attributeusage (attributetargets. Property, allowmultiple = true, inherited = true)]
Public class numvalidateattribute: attribute
{
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "intminvalue"> Minimum value </param>
/// <Param name = "intmaxvalue"> maximum value </param>
Public numvalidateattribute (INT intminvalue, int intmaxvalue)
{
This. intminvalue = intminvalue;
This. intmaxvalue = intmaxvalue;
}
Private int intminvalue;
/// <Summary>
/// Maximum value
/// </Summary>
Public int minvalue
{
Get
{
Return intminvalue;
}
}
Private int intmaxvalue;
/// <Summary>
/// Minimum value
/// </Summary>
Public int maxvalue
{
Get
{
Return intmaxvalue;
}
}
/// <Summary>
/// Perform verification
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public bool validateresult (INT value)
{
If (this. intminvalue <= Value & value <= This. intmaxvalue)
{
Return true;
}
Else
{
Return false;
}
}
}
}