Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add CSS class to asp.net from code behind [closed]

Tags:

html

c#

css

asp.net

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.

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.

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.

thank you once again

Kind regards

Aiden

like image 491
Aiden Strydom Avatar asked Dec 12 '22 23:12

Aiden Strydom


2 Answers

Your class attribute should be lowercase (XHTML):

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

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

like image 195
James Avatar answered Jan 06 '23 20:01

James


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.

like image 43
Sven Bieder Avatar answered Jan 06 '23 20:01

Sven Bieder