Property 与 Attribute 二个术语一般都是翻译成「属性」,例如类别的属性,是使用英文的 Property,而 HTML/XML 的元素属性,使用的英文则是 Attribute。在 .NET 中 Property 与 Attribute 的意义及用法不同,不过微软在线文件也将它翻译为「属性」,这可能让人发生困扰及误解;笔者比较喜欢的方式就是 Property 是属性,Attribute 就维持原文。在 .NET 中类别或属性上可以套用上不同的 Attribute,使类别或属性具有不同的特性,本文将介绍一些在服务器控件常使用到的 Attribute。

一、DescriptionAttribute 类别

作用:指定控件或属性的描述。
当 DescriptionAttribute 套用至控件的类别时,设定的描述内容就会出现在工具箱中控件的提示。

 

<Description("按鈕控制項")> _
Public Class TBButton
    Inherits System.Web.UI.WebControls.Button
End Class

 

image

 

当 DescriptionAttribute 套用至控件的属性时,在属性窗口下面就会出现设定的属性描述内容。

        <Description("詢問訊息")> _
        Public Property ConfirmMessage() As String

 

image

 

二、DefaultValueAttribute 类别

作用:指定属性的默认值。
使用 DefaultValueAttribute 设定属性的默认值,若设定的属性值与默认值相同时,此属性值就不会出现在 aspx 程序代码中;笔者强烈建议属性一定套用 DefaultValueAttribute,一来在 aspx 中的程序代码会比较少,另外一个重点是若因为某些因素需要修改属性的默认值时,所有已开发页面的控件属性值会一并变更;因为当初属性值是默认值,没有被写入 aspx 程序代码中,所以一但控件的属性默认值变更,页面已布属的控件的属性值就会全面适用。

        Private FConfirmMessage As String = String.Empty
 
        <DefaultValue("")> _
        Public Property ConfirmMessage() As String
            Get
                Return FConfirmMessage
            End Get
            Set(ByVal value As String)
                FConfirmMessage = value
            End Set
        End Property

 

 

三、CategoryAttribute 类别

作用:指定属性或事件的分类名称,当属性窗口设定为 [分类] 模式时,以群组方式来显示属性或事件。
例如设定 ConfirmMessage 属性在 "Behavior" 分类,则 ConfirmMessage 属性会被归类到「行为」分类。

        <Category("Behavior")> _
        Public Property ConfirmMessage() As String

 

image

 

四、BindableAttribute 类别

作用:指定成员是否通常使用于系结。
在数据系结设定窗口中中,指定属性是否预设会出现在属性清单中。

        <Bindable(True)> _
        Public Property ConfirmMessage() As String

 

 

image

 

五、BrowsableAttribute 类别

作用:指定属性或事件是否应该在 [属性] 窗口中显示。
若属性在设计阶段无须修正,可以设定该属性不要出现在属性窗口中,此时可以在属性上套用 Browsable(False) 即可。

        <Browsable(False)> _
        Public Property ConfirmMessage() As String

 

备注:本文同步发布于「第一届iT邦帮忙铁人赛」,如果你觉得这篇文章对您有帮助,记得连上去推鉴此文增加人气 ^^
http://ithelp.ithome.com.tw/question/10012016

posted on 2008-10-14 07:17  jeff377  阅读(483)  评论(0编辑  收藏  举报