[ASP. NET Control for day14] inherits compositecontrol for Toolbar Control

Source: Internet
Author: User

Previously, we briefly introduced how to implement compositecontrol by inheriting compositecontrol. In this article, we will take the toolbar control as an example to implement a toolbar control by using compositecontrol, the tool column control contains the items attribute to describe the tool column project set. The tool column button is created according to the items attribute setting. In addition, the click event can be found that the button is pressed.

Download program code: ASP. NET Server Control-day14.rar

1. tool column project set category

The tool column contains multiple buttons. The tbtoolbaritem category is added to describe the tool column project. The tbtoolbaritem category includes three attributes: Key, text, and enabled; the tbtoolbaritemcollection is the set category of tbtoolbaritem to describe the tool column button set.

 

II. Implementation TbtoolbarWidget

Step1.New inheritance CompositecontrolOf TbtoolbarWidget

    < _
Description (". "),_
    ParseChildren(True, "Items"), _
    ToolboxData("<{0}:TBToolbar runat=server ></{0}:TBToolbar>") _
    > _
    Public Class TBToolbar
        Inherits CompositeControl
    End Class 

Step2.New ItemsAttribute, which describes the set of Tool Columns.

        ''' <summary>
'''List of Tool Columns.
        ''' </summary>
        < _
Description (" set. "),_
        PersistenceMode(PersistenceMode.InnerProperty), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
        Editor(GetType(CollectionEditor), GetType(UITypeEditor)) _
        > _
        Public ReadOnly Property Items() As TBToolbarItemCollection
            Get
                If FItems Is Nothing Then
                    FItems = New TBToolbarItemCollection()
                End If
                Return FItems
            End Get
        End Property

 

Step3.New ClickEvent

The click event is added to the tbtoolbar category. When a button is pressed, the click event is triggered. The Click Event independent variable E. Key can tell which button the user has pressed.

        ''' <summary>
'''Click event reference.
        ''' </summary>
        Public Class ClickEventArgs
            Inherits System.EventArgs
            Private FKey As String = String.Empty
 
            ''' <summary>
''' The value of marker.
            ''' </summary>
            Public Property Key() As String
                Get
                    Return FKey
                End Get
                Set(ByVal value As String)
                    FKey = value
                End Set
            End Property
        End Class
 
        ''' <summary>
''' Press the tool column to list the events triggered by events.
        ''' </summary>
        < _
Description ("press the tool column to list events triggered by events. ")_
        > _
        Public Event Click(ByVal sender As Object, ByVal e As ClickEventArgs)
 
        ''' <summary>
''' Triggers a click event.
        ''' </summary>
        Protected Overridable Sub OnClick(ByVal e As ClickEventArgs)
            RaiseEvent Click(Me, e)
        End Sub

Step 4.Create a tool column button set

Override the createchildcontrols method and create a set of buttons in the tool column according to the items attribute settings. The click events of each button are directed to the buttonclickeventhandler method to process the click actions of all buttons and trigger the click events of the tbtoolbar.

        Private Sub ButtonClickEventHandler(ByVal sender As Object, ByVal e As EventArgs)
            Dim oButton As Button
            Dim oEventArgs As ClickEventArgs
 
            oButton = CType(sender, Button)
            oEventArgs = New ClickEventArgs()
            oEventArgs.Key = oButton.ID
            OnClick(oEventArgs)
        End Sub
 
        ''' <summary>
''' Create a subcontrol worker.
        ''' </summary>
        Protected Overrides Sub CreateChildControls()
            Dim oItem As TBToolbarItem
            Dim oButton As Button
 
            For Each oItem In Me.Items
                oButton = New Button()
                oButton.Text = oItem.Text
                oButton.Enabled = oItem.Enabled
                oButton.ID = oItem.Key
                AddHandler oButton.Click, AddressOf ButtonClickEventHandler
                Me.Controls.Add(oButton)
            Next
            MyBase.CreateChildControls()
        End Sub

 

Iii. Test Procedure

Drag the tbtoolbar control on the page and set the items attributes, such as ADD, modify, and delete buttons.

Add the test code to the Click Event of the tbtoolbar control and output the E. Key that triggers the click event.

    Protected Sub TBToolbar1_Click(ByVal sender As Object, ByVal e As Bee.Web.WebControls.TBToolbar.ClickEventArgs) Handles TBToolbar1.Click
Me. response. Write (string. Format ("You pressed {0}", E. Key ))
    End Sub

Execute the program. When you press the button in the tool column, the click event is triggered and the key corresponding to the button is output.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.