[Implementing the ASP. Net Control as day4] Composite Control

Source: Internet
Author: User

A composite control is a control that can contain other sub-controls. The composite control inherits from system. Web. UI. webcontrols. compositecontrol. For example, a login or wizard control is a composite control. We often see three drop-down lists of year, month, and day as one input date on the webpage. This article uses the composite control to implement the drop-down list control of year, month, and day to demonstrate how to implement the composite control.

I,CompositecontrolCATEGORY features

The compositecontrol class is an abstract class, which is implemented as the inamingcontaner interface. The inamingcontaner interface adds the parent Control ID to the clinetid of the child control to ensure that the clientid of the control on the page is unique. The inherited compositecontrol class is generally overwritten with the createchildcontrols method. In this method, a child control is created and added to the controls set attribute. when accessing its child control, if the child control is not created, the createchildcontrols method is executed, to ensure that all child controls have been created before the controlcollection is accessed.

Ii. Date drop-down list Input Device

We inherit the compositecontrol class and name it tbdropdowndate. This control will contain three drop-down lists (dropdownlist), so we only need to create the dropdownlist sub-Control of year, month, and day in sequence in the createchildcontrols method and add it to the controls set attribute.

''' <summary>
'''Date drop-down list handler.
''' </summary>
< _
ToolboxData("<{0}:TBDropDownDate runat=server></{0}:TBDropDownDate>") _
> _
Public Class TBDropDownDate
    Inherits System.Web.UI.WebControls.CompositeControl
 
    Protected Overrides Sub CreateChildControls()
        Dim oYear As DropDownList
        Dim oMonth As DropDownList
        Dim oDay As DropDownList
        Dim N1 As Integer
 
'The annual drop-down time range is 1950-2010 (the annual time range can be set by region)
        oYear = New DropDownList
        oYear.ID = "Year"
        For N1 = 1950 To 2010
            oYear.Items.Add(N1.ToString)
        Next
Me. Controls. Add (oyear) 'to add sub-control worker
Me. Controls. Add (New literalcontrol ("year "))
 
'Month drop-down ticket area is 1-12
        oMonth = New DropDownList
        oMonth.ID = "Month"
        For N1 = 1 To 12
            oMonth.Items.Add(N1.ToString)
        Next
Me. Controls. Add (omonth) 'to add sub-control worker
Me. Controls. Add (New literalcontrol ("month "))
 
'Day drop-down list area is 1-31
        oDay = New DropDownList
        oDay.ID = "Day"
        For N1 = 1 To 12
            oDay.Items.Add(N1.ToString)
        Next
Me. Controls. Add (oday) 'to add the Sub-Control Handler
Me. Controls. Add (New literalcontrol (" "))
 
    End Sub
End Class

 

Drag tbdropdowndate to the page in the setup phase, and you will see the child control we added in the createchildcontrols method.

Run the program and check its original HTML code. Then, the clientid of the child control of the year, month, and day will be added with the ID of the parent control before the original ID, in this way, the naming rules ensure that all the controls have unique clinetid values.

<span id="TBDropDownDate1">
<select name="TBDropDownDate1$Year" id="TBDropDownDate1_Year">
 
... Omitted
 
<select name="TBDropDownDate1$Month" id="TBDropDownDate1_Month">
 
... Omitted
 
<select name="TBDropDownDate1$Day" id="TBDropDownDate1_Day">
</span>

Iii. Conclusion

We have seen simple cases of three types of server controls, but these three cases are just a simple description of the control UI, A complete control requires support for properties, methods, events, and design phases... in the subsequent articles, we will introduce these parts in detail.

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

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.