你可注意VS中属性栏里有如下一些属性形式,红色标记部分:
属性描述:![ASP.NET2.0自定义控件属性的写法_第1张图片](http://img.e-com-net.com/image/info8/ed5199411cc641adbb26a980ac630ea0.jpg)
一般属性:![ASP.NET2.0自定义控件属性的写法_第2张图片](http://img.e-com-net.com/image/info8/84b57ebcc13448b7a218034a4a3d3ade.jpg)
下拉列表属性:![ASP.NET2.0自定义控件属性的写法_第3张图片](http://img.e-com-net.com/image/info8/2ff089fe539f41848cbb9647badd6e5a.jpg)
树状属性:![ASP.NET2.0自定义控件属性的写法_第4张图片](http://img.e-com-net.com/image/info8/64a2b4bb0eb54117947717e22ea339be.jpg)
那么它们是怎么简单地实现的呢?答案就在下面,
1.属性描述
属性描述包含在属性定义的开头,"[.....]"一段代码中.
代码:
[
Bindable( true ), // 是否显示在属性栏中
Category( " Appearance " ), // 属性的分类,Appearance表示将此属性显示在"外观"栏中
DefaultValue( "" ), // 属性的默认值
Description( " 设置或获取BaseControl类型 " ) // 属性栏下方的描述
]
public virtual ControlType Type
![](http://img.e-com-net.com/image/info8/a7ee9f50d7f64f679945cdb1f90bd07a.gif)
... {
//属性的定义部分
}
2.一般属性
代码:
[属性描述]
public string Title
![](http://img.e-com-net.com/image/info8/a7ee9f50d7f64f679945cdb1f90bd07a.gif)
... {
get
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
object o = ViewState["Title"];
if (o == null)
return String.Empty;
return (string)o;
}
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
set ...{ ViewState["Title"] = value; }
}
如果只想让属性为只读的话,只需写get语句就行了.如果属性是只读的,它在属性栏中显示灰色.
3.下拉列表属性
此中属性要定义一个枚举类型.
代码:
public enum ControlType
![](http://img.e-com-net.com/image/info8/a7ee9f50d7f64f679945cdb1f90bd07a.gif)
... {
Label=0,
TextBox=1,
DatePicker=2
}
![](http://img.e-com-net.com/image/info8/3021fccc4e39443e82f669e757b188bf.gif)
public class BaseControl : WebControl, INamingContainer
![](http://img.e-com-net.com/image/info8/a7ee9f50d7f64f679945cdb1f90bd07a.gif)
... {
......
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
[属性描述]
public virtual ControlType Type
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
get
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
object _type = ViewState["Type"];
if (_type == null)
return ControlType.TextBox;
else
return (ControlType)_type;
}
set
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
ViewState["Type"] = value;
}
}
......
}
4.树状属性
我们先给子属性定义的类.
代码:
public class LabelTextBox : Control
![](http://img.e-com-net.com/image/info8/a7ee9f50d7f64f679945cdb1f90bd07a.gif)
... {
private SizeInfo _size;
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
[Browsable(true),
Description("获取和设置控件的大小"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
Category("Appearance")
]
public virtual SizeInfo Size
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
get
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
if (_size == null)
_size = new SizeInfo();
return _size;
}
}
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
......
}
![](http://img.e-com-net.com/image/info8/3021fccc4e39443e82f669e757b188bf.gif)
[TypeConverter( typeof (ExpandableObjectConverter))]
public class SizeInfo
![](http://img.e-com-net.com/image/info8/a7ee9f50d7f64f679945cdb1f90bd07a.gif)
... {
private int _width;
private int _height;
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
[NotifyParentProperty(true)]
public int Width
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
get ...{ return _width; }
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
set ...{ _width = value; }
}
![](http://img.e-com-net.com/image/info8/30b3da7787b24a2b9dc5b44c8de3f160.gif)
[NotifyParentProperty(true)]
public int Height
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
...{
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
get ...{ return _height; }
![](http://img.e-com-net.com/image/info8/953997d2b9a34ec38ab077e6550e3034.gif)
set ...{ _height = value; }
}
}
这里的属性用另外一种方式写的,建议还是用第一种方式写比较好.
关于属性的定义写的不够全面,论述的不够深入,由于笔者学浅,请多多提出宝贵意见.