Asp. NET Dynamic Settings page Title method Detailed _ Practical skills

Source: Internet
Author: User

Asp. NET provides us with a control class: System.Web.UI.HtmlControls.HtmlGenericControl. It can implement an instance of an HTML element, such as controlling the <td> element in ASPX in the. CS code (note that it is not <ASP:TableCell>). We know that the title of the page is contained in <TITLE></TITLE>, and that <TITLE> is also an element of HTML, so We can use System.Web.UI.HtmlControls.HtmlGenericControl to control <TITLE>.
Asp. NET, If you want to be in. cs controls an element of ASPX (whether the element is a Web control or an HTML control), the Runat property of the element must be set to server, that is, only the element's Runat property is set to server, and the element can be controlled in. CS (of course, this element must be set ID's).

Now let's try, in the HTML code for the ASPX file, change the <TITLE> element to:

Copy Code code as follows:

<title runat= "Server" id= "Titlecontrol" >default title</title>

Note Be sure to set the runat= "server" and ID (ID is important and note case). The default title here is the defaults, and when you do not change the title, it displays default title.
Go to the. cs file, declare a variable Titlecontrol, type System.Web.UI.HtmlControls.HtmlGenericControl:
Copy Code code as follows:

protected System.Web.UI.HtmlControls.HtmlGenericControl Titlecontrol;

In this way, Titlecontrol is the <TITLE>. To change the title, you can add in the. cs method (for example: Page_Load):

titlecontrol.innertext= "I changed the title!" ”;

In fact, the System.Web.UI.HtmlControls.HtmlGenericControl control can be the performance of all HTML controls on the server side, that is to say, just in the background code (. Aspx.cs/.aspx.vb) You can declare a System.Web.UI.HtmlControls.HtmlGenericControl control object that is the same as the HTML control ID on the front end, and note that the HTML control's properties in the front-end include a runat= "server". Otherwise, the background code does not perform operations on the front-end HTML controls.

You can also add the literal control between <title></title>, the effect is the same!

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.