[Implementing the ASP. Net Control as day17] Set attributes include members of different types

Source: Internet
Author: User

We know that columns set attributes in the gridview can contain fields of different types, such as boundfield, checkboxfield, hyperlinkfield... and other fields of different types. If we want the tool column to contain more than buttons and other child controls of different types, what should we do? In this article, the tbtoolbar control in the previous article is used as a case, so that the items set attributes can be added to different child controls such as button, Textbox, label.

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

1. Set members of different types

Our requirement is that the tool columns can be added with three sub-controls: button, Textbox, and label. Therefore, we inherit the original tbtoolbaritem (only the enabled attribute is retained ), three categories are added: tbtoolbarbutton, tbtoolbartextbox, and tbtoolbarlabel.

 

These new member classes are inherited to tbtoolbaritem. Therefore, when you manually enter items members in the aspx program code, these defined member types are listed.

 

2. Create child controls for members of different types of Sets

Because the members of the items attribute have different types, We need to rewrite the rendercontents method and determine the member type to create a child control of the corresponding type. If it is tbtoolbarbutton, a button control is created. If it is tbtoolbartextbox, A Textbox Control is created. If it is tbtoolbarlabel, a label control is created. The control created by tbtoolbarbutton is tbbutton. This control is a button control with inquiry information that we implement in [ASP. NET control implement day3] extend existing Server control function.

        ''' <summary>
''' Override the rendercontents method.
        ''' </summary>
        Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim oItem As TBToolbarItem
            Dim oControl As Control
 
            For Each oItem In Me.Items
                If TypeOf oItem Is TBToolbarButton Then
'Create a Button Control Handler
                    oControl = CreateToolbarButton(CType(oItem, TBToolbarButton))
                ElseIf TypeOf oItem Is TBToolbarTextbox Then
'Create a Textbox Control struct
                    oControl = CreateToolbarTextbox(CType(oItem, TBToolbarTextbox))
                Else
'Create a label control role
                    oControl = CreateToolbarLabel(CType(oItem, TBToolbarLabel))
                End If
                Me.Controls.Add(oControl)
            Next
 
            MyBase.RenderContents(writer)
        End Sub
 
        ''' <summary>
'''Create a tool column and press confirm.
        ''' </summary>
        Private Function CreateToolbarButton(ByVal Item As TBToolbarButton) As Control
            Dim oButton As TBButton
            Dim sScript As String
 
            oButton = New TBButton()
            oButton.Text = Item.Text
            oButton.Enabled = Item.Enabled
            oButton.ID = Item.Key
            oButton.ConfirmMessage = Item.ConfirmMessage
            sScript = Me.Page.ClientScript.GetPostBackEventReference(Me, Item.Key)
            oButton.OnClientClick = sScript
 
            Return oButton
        End Function
 
        ''' <summary>
''' Create a text box for the tool column.
        ''' </summary>
        Private Function CreateToolbarTextbox(ByVal Item As TBToolbarTextbox) As Control
            Dim oTextBox As TextBox
 
            oTextBox = New TextBox
            Return oTextBox
        End Function
 
        ''' <summary>
''' Create a tool column label.
        ''' </summary>
        Private Function CreateToolbarLabel(ByVal Item As TBToolbarLabel) As Control
            Dim oLabel As Label
 
            oLabel = New Label()
            oLabel.Text = Item.Text
            Return oLabel
        End Function

 

When we manually enter different types of members in the aspx program code, the tbtoolbar control will display the corresponding child control.

 

Iii. execution procedure

Execute the program and you will see the rendered tool column in the browser. When you press "delete", the inquiry message we defined will also appear.

 

The output HTML code 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 = "Divide division" onclick = "If (confirm ('Are you sure you want to divide? ') = False) {return false; }__ dopostback ('tbtoolbar1', 'delete'); "id =" tbtoolbar1_delete "/>
<Span> keywords </span>
<input name="TBToolbar1$ctl01" type="text" />
<Input type = "Submit" name = "tbtoolbar1 $ search" value = "search criteria" onclick = "_ dopostback ('tbtoolbar1', 'search '); "id =" tbtoolbarshortsearch "/>
</span>

 

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/10012600

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.