ASP. NET provides a control class: System. Web. UI. HtmlControls. HtmlGenericControl. It can implement an example of HTML elements, such as controlling the <td> element in aspx in. cs code (note that it is not <ASP: TableCell> ). We know that the page TITLE is included in <TITLE> </TITLE>, and <TITLE> is also an HTML element. Therefore, we can use System. web. UI. htmlControls. htmlGenericControl to control <TITLE>.
ASP. NET, if you want. an element in cs that controls aspx (whether it is a Web control or an HTML control), the runat attribute of this element must be set to server, that is, after the runat attribute of the element is set to server. in cs, this element can be controlled (of course, this element must be set to id ).
Now let's try it out. In the HTML code of the aspx file, change the <TITLE> element:
Copy codeThe Code is as follows:
<TITLE runat = "server" id = "titleControl"> Default Title </TITLE>
Be sure to set runat = "server" and id (id is important and case sensitive ). The Default Title is the Default Title. If you do not change the Title, the Default Title is displayed.
Go to the. cs file and declare a variable titleControl. The type is System. Web. UI. HtmlControls. HtmlGenericControl:
Copy codeThe Code is as follows:
Protected System. Web. UI. HtmlControls. HtmlGenericControl titleControl;
In this way, titleControl is <TITLE>. To change the title, add the following content to the. cs method (for example, Page_Load:
TitleControl. InnerText = "I changed the title !";
In fact, for System. web. UI. htmlControls. the HtmlGenericControl control can be used as the performance of all HTML controls on the server, that is, as long as the Code (. aspx. cs /. aspx. vb) declare a System with the same Id as the front-end HTML control. web. UI. htmlControls. you can use the HtmlGenericControl control object, and note that you must add a runat = "server" to the front-end HTML control attribute. Otherwise, background Code does not perform operations on front-end HTML controls.
You can also add the Literal control between <title> </title>. The effect is the same!