Background dynamic loading file code:
Assume CSS file: testcss.css #region Dynamically load CSS file public void Addcss () { HtmlGenericControl _cssfile = new HtmlGenericControl ("link"); _cssfile.id = "Cssfile"; _cssfile.attributes["rel"] = "stylesheet"; _cssfile.attributes["type"] = "text/css"; _cssfile.attributes["href"] = "/styles/testcss.css"; if (this. FindControl (_cssfile.id) = = null) {this . PAGE.HEADER.CONTROLS.ADD (_cssfile); } } #endregion dynamically loading CSS files
Skin Changing Solutions
1) Write a class (Page_parent.cs) dynamic load style file
2) All pages inherit Page_parent.cs class
Page_parent.cs class
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI.HtmlControls; Namespace test{public class Page_Parent:System.Web.UI.Page {public page_parent () {this. Load + = Page_parent_load; This. Error + = Page_parent_error; }///<summary>///Catch an unhandled page error///</summary>//<param name= "Sender" ></ param>//<param name= "E" ></param> private void Page_parent_error (object sender, EventArgs E ) {throw new NotImplementedException (); } private void Page_parent_load (object sender, EventArgs e) {addcss (); }//Assume CSS file: testcss.css #region dynamic load css file public void Addcss () {HtmlGenericControl _cssfile = new HtmlGenericControl ("link"); _cssfile.id = "Cssfile"; _cssfile.attributes["rel"] = "stylesheet"; _cssfile. attributes["type"] = "text/css"; _cssfile.attributes["href"] = "/styles/testcss.css"; if (this. FindControl (_cssfile.id) = = null) {this. PAGE.HEADER.CONTROLS.ADD (_cssfile); }} #endregion load css file dynamically}}
Test page web_test.aspx:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Web_Test.aspx.cs" inherits= "Web.web_test"%><! DOCTYPE html>Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;namespace web{public partial class web_test:page_parent { protected void Page _load (object sender, EventArgs e) { }}}
. NET Web Backend dynamically loading CSS, JS files, skin-changing solutions