Create your own controls

Source: Internet
Author: User

Insus. NET has always wanted to write its own control, not a user control (UserControl). I have read a lot of related knowledge over the past few days. The first step is the most critical. Let's start practicing it.
To write custom controls, you can learn an important method:

}


In this exercise, the custom control receives a data source to enable the custom control to receive a data source when it is instantiated.

Create a class [LinkControl] In the App_Code of the website, which inherits (Inheritance) WebControl. Three namespaces are also referenced. For more information, see



Write one write-only attribute (set) and only receive values.



OK. Here we write override void Render (System. Web. UI. HtmlTextWriter writer ):



In, Row 3 of code is used to determine whether the data source is empty. If it is empty, return and skip the following Render execution.
Lines 30th and 44 will generate UL, 34th and 41 lines of code, and generate LI labels.

The custom control has been written.
Create a web page on the site and pull a PlaceHolder control in. aspx to load the custom new instance.

Prepare the data source in. aspx. cs:



In the Page_Load event of. aspx. cs, instantiate the custom control ListControl, bind the data, and load it in the PlaceHolder control:



Run and view:


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.