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: