The following is a fully generated basic control that needs to be created from the system. web. UI. control class is derived, and all HTML content displayed in the browser must be specified. The rendered content is specified by the render method in the control class.
Code
1 Using System. Web. UI;
2
3 Namespace Mycontrols
4 {
5 Public Class Fullyrenderedcontrol: Control
6 {
7 Private String _ Text2;
8
9 Public String Text2
10 {
11 Get { Return _ Text2 ;}
12 Set {_ Text2 = Value ;}
13 }
14
15 Protected Override Void Render (htmltextwriter writer)
16 {
17 Writer. Write (_ text2 );
18 }
19 }
20 }
Put the aboveCodeAdd the file to the app_code folder because the code in this folder will be dynamically compiled.
You can use the write method of htmltextwriter to output the specified content to the browser.
The following code shows how to use the new control generated above in the page.
Code
<% @ Page Language = " C # " %>
<% @ Register tagprefix = " Custom " Namespace = " Mycontrols " %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head ID = "Head1" Runat = "Server" >
< Title > Show fully rendered Control </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< Custom: fullyrenderedcontrol
ID = "Fullyrenderedcontrol1"
Text2 = "Hello world! "
Runat = "Server" />
</ Div >
</ Form >
</ Body >
</ Html >
The above code registers a custom control on the page. You can also register the control for the entire website in the <pages> section of the web configuration file.