在Asp.Net添加样式表编程 [英] Adding StyleSheets Programmatically in Asp.Net

查看:139
本文介绍了在Asp.Net添加样式表编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在头部分,但我看到似乎需要code多行的例子之一编程方式添加样式表来添加只有一个样式表,即使我可能需要大量的:

I want to add StyleSheets programmatically in the head section but one of the examples I saw seemed to need to many lines of code to add just one style sheet even though I may need a lot:

示例code:

HtmlLink css = new HtmlLink();
css.Href = "css/fancyforms.css";
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Header.Controls.Add(css);

我也用 Page.Header.RenderControl()方法,但它也不能工作。空对象错误的东西被抛出。

I also use Page.Header.RenderControl() method but it didn't work either. Object null something error was thrown.

我也用 Page.Header.InnerHtml 的InnerText + =<联系....../> 的东西,但他们扔了文字错误这是我觉得常见的错误。

I also used Page.Header.InnerHtml and InnerText += "<link .... "/> things but they threw the Literal error which is I think common error.

我用这个code:

List<Literal> cssFiles = new List<Literal>();
cssFiles.Add(new Literal() { Text = @"<link href=""" +   ResolveUrl("~/Resources/Styles/MainMaster/MainDesign.css") + @""" type=""text/css"" rel=""stylesheet"" />" });
cssFiles.Add(new Literal() { Text = @"<link href=""" + ResolveUrl("~/Resources/Styles/MainMaster/MainLayout.css") + @""" type=""text/css"" rel=""stylesheet"" />" });
AddStyleRange(cssFiles);

private void AddStyleRange(List<Literal> cssFiles)
{
   foreach (Literal item in cssFiles)
   {
     this.Header.Controls.Add(item);
   }
}

据曾在第一,但是当我改变它的网页停止工作。

It worked at first but when I change the pages it stopped working.

我使用的母版页,我对 Master.cs 写这些codeS文件,也有人推荐使用这一点。标题而不是 Page.Header 但是当我建立它抛出它说我不能宣布这样的错误。

I am using Master Page and I am writing these codes on Master.cs file and also some people recommended to use this.Header instead of Page.Header but when I built it throws an error which says I cannot declare that like this.

它不应该是很难添加很多款式。

It shouldn't be that hard to add many styles.

这是越来越复杂。

推荐答案

好吧,这是我目前使用的解决方案:

Okay, here is the solution I am currently using :

我创建了一个辅助类:

namespace BusinessLogic.Helper
{
    public class CssAdder
    {
        public static void AddCss(string path, Page page)
        {
            Literal cssFile = new Literal() { Text = @"<link href=""" + page.ResolveUrl(path) + @""" type=""text/css"" rel=""stylesheet"" />" };
            page.Header.Controls.Add(cssFile);
        }
    }
}

,然后通过这个辅助类,我所要做的就是:

CssAdder.AddCss("~/Resources/Styles/MainMaster/MainDesign.css", this.Page);
CssAdder.AddCss("~/Resources/Styles/MainMaster/MainLayout.css", this.Page);
CssAdder.AddCss("~/Resources/Styles/Controls/RightMainMenu.css", this.Page);
//...

所以我可以用简单的code的一条线路增加多达我想要的。

So I can add as much as I want with one line of simple code.

它还可以与母版和内容页之间的关系。

It also works with Masterpage and content page relationships.

希望它帮助。

P.S:我不知道这和其他解决方案之间的性能差异,但它看起来更优雅,更容易消耗。如果你知道更好的方法,请让我知道。谢谢...

P.S: I don't know the performance difference between this and other solutions but it looks more elegant and easy to consume. If you know better ways, please let me know. Thanks...

这篇关于在Asp.Net添加样式表编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆