C# 如何从后面的代码向asp.net添加CSS类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12196112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:25:51  来源:igfitidea点击:

How to add CSS class to asp.net from code behind

c#asp.nethtmlcss

提问by Aiden Strydom

this is really frustrating and have search a dozen sites:

这真的很令人沮丧,并搜索了十几个网站:

I have a <asp:table id="questionsTable" runat="server">to which i dynamically add rows and columns. I am trying to add a predefined Cssclass to these newly created rows and columns, but to no avail.

我有一个<asp:table id="questionsTable" runat="server">动态添加行和列的对象。我正在尝试向这些新创建的行和列添加预定义的 Cssclass,但无济于事。

The setup code for the rows and columns:

行和列的设置代码:

TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
TableCell cell5 = new TableCell();

I know i can do this:

我知道我可以这样做:

row.Style.Add("width", "80%");
row.Style.Add("text-align", "left");

cell1.Style.Add("width", "10px");
cell2.Style.Add("width", "auto");
cell3.Style.Add("width", "75px");
cell4.Style.Add("width", "75px");
cell5.Style.Add("width", "75px");

And it works... but it makes the code-behind file messy.

它有效......但它使代码隐藏文件变得混乱。

So i've seen this:

所以我看到了这个:

row.Attributes.Add("Class", "rowA");

//CSS - in StyleSheet.css
.rowA
{
    width:80%;
    text-align:center;
    background-color:#FCF6CF;
}

But it does not seem to be working for me....

但它似乎对我不起作用......

But strangely enough if i look at the markup source generated i get this

但奇怪的是,如果我查看生成的标记源,我会得到这个

    </tr><tr Class="rowA">

The above is copied from the rendered page - yet the Css is not being applied... I know the css is correct because if i add it manually it applies it correctly.

以上内容是从渲染页面复制的 - 但并未应用 Css... 我知道 css 是正确的,因为如果我手动添加它,它会正确应用它。

EDIT

编辑

To all who assisted in this thank you very much. Unfortunately something went wrong and the link to the external stylesheet got deleted. Kudos to Sven for thinking of that. After a long day such as today i to can make beginner mistakes.

非常感谢所有为此提供帮助的人。不幸的是出了点问题,外部样式表的链接被删除了。感谢 Sven 想到这一点。经过漫长的一天,例如今天,我可能会犯初学者错误。

thank you once again

再一次谢谢你

Kind regards

亲切的问候

Aiden

艾登

采纳答案by Sven Bieder

When the generated html output of the row has the class, then there is the css file not linked in the html file, or you have a typo in the class name.

当生成的行的html输出有class时,那么就是html文件中没有链接的css文件,或者你的class名有错别字。

回答by James

Your classattribute should be lowercase (XHTML):

您的class属性应为小写 (XHTML):

row.Attributes.Add("class", "rowA");

Also make sure your including the CSS file in the page your rendering your table.

还要确保在呈现表格的页面中包含 CSS 文件。

回答by nunespascal

There is nothing wrong with your code behind. Its your CSS that is wrong.

你的代码背后没有任何问题。它是你的 CSS 是错误的。

Refer the W3 docs on tables:

请参阅有关表格W3 文档

the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table's width, the width of the columns, and borders or cell spacing.

表格的水平布局不依赖于单元格的内容;它仅取决于表格的宽度、列的宽度以及边框或单元格间距。

if you want the rows to have 80% width, you should set the table width to 80%. Like this:

如果您希望行具有 80% 的宽度,则应将表格宽度设置为 80%。像这样:

<asp:table id="questionsTable" runat="server" class="myTable">

.rowA
{
   text-align:center;
   background-color:#FCF6CF;
}

.myTable{
   width:80%;
}