Full introduction to dynamic settings in the head area on the Asp.net page

Source: Internet
Author: User
The head area of a webpage cannot use common controls, but it is sometimes important, such as title, keywords, and description. Each page may be different. How can we set them dynamically based on the content?
Method 1: What ASP can do, Asp.net can do, of course, as long as you use response for the entire page. write (). Nothing cannot be customized. Of course, it can also be "<% = A member %> ". However, it is obvious that the. NET feature cannot be used.
Method 2: Use the special attribute runat = "server" of Asp.net to set an ID for the title and make it a server variable. Then you can set its text. However, the HTML contains an ID, which is really unpleasant.
Method 3: Use the literal control, foreground:

< Head >
< ASP: literal ID = "Lt_title" Runat = "Server"   />
< ASP: literal ID = "Lt_keywords" Runat = "Server"   />
< ASP: literal ID = "Lt_descri" Runat = "Server"   />
</ Head >

Background:

Private   Void Page_load ( Object Sender, system. eventargs E)
{
Lt_title.text =   " <Title> title </title> " ;
Lt_keywords.text =   " <Meta name = \ " Keywords \ " Content = \ " Keyword \ " > " ;
Lt_descri.text =   " <Meta name = \ " Description \ " Content = \ " Description \ " > " ;
}

In this way, it is almost perfect.

Furthermore, many user controls are used on my pages, and these user controls are hierarchical, which may be determined by the sub-user controls in a user control, the hierarchy of user controls is not fixed. How can we set it dynamically?

The home page is used as a base class of the user control and a public method is added to it:

Public   Void Settitle ( String Title)
{
Setliteraltext ( " Lt_title " , String . Format ( " <Title> {0} </title> " , Title ));
}

Add a private method: Private   Void Setliteraltext ( String ID, String Text)
{
Literal lt =   Null ;
Control CTRL =   This ;
Do
{
CTRL = CTRL. parent;
} While (CTRL ! =   Null   && CTRL. GetType (). fullname ! =   " System. Web. UI. htmlcontrols. htmlform " );

If (CTRL ! =   Null )
{
Lt = CTRL. findcontrol (ID) As Literal;
If (LT ! =   Null )
Lt. Text = Text;
}
}

In this way, your user control only needs to let it inherit from this base class, and then calls base when you want to set the page title. settitle ("title. For other headers, the principle is the same as setting the title.

Related Article

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.