Summary of method examples of dynamically adding HTML elements in ASP.NET

高洛峰
Release: 2017-02-03 15:01:44
Original
1514 people have browsed it

The example in this article describes the method of dynamically adding HTML elements in ASP.NET. Share it with everyone for your reference, the details are as follows:

When using asp.net for web development, the information in the in the page can be dynamic through the cs file of asp.net specified.

1. Dynamically add style sheet

/*动态增加样式表*/
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("href", "/css/base.css");
this.Header.Controls.Add(link);
Copy after login

2. Dynamically add style

/*动态增加样式*/
Style style = new Style();
style.Font.Size = 20;
style.ForeColor = System.Drawing.Color.Navy;
style.BackColor = System.Drawing.Color.LightGray;
this.Header.StyleSheet.CreateStyleRule(style, null, "body");
Copy after login

3. Dynamically add Meta

/*动态增加Meta*/
HtmlMeta meta = new HtmlMeta();
meta.Name = "keywords";
meta.Content = "Your keywords here";
this.Header.Controls.Add(meta);
meta = new HtmlMeta();
meta.Name = "company";
meta.Content = "microsoft";
this.Header.Controls.Add(meta);
meta = new HtmlMeta();
meta.Name = "date";
meta.Content = DateTime.Now.ToString("yyyy-MM-dd");
meta.Scheme = "YYYY-MM-DD";
this.Header.Controls.Add(meta);
Copy after login

4. Dynamically add js files

/*动态增加js文件*/
HtmlGenericControl si = new HtmlGenericControl();
si.TagName = "script";
si.Attributes.Add("language", "javascript");
si.Attributes.Add("type", "text/javascript");
si.Attributes.Add("src", "/js/common/base.js");//注意路径的写法
this.Page.Header.Controls.Add(si);
Copy after login

Notes

When using the above code, the tag must be added runat="server" (server control).

<head runat="server">
</head>
Copy after login

I hope this article will be helpful to everyone in asp.net programming.

For more ASP.NET methods to dynamically add HTML elements and summary related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!