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