ASP. NET dynamic page title setting method detail

  • 2020-06-15 08:01:49
  • OfStack

ASP. NET provides us with a control class: System. Web. UI. HtmlControls. HtmlGenericControl. It can implement 1 instance of an HTML element, such as controlling aspx in the.cs code < td > Element (note that it is not < ASP:TableCell > ). As we know, the page title is included in < TITLE > < /TITLE > In the, and < TITLE > Is also a HTML elements, so, we can use System. Web. UI. HtmlControls. HtmlGenericControl to control < TITLE > .
In ASP.NET, if you want to control one element of aspx in.cs (whether that element is an Web control or an HTML control), then the runat attribute of that element must be set to server, that is, only after the runat attribute of the element is set to server can you control that element in.cs (of course, this element must be set to id).

Now let's try it out in the HTML code of the aspx file < TITLE > Elements as follows:


    <TITLE runat="server" id="titleControl">Default Title</TITLE>

Note that 1 must set runat="server" and id (id is important and case sensitive). Default Title is the default title. When you do not change the title, Default Title is displayed.
Go to. cs file, declare a variable titleControl, type of System. Web. UI. HtmlControls. HtmlGenericControl:

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

So titleControl is < TITLE > . To change the heading, add the following to the.cs method (for example: Page_Load) :

titleControl.InnerText= "I've changed the title!" ;

Actually for System. Web. UI. HtmlControls. HtmlGenericControl controls can be used as all HTML control performance on the server side, that is to say as long as the code in the background (. aspx. cs /. aspx. vb) declared in a same as the front end HTML control Id System. Web. UI. HtmlControls. HtmlGenericControl can control object, Note that the properties of the front-end HTML control include the following sentence: runat="server", otherwise the background code will not perform operations on the front-end HTML control.

Can also be < title > < /title > Add Literal control between, effect 1!


Related articles: