Net中以编程方式添加样式表

Net中以编程方式添加样式表

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

问题描述

我想在头部中以编程方式添加StyleSheets,但我看到的一个示例似乎需要多行代码来添加一个样式表,即使我可能需要很多:



示例代码

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

我也使用 Page.Header.RenderControl()方法,但它没有工作。



我也使用了 Page.Header.InnerHtml InnerText + =< link ..../> 但是他们抛出了Literal错误,这是我认为常见的错误。



我使用这个代码:

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

private void AddStyleRange(List< Literal> cssFiles)
{
foreach(cssFiles中的文字项)
{
this.Header.Controls.Add (项目);
}
}

它起作用,但是当我更改页面停止工作。



我使用Master Page,我在 Master.cs 文件中编写这些代码,使用 this.Header 而不是 Page.Header 但是当我构建它时抛出一个错误,我不能声明这。



不应该那么难以添加很多样式。



这变得越来越复杂。

解决方案

解决方案我目前正在使用:



我创建了一个辅助类:

 使用System.Web.UI; 
using System.Web.UI.WebControls;

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 =stylesheet /> };
page.Header.Controls.Add(cssFile);
}
}
}

这个帮助类,我所要做的是:

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

所以我可以根据需要添加一行



它希望它有帮助。



它也适用于母版页和内容页面的关系。 p>

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


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:

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

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

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

I used this 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.

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.

It is getting complicated.

解决方案

Okay, here is the solution I am currently using :

I created a helper class :

using System.Web.UI;
using System.Web.UI.WebControls;

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

and then through this helper class, all I have to do is :

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);
//...

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

It also works with Masterpage and content page relationships.

Hope it helps.

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中以编程方式添加样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:40