[ASP. NET Control for day16] inherit webcontrol for Toolbar Control

Source: Internet
Author: User

We have discussed "inherit from compositecontrol to implement Toolbar Control". This article will inherit webcontrol to implement Toolbar Control with the same function and implement the same control in different ways, then compare the differences between the two.

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

1. Inheritance WebcontrolImplementation TbtoolbarWidget

Step1.New inheritance WebcontrolOf TbtoolbarWidget

Added the tbtoolbar control that inherits webcontrol. You can also directly modify the original tbtoolbar control and change the inherited object from compositecontrol to webcontrol. Add the items attribute and click event to the tbtoolbar control as before.

In addition, the tbtoolbar control must be implemented as the inamingcontainer interface, which has no special attributes or methods. The inamingcontainer interface is used to add the clickid of the parent control to the clientid of the child control, each sub-control has a unique clientid.

 

 

Step2.Create a tool column button set

Override the rendercontents method and move the code of the tool column button program created in the createchildcontrols method of the original tbtoolbar (Composite Control) to the rendercontents method.

        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>
''' Override the rendercontents method.
        ''' </summary>
        Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
            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
 
            If Me.Items.Count = 0 AndAlso Me.DesignMode Then
                oButton = New Button()
Obutton. Text = "Please set items adequacy. "
                Me.Controls.Add(oButton)
            End If
 
            MyBase.RenderContents(writer)
        End Sub

 

 

Another problem with the Code directly moved above is that the original method of using addhandler to handle button events becomes useless? Because it is not a compound control, when the current button PostBack is returned to the servo end, tbtoolbar does not create a sub-control bar in advance, so the mechanism will not find the original button, you cannot use addhandler to handle events.

AddHandler oButton.Click, AddressOf ButtonClickEventHandler
 

Step3.Processing ClickEvent

Because you cannot use addhandler to process button events, we use the page. clientscript. getpostbackeventreference method to generate the client instruction code for the PostBack action. The onclientclick of the button executes the PostBack action.

            For Each oItem In Me.Items
                oButton = New Button()
                oButton.Text = oItem.Text
                oButton.Enabled = oItem.Enabled
                oButton.ID = oItem.Key
                sScript = Me.Page.ClientScript.GetPostBackEventReference(Me, oItem.Key)
                oButton.OnClientClick = sScript
                Me.Controls.Add(oButton)
            Next

 

The HTML code output by the tbtoolar control is as follows:

<span id="TBToolbar1">
<Input type = "Submit" name = "tbtoolbar1 $ Add" value = "add" onclick = "_ dopostback ('tbtoolbar1', 'add '); "id =" tbtoolbartailadd "/>
<Input type = "Submit" name = "tbtoolbar1 $ edit" value = "modify" onclick = "_ dopostback ('tbtoolbar1', 'edit '); "id =" tbtoolbar#edit "/>
<Input type = "Submit" name = "tbtoolbar1 $ Delete" value = "except" onclick = "_ dopostback ('tbtoolbar1', 'delete '); "id =" tbtoolbarrentdelete "/>
</span>

 

To handle PostBack events on your own, you must implement the ipostbackeventhandler interface to trigger the Click Event of the tbtoolbar in the raisepostbackevent method.

    Public Class TBToolbar
        Inherits WebControl
        Implements INamingContainer
        Implements IPostBackEventHandler
 
        Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
            Dim oEventArgs As ClickEventArgs
 
            oEventArgs = New ClickEventArgs()
            oEventArgs.Key = eventArgument
            Me.OnClick(oEventArgs)
        End Sub
 
    End Class

Ii. Test Procedure

Place the tbtoolbar control on the test page and write the test program code in 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("Toolbar1 Click - " & e.Key)
    End Sub

 

When we press the button on the tool column, the corresponding cliek event will be triggered.

 

Note: This article is also published in "the first it state help tie Ren Competition". If you think this article is helpful to you, remember to repeat it to improve your popularity ^
Http://ithelp.ithome.com.tw/question/10012507

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.