Adding css class through aspx code behind

落爺英雄遲暮 提交于 2019-11-28 06:41:20

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

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

Then in the code-behind:

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

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:

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

classMe.cssClass = "someClass"
Anwar
controlName.CssClass="CSS Class Name";

working example follows below

txtBank.CssClass = "csError";
Veerendranath Darsi
BtnAdd.CssClass = "BtnCss";

BtnCss should be present in your Css File.

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

Assuming your div has some CSS classes already...

<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";
Kishor Makwana

Syntax:

controlName.CssClass="CSS Class Name";

Example:

txtBank.CssClass = "csError";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!