简体   繁体   English

从后面的代码修改HTML标记级CSS选择器

[英]Modify an HTML tag level CSS selector from the code behind

I want to be able to fully manipulate all CSS selectors (including classes and semantic tags) using the ASP.NET backend code. 我希望能够使用ASP.NET后端代码完全操纵所有CSS选择器(包括类和语义标记)。 Like so: 像这样:

CSS : CSS

html {
    color: black;
}

.myClass {
    width: 100px;
}

Desired C# : 所需的 C#

protected void Page_Load(...) {
    // Some code & if logic ...
    html.Attributes.Add("style", "color: green")
    myClass.Attributes.Add("style", "width: 325px")
}

However, the C# aspect of it I do not know what to do to access semantic level tags. 但是,它的C#方面我不知道如何访问语义级别的标签。 I am curious how to modify just the style classes from C# (ie do not mess with the ASP.NET / id's). 我很好奇如何仅从C#修改样式类(即不要与ASP.NET/id混淆)。 This is for design aspects as CSS does not have certain logic behind it and I cannot make another "html" selector in CSS or C#. 这是出于设计方面的考虑,因为CSS背后没有确定的逻辑,并且我无法在CSS或C#中创建另一个“ html”选择器。

EDIT : 编辑

Class applied to a generic div element containing other elements. 应用于包含其他元素的通用div元素的类。 runat="server" runat =“服务器”

Try this CSS parser. 试试这个 CSS解析器。 I think you will be able to accomplish your task. 我认为您将能够完成任务。 If not, it is open source, you can use it as a guide. 如果不是,它是开源的,您可以将其用作指南。

This code below will find "url('/images/logo.png')" 下面的代码将找到“ url('/ images / logo.png')”

var parser = new Parser();
var stylesheet = parser.Parse(".someClass{color: red; background-image: url('/images/logo.png')");

var imageUrl = stylesheet.Rulesets
            .SelectMany(r => r.Declarations)
            .FirstOrDefault(d => d.Name.Equals("background-image", StringComparison.InvariantCultureIgnoreCase))
            .Term
            .ToString(); 

whatever controls you want to access in the code, should be runat="server" and with id. 您想在代码中访问的任何控件都应为runat =“ server”并使用ID。

< div id="dv" runat="server">...your design in between...< /div>

from the backend , you can access it like this: 从后端,您可以像这样访问它:

dv.Attributes.Add("style", "color: green");

well, I suggest to have a css, and you can assign the class directly: 好吧,我建议有一个CSS,您可以直接分配该类:

dv.Attributes.Add("class", "myClass");

or 要么

dv.cssClass ="myClass";

Note: if you are looking for different themes can be applied on the user selection refer this link https://msdn.microsoft.com/en-us/library/ms366514.aspx http://www.c-sharpcorner.com/UploadFile/8dd3df/themes-in-Asp-Net/ 注意:如果您正在寻找可以在用户选择上应用的不同主题,请参考此链接https://msdn.microsoft.com/en-us/library/ms366514.aspx http://www.c-sharpcorner.com/ UploadFile / 8dd3df / themes-in-Asp-Net /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM