博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net组件(1):常用标签

Posted on 2007-12-17 16:50  a-peng  阅读(1278)  评论(0编辑  收藏  举报

新建类库 WebComponent

using System;
using System.ComponentModel;
using System.Web.UI;

namespace WebComponent
{
    [DefaultPropertyAttribute(
"Name")]
    [ToolboxData(
@"<{0}:UserComponent Name='阿鹏' Age=20 runat='server'></{0}:UserComponent>")]
    
public class UserComponent : Control
    
{
        
private string _name = "阿鹏";
        
private int _age = 20;

        [CategoryAttribute(
"Appearance")]
        [DescriptionAttribute(
"获取用户年龄")]
        [BrowsableAttribute(
true)]
        [DefaultValueAttribute(
20)]
        
public int Age
        
{
            
get
            
{
                
return _age;
            }

            
set
            
{
                _age 
= value;
            }

        }


        [CategoryAttribute(
"Appearance")]
        [DescriptionAttribute(
"获取用户姓名")]
        [BrowsableAttribute(
true)]
        [DefaultValueAttribute(
"阿鹏")]
        
public string Name
        
{
            
get
            
{
                
return _name;
            }

            
set
            
{
                _name 
= value;
            }

        }


        
public UserComponent()
        
{ }

    }

}


主要说明以上使用的Attribute 标签
引入命名空间using System.ComponentModel; 组件模型

1.[DefaultPropertyAttribute("Name")] 
是设置控件默认的属性的。这里是其Name属性。就是当你选择这个控件的时候,在属性窗口中自动被选中的是Name属性。 

2.[ToolboxData(@"<{0}:UserComponent Name='阿鹏' Age=20 runat='server'></{0}:UserComponent>")]
ToolBoxData 的意思是当你将这个控件从tool box 中拖放到WEBFORM中时在aspx文件的 HTML代码中添加的对该控件的定义。
这里面的{0}表示什么意思? (控件的标记的前缀)
<%@ Register Assembly="WebComponent" Namespace="WebComponent" TagPrefix="cc1" %>
<cc1:UserComponent ID="UserComponent1" runat="server" Age="20" Name="阿鹏">
</cc1:UserComponent>
(cc1就是上面的{0}控件的标记的前缀)

3.[CategoryAttribute("Appearance")]
指定属性在属性浏览器中进行分组显示的类别。该设计时特性帮助可视化编辑器将属性进行逻辑分组。
通常分为:外观(Appearance)、行为(Behavior)、布局(Layout)、数据(Data)、操作(Action)、键盘(Key)、鼠标(Mouse)等。除此之外,读者还可以自定义分类,例如Category("ItemStyle"),表示该属性在属性浏览器中显示为ItemStyle一组

4.[DescriptionAttribute("获取用户年龄")] 在可视化编辑器显示的对该属性的描述

5.[BrowsableAttribute(true)] 是否在属性编辑器中显示

6.[DefaultValueAttribute(20)] 属性的默认值

7.[BindableAttribute(false)]
这个特性表示属性是否可以绑定一个有效数据源。通常使用布尔值进行设置,例如:Bindable(true)。
如果使用值true标记属性,表示该属性可以绑定一个有效数据源,且应引发该属性的属性更改通知;
如果属性值为false,则表示该属性不能绑定数据。

需要说明:以上标签可把Attribute去掉。系统默认会为其加上Attribute
所以我们常用 [Category("Appearance")]而不写[CategoryAttribute("Appearance")]少了挺多字不是吗.

下面介绍下一些以后将来比较重要的标签.大家可以先过目一下,以后用到时再回过来看看.
下面这些标签的运用,可以查看.组件(9)简单属性与复杂属性,将会被用到.

8.[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
说明:它表示代码生成器将序列化属性的子属性而不是它本身.
理解:就是说Button控件的Font属性中的Bold和Underline子属性
将被序列化为(Font-Underline="True" Font-Bold="True)这种形式.

9.[TypeConverter(typeof(ExpandableObjectConverter))]
说明:这个属性用作此属性所绑定到的对象的转换器的类型。用于转换的类必须从TypeConverter继承。
理解:
就是将属性 TypeConverter 类型转化为 Expandable Object 可展开的类型(指属性编辑器中可展开的意思)

[TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
理解:就是将属性 TypeConverter 类型转化为 Web Color(在属性编辑器为出现颜色选择框)

10.[NotifyParentProperty(true)]
从子属性往父级通知一个更改

11.[PersistenceMode(PersistenceMode.InnerProperty)]
说明:PersistenceMode 指定如何将服务器控件属性或事件保持到ASP.NET页的元数据属性
理解:InnerProperty就是让某个控件属性能够具有嵌套的功能.

12.[ParseChildren(true)]
说明:ParseChildren
使用该特性指示当在页上以声明方式使用控件时,
嵌套在服务器控件标记内的XML元素是应该视为属性还是应视为子控件。
通常情况下,包含两种声明方式:
ParseChildren(true),表示将子XML元素作为服务器控件的属性分析
ParseChildren(false),表示将子XML元素作为服务器控件的子控件分析

即:用于告知页面分析器把控件标记中的内容解析为属性(true)还是子控件(false)

13.[PersistChildren(false)]
说明:该特性指示设计时是否应将服务器控件的子控件作为内部嵌套控件保持。
PersistChildren(true),则将服务器控件的子控件作为嵌套服务器控件标记保持。
PersistChildren(false),则将该控件的属性作为嵌套元素保持。

即:用于告知设计器把控件标记中的内容保存为属性(false)还是子控件(true)

一般设计具有嵌套功能的控件时.
会使用12+13
[ParseChildren(true)]
[PersistChildren(false)]
MyControl:Control