通过后面的aspx代码添加css类

时间:2022-11-22 23:21:10

I am using aspx. If I have HTML as follows:

我使用aspx。如果我有如下HTML:

<div id="classMe"></div>

I am hoping to dynamically add a css class through the code behind file, ie on Page_Load. Is it possible?

我希望通过文件后面的代码(即Page_Load)动态地添加css类。是可能的吗?

6 个解决方案

#1


99  

If you want to add attributes, including the class, you need to set runat="server" on the tag.

如果要添加属性(包括类),需要在标记上设置runat="server"。

    <div id="classMe" runat="server"></div>

Then in the code-behind:

然后在后台代码:

classMe.Attributes.Add("class", "some-class")

#2


16  

If you're not using the id for anything other than code-behind reference (since .net mangles the ids), you could use a panel control and reference it in your codebehind:

如果您没有将id用于除代码后引用之外的任何内容(因为.net管理了这些id),您可以使用一个面板控件并在您的代码后引用它:

<asp:panel runat="server" id="classMe"></asp:panel>

classMe.cssClass = "someClass"

#3


3  

BtnAdd.CssClass = "BtnCss";

BtnCss should be present in your Css File.

BtnCss应该出现在您的Css文件中。

(reference of that Css File name should be added to the aspx if needed)

(如果需要,应该将Css文件名称添加到aspx中)

#4


3  

controlName.CssClass="CSS Class Name";

working example follows below

工作示例如下所示

txtBank.CssClass = "csError";

#5


1  

Syntax:

语法:

controlName.CssClass="CSS Class Name";

Example:

例子:

txtBank.CssClass = "csError";

#6


0  

Assuming your div has some CSS classes already...

假设您的div已经有一些CSS类……

<div id="classMe" CssClass="first"></div>

The following won't replace existing definitions:

以下的定义不会取代现有的定义:

ClassMe.CssClass += " second";

And if you are not sure until the very least moment...

如果你不确定,直到最不重要的时刻……

string classes = ClassMe.CssClass;
ClassMe.CssClass += (classes == String.Empty) ? "second" : " second";

#1


99  

If you want to add attributes, including the class, you need to set runat="server" on the tag.

如果要添加属性(包括类),需要在标记上设置runat="server"。

    <div id="classMe" runat="server"></div>

Then in the code-behind:

然后在后台代码:

classMe.Attributes.Add("class", "some-class")

#2


16  

If you're not using the id for anything other than code-behind reference (since .net mangles the ids), you could use a panel control and reference it in your codebehind:

如果您没有将id用于除代码后引用之外的任何内容(因为.net管理了这些id),您可以使用一个面板控件并在您的代码后引用它:

<asp:panel runat="server" id="classMe"></asp:panel>

classMe.cssClass = "someClass"

#3


3  

BtnAdd.CssClass = "BtnCss";

BtnCss should be present in your Css File.

BtnCss应该出现在您的Css文件中。

(reference of that Css File name should be added to the aspx if needed)

(如果需要,应该将Css文件名称添加到aspx中)

#4


3  

controlName.CssClass="CSS Class Name";

working example follows below

工作示例如下所示

txtBank.CssClass = "csError";

#5


1  

Syntax:

语法:

controlName.CssClass="CSS Class Name";

Example:

例子:

txtBank.CssClass = "csError";

#6


0  

Assuming your div has some CSS classes already...

假设您的div已经有一些CSS类……

<div id="classMe" CssClass="first"></div>

The following won't replace existing definitions:

以下的定义不会取代现有的定义:

ClassMe.CssClass += " second";

And if you are not sure until the very least moment...

如果你不确定,直到最不重要的时刻……

string classes = ClassMe.CssClass;
ClassMe.CssClass += (classes == String.Empty) ? "second" : " second";