Home >Backend Development >C#.Net Tutorial >Summary of method examples of dynamically adding HTML elements in ASP.NET

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

高洛峰
高洛峰Original
2017-02-03 15:01:441599browse

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 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 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);

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");

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);

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);

Notes

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

<head runat="server">
</head>

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!

Statement:
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