"ASP. net2.0 secrets" Reading Notes -- build a fully-generated control

Source: Internet
Author: User
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.

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.