3

Here is my problem:

  • When I create my table in C#, I would like to add different CSS style to each cell.

    while (DR.Read()) {

        TableRow linha1 = new TableRow();
        cel1 = new TableCell();
        cel2 = new TableCell();
        cel3 = new TableCell();
        cel4 = new TableCell();
    
        cel1.Controls.Add(new LiteralControl(DR.GetValue(0).ToString()));
        cel2.Controls.Add(new LiteralControl(DR.GetValue(1).ToString()));
        cel3.Controls.Add(new LiteralControl(DR.GetValue(2).ToString()));
        cel4.Controls.Add(new LiteralControl(DR.GetValue(3).ToString()));
    
    
        linha1.Controls.Add(cel1);
        linha1.Controls.Add(cel2);
        linha1.Controls.Add(cel3);
        linha1.Controls.Add(cel4);
        Tab_artigos_all.Controls.Add(linha1);
    }
    
1
  • 1
    No, this isn't your problem, this is your goal. What have you tried so far? Adding styles to asp.net controls is really a trivial thing, covered by multiple articles on the internet...What exactly is your problem then? Commented Jun 11, 2012 at 9:57

3 Answers 3

9

Its actually pretty easy.

cel1.Style["CSSPROPERTY"] = "SomeValue"

or

cel1.Attributes.Add("class", "CSSCLASSNAME");

That oughtta do it

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

1 Comment

Thank you very much. " cel1.Attributes.Add("class", "CSSCLASSNAME");" - that is what i wanted to find
5

Try this

cel1.Attributes.Add("class", "className");

or

cel1.Style.Add("background-color", "red");

Comments

1

I like the answers above, for sure. But you can also use the format of

cel1.Style(HtmlTextWriterStyle.FontSize) = 9

for example. Autocomplete drop-downs will obviously give you a list of possible values to use along with HtmlTextWriterStyle if you're in Visual Studio.

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.