How to add CSS class to from code behind [closed]

Tags: c#,,html,css

Problem :

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

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.


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


Solution :

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.

    CSS Howto..

    How do I get this div to cover flash control (jw player)?

    How to link to a “tab” on my website

    How to override priority in CSS classes?

    How to add CSS reference link to local HTML files in folder?

    How to return WebElement having particular CSS property using JavascriptExecutor?

    How to find equivalent property in Mozilla of given Webkit CSS property

    How are the invalid CSS lines interpreted?

    How can I make an image carousel with only CSS?

    how to place one div on top of another, when both are centered using auto margin?

    How to copy this semi-static menu?

    How to fit inner div inside an outer div

    How can I have a responsive height with absolutley position child elements

    How should I crop the image at client side using jcrop and upload it?

    How to version files in the section?

    What's the best way to show (un/ordered) HTML lists as a top-down tree?

    How can I center my text on top of an image, given that it's responsive?

    How can I get this anchor link to look like a button

    How to add an icon to bottom right of textareas via CSS?

    How to apply css on one html file only

    This should be simple but want to do it right. CSS display:none; how to turn back on according to @media

    IE11 shows every display: table-cell element at a new line

    How to enlarge an image horizontally in css?

    How to reffer in CSS to an input which has class attached to?

    css transition effects on show/hide element using css3 [duplicate]

    How do I implement responsive design into my CSS when I have two spans, side by side?

    How create a grid in which a variable number of squares automatically resize to fit a space?

    Phonegap : how Control the font size of the Mobile?

    How do I modify the bootstrap carousel javascript file so that it can trigger this function?

    how to position links at the bottom in a fixed height list

    How to hide the rest of the contents using simple CSS and Javascript