Dynamically set some headers on the page

Source: Internet
Author: User

I have studied how to dynamically set the Mate of the Head part of the page. Because my memory is not very good, I decided to record it for emergency purposes.

The web form of asp.net provides an HtmlHead class. With this class, we can add Meta and Link to the Head part of the page. We can do this.

Protected void Page_Load (object sender, EventArgs e)


HtmlMeta hm = new HtmlMeta ();

Hm. Name = "description ";

Hm. Content = "Bu junsheng's blog ";

Page. Header. Controls. Add (hm );


Note: HtmlMeta is in the System. Web. UI. HtmlControls namespace, so you need to import the namespace.

Running result

Of course, we can also encapsulate the above code into a method in a separate class, and then call this method, which is more conducive to reuse and more convenient to use.

The Code is as follows:


1 public class Header

2 {

3 public void SetMeta (Page p, string name, string content)

4 {

5 HtmlMeta hm = new HtmlMeta ();

6 hm. Name = name;

7 hm. Content = content;

8 p. Header. Controls. Add (hm );





The System must be imported into the header class. web. UI and System. web. UI. htmlControls namespace, because the parameter p in the SetMeta method is a Page type, which is located in System. web. UI, HtmlMeta, which is located in System. web. UI. htmlControls.

In the SetMeta method, the parameter p is the Page type, which indicates the Page object that calls this method. The parameter name and content are the values of the HtmlMeta name and content attribute. The code for calling this method is as follows:

1 protected void Page_Load (object sender, EventArgs e)

2 {

3 Header h = new Header ();

4 h. SetMeth (this, "deacription", "Bu junsheng's blog ");


The running result is as follows:

Haha! This makes it much easier.

The same is true for Link settings.

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.