ASP.NET override the Render load CSS style file and JS files (CSS switch for the skin)

The web page for the skin There are many ways, the simplest is usually switch pages CSS, and CSS usually written in an external CSS file. So in fact, it is to replace the html CSS switch in the link href path. I searched online under.

There are two general ways:

1, page put a Holder control. Then programmatically to the current user's style css link written page.

2, after reflection, individually control to set css styles.

Above all kind of trouble in two ways, the first holder need to put a control on each page, there is a similar approach to link tags added runat = server approach, more pages, are more troublesome. The second not taken into account, the performance efficiency of programming are a lot of problems.

I remember when learning the DNN, in which he discovered a modified form in the default mode of action addresses directly refer to the next. not bad:

Direct override the Render event

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);

base.Render(htmlWriter);
//当前用户选择的风格css
string css = "<link href="css url" rel="stylesheet" type="text/css">";

string html = sw.ToString() ;
int startPoint = html.IndexOf("</head>", StringComparison.CurrentCultureIgnoreCase);
if(startPoint > 0)
{
html = html.Insert(startPoint, css);
}

writer.Write(html) ;
}

Put this base class PageBase each page's. It's more convenient.

Of course, if you do not want to let each page are base class inheritance custom, you can also in HttpModule to write. It is also very convenient, a written, it is good enough Page.

Reproduced in: https: //www.cnblogs.com/Alenliu/p/4937726.html

Guess you like

Origin blog.csdn.net/weixin_33690963/article/details/93470054