转:ASP.NET控件开发之属性
转自:http://blog.csdn.net/anyqu/archive/2009/09/28/4603724.aspx
一、效果图
1.简单属性
2.下拉框属性
3.颜色属性
4.包含属性
5.集合属性
编辑器
6.日期属型
二、程序代码
- 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.Drawing;
- using System.Collections;
- namespace GoldWisdom
- {
- [DefaultProperty("Text")]
- [ToolboxData("<{0}:GWEdit runat=server></{0}:GWEdit>")]
- //[ToolboxBitmap(typeof(GoldWisdom.Resources.Icon), "GWEdit.bmp")]
- //引用系统控件的图标
- [ToolboxBitmap(typeof(System.Web.UI.WebControls.TextBox))]
- //[ToolboxItem(typeof(GWToolBoxItem))]
- public class GWEdit : TextBox
- {
- [Bindable(true)]
- [Category("Appearance")]
- [DefaultValue("")]
- [Localizable(true)]
- public override string Text
- {
- get
- {
- String s = (String)ViewState["Text"];
- return ((s == null) ? String.Empty : s);
- }
- set
- {
- ViewState["Text"] = value;
- }
- }
- //--------------------------------以下学习属性------------------------//
- //----------1.简单属性------------//
- //字符串
- [Browsable(true)]
- //指定属性是否应该在属性窗口中显示,使用布尔值设置。一般情况下,对于常用的和比较重要的属性设置Browsable为true,否则,设置Browsable为false。
- [Category("扩展属性")]
- //指定属性在属性浏览器中进行分组显示的类别。该设计时特性帮助可视化编辑器将属性进行逻辑分组。通常分为:外观(Appearance)、行为(Behavior)、布局(Layout)、数据(Data)、操作(Action)、键盘(Key)和鼠标(Mouse)等。如果您安装的是中文版的IDE,则默认情况下中文分类和英文分类是通用的,即设置成“数据”或“Data”类别是等价的。
- [Description("简单属性的描述")]
- //设置显示在属性窗口最下面的描述属性功能的文字说明。
- [DesignOnly(false)]
- //如果此属性设置为true,表示该属性只能在设计期间使用,不能在页面代码中设置其值。
- [ReadOnly(false)]
- //设置该属性是否为只读状态。如果此特性设置为true,则在属性窗口能看到属性,但不能设置其值。另外,通过在属性语句体中把 set 语句段去掉也可以起到相同的效果。
- [DefaultValue("默认值")]
- //指定属性的默认值。此特性的设置需要特别谨慎,假如设置的值不为空,则开发人员在使用时如果自己输入的值与默认值相同,则控件不会装载开发人员输入的值。也就是说此默认值不能指定为具有有效意义或业务意义的实际值。一般设置为空即可。
- public string SimplePropertyString
- {
- get
- {
- String s = (String)ViewState["SimplePropertyString"];
- return ((s == null) ? String.Empty : s);
- }
- set
- {
- ViewState["SimplePropertyString"] = value;
- }
- }
- //----------2.下拉框属性------------//
- public enum SexType { 男 =0, 女 =1,人妖=2};
- public SexType st;
- [Browsable(true)]
- [Category("扩展属性")]
- [Description("布尔属性的描述")]
- public SexType Sex
- {
- get
- {
- return st;
- }
- set
- {
- st = value;
- }
- }
- //----------3.颜色属性------------//
- private System.Drawing.Color _color;
- [Bindable(true),
- Category("扩展属性"),
- DefaultValue("Red"),
- Description("按钮背景颜色")]
- public System.Drawing.Color Color
- {
- get
- {
- return _color;
- }
- set
- {
- _color = value;
- }
- }
- //----------4.包含子元素属性(如FONT属性)------------//
- private Company _company =new Company();
- //_company一定要实例化,否则属性窗口只显示一个输入框
- [Browsable(true),
- Category("扩展属性"),
- DefaultValue("Red"),
- Description("折叠属性描述")]
- [TypeConverter(typeof(ExpandableObjectConverter))]
- //指定属性设计器当前属性为折叠形式的
- public Company CompanyInfo
- {
- get { return _company; }
- }
- //----------5.集合属性(如DropDownList Item 项 属性)------------//
- private List<Dept> _deptlist;
- [Browsable(true),
- Category("扩展属性"),
- Description("集合属性描述")]
- //定义集合属性
- [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
- //指定集合属性为内部默认属性;
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
- //指定要序列化的是集合属性的内容,而不是集合属性本身(对Dept序列化,而不是DeptList)
- [NotifyParentProperty(true)]
- //指定集合属性的子属性修改时会通知父属性(这个在上一个属性类型中提到过)
- [Editor(typeof(DeptCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
- public List<Dept> DeptList
- {
- get
- {
- if (_deptlist == null)
- {
- _deptlist = new List<Dept>();
- }
- return _deptlist;
- }
- }
- //----------6.日期属性------------//
- private System.DateTime _date;
- [Bindable(true),
- Category("扩展属性"),
- //DefaultValue(System.DateTime.Now.ToString("yyyy-MM-dd")),
- Description("按钮背景颜色")]
- public System.DateTime CurrentDate
- {
- get
- {
- return _date;
- }
- set
- {
- _date = value;
- }
- }
- protected override void RenderContents(HtmlTextWriter output)
- {
- output.Write(Text);
- }
- }
- }
集合项类
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel;
- namespace GoldWisdom
- {
- public class Dept
- {
- private string _id;
- private string _name;
- public Dept()
- {
- this._name = Name;
- this._id = ID;
- }
- [Category("扩展属性")]
- [DefaultValue("")]
- [Description("子项文本")]
- [NotifyParentProperty(true)]
- public string ID
- {
- get { return _id; }
- set { _id= value; }
- }
- [Category("扩展属性")]
- [DefaultValue("")]
- [Description("子项文本")]
- [NotifyParentProperty(true)]
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- }
- }
用于编辑集合项的编辑器
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel.Design;
- namespace GoldWisdom
- {
- class DeptCollectionEditor : CollectionEditor
- {
- public DeptCollectionEditor(Type type): base(type)
- {
- }
- //一次可否选择多项
- protected override bool CanSelectMultipleInstances()
- {
- return false;
- }
- //获取此集合包含的数据类型
- protected override Type CreateCollectionItemType()
- {
- return typeof(Dept);
- }
- }
- }
落下了个图像属性,今天补上:
- private System.Drawing.Image img;
- [Browsable(true)]
- [Category("基本设置")]
- [DefaultValueAttribute(null)]//如不加此句,属性值可能会不能被清除
- [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
- public System.Drawing.Image BgGround
- {
- get { return img; }
- set { img = value; }
- }
如不加编辑器元数据属性,则点选属性时,为打开文件框效果:
加入默认编辑器效果图
--------------------------------------------------------------------------------------------------------------
今天看到URL属性,找到本文前,总觉得自己在哪里写过!不过这里却没有:在此补上
- [Bindable(true)]
- [Category("Appearence")]
- [DefaultValue("")]
- [Editor( typeof( System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
- public string ImageUrl
- {
- get
- {
- String s = (String)ViewState["ImageUrl"];
- return ((s == null) ? String.Empty : s);
- }
- set
- {
- ViewState["ImageUrl"] = value;
- }
- }
主要是: [Editor( typeof( System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))] 这句!
参考资料
MSDN;
http://www.cnblogs.com/Clingingboy/archive/2006/09/17/506741.html
《庖丁解牛:纵向切入Asp.net 3.5控件和组件开发技术》
在此致谢!