接续上篇 Attribute 的介绍,本文将再介绍一些服务器控件常用的 Attribute。

 

六、ToolboxDataAttribute 类别

作用:指定当自订控件从工具箱拖曳到页面时,为此自订控件产生的预设标记。
当我们新增一个服务器控件,它就会预设在控件类别套用 ToolboxDataAttribute,定义在控件在 aspx 程序代码中的标记。

<ToolboxData("<{0}:TBButton runat=server ></{0}:TBButton>")> _
Public Class TBButton
    Inherits System.Web.UI.WebControls.Button
End Class

 

 

七、DefaultPropertyAttribute 类别

作用:指定类别的预设属性。
下面的范例中,MyTextbox 类别套用 DefaultPropertyAttribute,设定 Text 属性为预设属性。

<DefaultProperty("Text")> _
Public Class MyTextbox
    Inherits WebControl
 
    Public Property Text() As String
        Get
            Return CType(Me.ViewState("Text"), String)
        End Get
 
        Set(ByVal value As String)
            Me.ViewState("Text") = value
        End Set
    End Property
End Class

 

八、DefaultEventAttribute 类别

作用:指定控件的预设事件。
下面的范例中,MyTextbox 类别套用 DefaultEventAttribute,设定 TextChanged 为预设属性。

<DefaultEvent("TextChanged")> _
Public Class MyTextbox
    Inherits WebControl
 
    ''' <summary>
    ''' TextChanged 事件。
    ''' </summary>
    Public Event TextChanged As EventHandler
End Class

 

当设计阶段在页面上的 MyTextbox 控件点二下时,就会产生预设事件的处理函式。

    Protected Sub MyTextbox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextbox3.TextChanged
 
    End Sub

 

九、LocalizableAttribute 类别

作用:指定属性是否应该当地语系化。
当属性套用设为为 true 的 LocalizableAttribute 时,其属性值会储存在资源档中,未来不需修改程序代码就可以将这些资源档当地语系化。

        <Localizable(True)> _
        Public Property Text() As String
            Get
                Return CType(Me.ViewState("Text"), String)
            End Get
 
            Set(ByVal value As String)
                Me.ViewState("Text") = value
            End Set
        End Property

 

 

十、DesignerAttribute 类别

作用:设定控件在设计阶段服务的类别。
指定一个设计阶段的服务类别,来管理控件在设计阶段的行为,例如控件的设计阶段外观、智能标签内容。例如下面范例的 TBGridView 控件就定义了 TBGridViewDesigner 来实作设计阶段的行为,未来的章节中也会介绍如何实作控件的 Designer。

    < Designer(GetType(TBGridViewDesigner)) > _
    Public Class TBGridView
        Inherits GridView
    End Class

 

十一、EditorAttribute 类别

作用:指定在属性窗口中编辑属性值的编辑器。
例如 ListBox 控件的 Items 属性,在属性窗口编辑 Items 属性时,会弹出 Items 集合属性的编辑器。以下范例就是定义 Items 属性的编辑器类别为 TBListItemsCollectionEditor,未来的章节中也会介绍如何实作属性的 Editor。

        <Editor(GetType(TBListItemsCollectionEditor), GetType(System.Drawing.Design.UITypeEditor))> _
        Public Overrides ReadOnly Property Items() As ListItemCollection

 

 

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

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