. NET Web Backend dynamically loading CSS, JS files, skin-changing solutions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.