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:
Code
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 );
9
10
11}
12}
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 ");
5}
The running result is as follows:
Haha! This makes it much easier.
The same is true for Link settings.