0

I have a css file named tablecellmembers.css (and is not yet linked in the .aspx file?) that contains:

td {
    border-collapse: collapse;
    border-top: thick;
    border-top-width: 5px;
    border-top-color: blue;
}

Then I created a table, table row, and table cell dynamically in the .cs file.

TableCell tc = new TableCell();

How can I apply that css file to my table cells?

I have tried:

tc.CssClass = "td";
tc.Attributes.Add("tablecellmembers", "td");

But the css is not being applied to the cells..

3
  • Where did TableCell come from? Commented Jan 29, 2013 at 12:49
  • In your css "td" isn't a css class name it's specifying the "td" html element. A classname would start with a "." e.g. ".myclassname". Commented Jan 29, 2013 at 12:50
  • just link your css file with your document and 'voilà' ? Commented Jan 29, 2013 at 12:52

5 Answers 5

2

In the head section of your aspx or master page, add

<link rel="stylesheet" type="text/css" href="tablecellmembers.css">

You don't need to do anything to use your td style in your code behind, as this style will be applied to all table cells (HTML td elements).

Sign up to request clarification or add additional context in comments.

Comments

1

Add a link tag to the <head> of your ASPX page (or master page if you're using one) like so:

<link rel="stylesheet" type="text/css" href="tablecellmembers.css">

You don't need to use CssClass or add any attributes as td is an element rather than a class. I recommend you read up on CSS selectors to clarify this.

In a nutshell, for the following element:

<td id="someID" class="someclass">...</td>

The following could be used in CSS to refer to it:

  • td { ... } would apply to all <td> elements
  • #someID { ... } applies to just the element with the id "someID"
  • .someclass { ... } applies to all elements (td or otherwise) with the class "someclass"

Comments

0

You have to include the reference of your css file in the aspx page within head tag (if you are not using master page) like this

<link href="your css file name" rel="stylesheet" type="text/css" />

Or if you are using a content page inherited from a master page then include above file reference at master page.

Comments

0

Using td as a style name is bad practice, since it's easily confusable for the td element. To add the stylesheet in .NET, you can do this:

    var styleSheet = new HtmlLink();
    styleSheet.Attributes.Add("rel","stylesheet");
    styleSheet.Attributes.Add("href", "tablecellmembers.css");
    Page.Header.Controls.Add(styleSheet);

Comments

0

You will need to first link the css file to the rendered aspx page:

<link rel="stylesheet" type="text/css" href="tablecellmembers.css"> 

Once this is done the style should be automatically applied to the table cells when they are rendered in the browser, you don't need to use attributes as the td selector has already linked the style to the table cell in the rendered html. Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.