转:ASP.NET控件开发之属性

转自:http://blog.csdn.net/anyqu/archive/2009/09/28/4603724.aspx

一、效果图

1.简单属性

简单属性

2.下拉框属性

下拉框属性

3.颜色属性

颜色属性

4.包含属性

包含属性

5.集合属性

集合属性

编辑器

编辑器

6.日期属型

日期属性

二、程序代码

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Text;   
  5. using System.Web;   
  6. using System.Web.UI;   
  7. using System.Web.UI.WebControls;   
  8. using System.Drawing;   
  9. using System.Collections;   
  10. namespace GoldWisdom   
  11. {   
  12.        
  13.     [DefaultProperty("Text")]   
  14.     [ToolboxData("<{0}:GWEdit runat=server></{0}:GWEdit>")]   
  15.     //[ToolboxBitmap(typeof(GoldWisdom.Resources.Icon), "GWEdit.bmp")]   
  16.     //引用系统控件的图标   
  17.     [ToolboxBitmap(typeof(System.Web.UI.WebControls.TextBox))]   
  18.     //[ToolboxItem(typeof(GWToolBoxItem))]   
  19.     public class GWEdit : TextBox   
  20.     {   
  21.         [Bindable(true)]   
  22.         [Category("Appearance")]   
  23.         [DefaultValue("")]   
  24.         [Localizable(true)]   
  25.         public override string Text   
  26.         {   
  27.             get  
  28.             {   
  29.                 String s = (String)ViewState["Text"];   
  30.                 return ((s == null) ? String.Empty : s);   
  31.             }   
  32.             set  
  33.             {   
  34.                 ViewState["Text"] = value;   
  35.             }   
  36.         }   
  37.         //--------------------------------以下学习属性------------------------//   
  38.         //----------1.简单属性------------//   
  39.         //字符串   
  40.         [Browsable(true)]   
  41.         //指定属性是否应该在属性窗口中显示,使用布尔值设置。一般情况下,对于常用的和比较重要的属性设置Browsable为true,否则,设置Browsable为false。   
  42.         [Category("扩展属性")]   
  43.         //指定属性在属性浏览器中进行分组显示的类别。该设计时特性帮助可视化编辑器将属性进行逻辑分组。通常分为:外观(Appearance)、行为(Behavior)、布局(Layout)、数据(Data)、操作(Action)、键盘(Key)和鼠标(Mouse)等。如果您安装的是中文版的IDE,则默认情况下中文分类和英文分类是通用的,即设置成“数据”或“Data”类别是等价的。   
  44.         [Description("简单属性的描述")]   
  45.         //设置显示在属性窗口最下面的描述属性功能的文字说明。   
  46.         [DesignOnly(false)]   
  47.         //如果此属性设置为true,表示该属性只能在设计期间使用,不能在页面代码中设置其值。   
  48.         [ReadOnly(false)]   
  49.         //设置该属性是否为只读状态。如果此特性设置为true,则在属性窗口能看到属性,但不能设置其值。另外,通过在属性语句体中把 set 语句段去掉也可以起到相同的效果。   
  50.          [DefaultValue("默认值")]   
  51.         //指定属性的默认值。此特性的设置需要特别谨慎,假如设置的值不为空,则开发人员在使用时如果自己输入的值与默认值相同,则控件不会装载开发人员输入的值。也就是说此默认值不能指定为具有有效意义或业务意义的实际值。一般设置为空即可。   
  52.         public string SimplePropertyString   
  53.         {   
  54.             get  
  55.             {   
  56.                 String s = (String)ViewState["SimplePropertyString"];   
  57.                 return ((s == null) ? String.Empty : s);   
  58.             }   
  59.             set  
  60.             {   
  61.                 ViewState["SimplePropertyString"] = value;   
  62.             }   
  63.         }   
  64.         //----------2.下拉框属性------------//   
  65.         public enum SexType  { 男 =0, 女 =1,人妖=2};   
  66.         public SexType st;   
  67.         [Browsable(true)]   
  68.         [Category("扩展属性")]   
  69.         [Description("布尔属性的描述")]   
  70.         public SexType Sex   
  71.         {   
  72.             get  
  73.             {   
  74.                 return st;   
  75.             }   
  76.             set  
  77.             {   
  78.                 st = value;   
  79.             }   
  80.         }   
  81.         //----------3.颜色属性------------//   
  82.         private System.Drawing.Color _color;   
  83.         [Bindable(true),   
  84.         Category("扩展属性"),   
  85.         DefaultValue("Red"),   
  86.         Description("按钮背景颜色")]   
  87.         public System.Drawing.Color Color   
  88.         {   
  89.             get  
  90.             {   
  91.                 return _color;   
  92.             }   
  93.             set  
  94.             {   
  95.                 _color = value;   
  96.             }   
  97.         }   
  98.         //----------4.包含子元素属性(如FONT属性)------------//   
  99.         private  Company _company =new Company();   
  100.         //_company一定要实例化,否则属性窗口只显示一个输入框   
  101.         [Browsable(true),   
  102.         Category("扩展属性"),   
  103.         DefaultValue("Red"),   
  104.         Description("折叠属性描述")]   
  105.         [TypeConverter(typeof(ExpandableObjectConverter))]   
  106.         //指定属性设计器当前属性为折叠形式的   
  107.         public Company CompanyInfo   
  108.         {   
  109.             get { return _company; }   
  110.         }   
  111.         //----------5.集合属性(如DropDownList Item 项 属性)------------//   
  112.         private List<Dept> _deptlist;   
  113.         [Browsable(true),   
  114.         Category("扩展属性"),   
  115.         Description("集合属性描述")]   
  116.            
  117.         //定义集合属性   
  118.         [PersistenceMode(PersistenceMode.InnerDefaultProperty)]   
  119.         //指定集合属性为内部默认属性;   
  120.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]   
  121.         //指定要序列化的是集合属性的内容,而不是集合属性本身(对Dept序列化,而不是DeptList)   
  122.         [NotifyParentProperty(true)]   
  123.         //指定集合属性的子属性修改时会通知父属性(这个在上一个属性类型中提到过)   
  124.         [Editor(typeof(DeptCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]   
  125.         public List<Dept> DeptList   
  126.         {   
  127.             get  
  128.             {   
  129.                 if (_deptlist == null)   
  130.                 {   
  131.                     _deptlist = new List<Dept>();   
  132.                 }   
  133.                 return _deptlist;   
  134.             }   
  135.         }   
  136.         //----------6.日期属性------------//   
  137.         private System.DateTime _date;   
  138.         [Bindable(true),   
  139.         Category("扩展属性"),   
  140.         //DefaultValue(System.DateTime.Now.ToString("yyyy-MM-dd")),   
  141.         Description("按钮背景颜色")]   
  142.         public System.DateTime CurrentDate   
  143.         {   
  144.             get  
  145.             {   
  146.                 return _date;   
  147.             }   
  148.             set  
  149.             {   
  150.                 _date = value;   
  151.             }   
  152.         }   
  153.         protected override void RenderContents(HtmlTextWriter output)   
  154.         {   
  155.             output.Write(Text);   
  156.         }   
  157.     }   
  158. }  

 

集合项类

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.ComponentModel;   
  5. namespace GoldWisdom   
  6. {   
  7.     public  class Dept   
  8.     {   
  9.         private string _id;   
  10.         private string _name;   
  11.         public Dept()   
  12.         {   
  13.             this._name = Name;   
  14.             this._id = ID;   
  15.         }   
  16.            
  17.         [Category("扩展属性")]   
  18.         [DefaultValue("")]   
  19.         [Description("子项文本")]   
  20.         [NotifyParentProperty(true)]   
  21.         public string ID   
  22.         {   
  23.             get { return _id; }   
  24.             set { _id= value; }   
  25.         }   
  26.         [Category("扩展属性")]   
  27.         [DefaultValue("")]   
  28.         [Description("子项文本")]   
  29.         [NotifyParentProperty(true)]   
  30.         public string Name   
  31.         {   
  32.             get { return _name; }   
  33.             set { _name = value; }   
  34.         }   
  35.     }   
  36. }  

 

用于编辑集合项的编辑器

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.ComponentModel.Design;   
  5. namespace GoldWisdom   
  6. {   
  7.     class DeptCollectionEditor : CollectionEditor   
  8.     {   
  9.          public DeptCollectionEditor(Type type): base(type)   
  10.         {   
  11.         }   
  12.         //一次可否选择多项   
  13.         protected override bool CanSelectMultipleInstances()   
  14.         {   
  15.             return false;   
  16.         }   
  17.         //获取此集合包含的数据类型   
  18.         protected override Type CreateCollectionItemType()   
  19.         {   
  20.             return typeof(Dept);   
  21.         }   
  22.     }   
  23. }  

 

落下了个图像属性,今天补上:

 

  1. private System.Drawing.Image img;   
  2.         [Browsable(true)]   
  3.         [Category("基本设置")]   
  4.         [DefaultValueAttribute(null)]//如不加此句,属性值可能会不能被清除   
  5.         [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]    
  6.         public System.Drawing.Image BgGround   
  7.         {   
  8.             get { return img; }   
  9.             set { img = value; }   
  10.         }  

 

如不加编辑器元数据属性,则点选属性时,为打开文件框效果:

未加编辑器效果图

加入默认编辑器效果图

默认编辑器效果图

--------------------------------------------------------------------------------------------------------------

今天看到URL属性,找到本文前,总觉得自己在哪里写过!不过这里却没有:在此补上

 

  1. [Bindable(true)]   
  2.         [Category("Appearence")]   
  3.         [DefaultValue("")]   
  4.         [Editor( typeof( System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))]    
  5.         public string ImageUrl     
  6.         {   
  7.              get  
  8.              {   
  9.                  String s = (String)ViewState["ImageUrl"];   
  10.                  return ((s == null) ? String.Empty : s);   
  11.              }   
  12.              set  
  13.              {   
  14.                  ViewState["ImageUrl"] = value;   
  15.              }    
  16.         }    

 

主要是: [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控件和组件开发技术》

在此致谢!

 

posted @ 2010-11-18 15:49  唐瑭  阅读(425)  评论(0编辑  收藏  举报