开发者

ASP.NET VB code behind html table forecolor

Sorry i'm new to .net and learning so this may be very simple.

I have a page with a html table on and am accessing it from code behind and changing what is displayed depending on values from a database, I have managed to change what te开发者_高级运维xt is displayed and the background color but i'm struggling to change either a rows or a cells font/fore color.

I know my code probably isn't the most efficient way of doing this but as i said i am learning.

Thanks in advance - J.

'Iterate through the rows of the table.
    For i = 0 To Table1.Rows.Count - 1

        'Iterate through the cells of a row.
        For j = 0 To Table1.Rows(i).Cells.Count - 1

            If i = 0 Then

                If (ds.Tables.Count > 0) And (ds.Tables(0).Rows.Count > 0) Then
                    Table1.Rows(i).BgColor = "#f4f4f4"
                Else
                    Table1.Rows(i).BgColor = "#ffffff"
                End If

                If j = 0 Then
                    If (ds.Tables.Count > 0) And (ds.Tables(0).Rows.Count > 0) Then
                        Table1.Rows(i).Cells(j).InnerHtml = "Personal"
                    End If
                End If

                If j = 1 Then
                    If (ds.Tables.Count > 0) And (ds.Tables(0).Rows.Count > 0) Then
                        Table1.Rows(i).Cells(j).InnerHtml = "Section complete"
                    Else
                        Table1.Rows(i).Cells(j).InnerHtml = "Please complete this section"
                    End If
                End If

                If j = 2 Then
                    If (ds.Tables.Count > 0) And (ds.Tables(0).Rows.Count > 0) Then
                        Table1.Rows(i).Cells(j).InnerHtml = "Tick"
                    Else
                        Table1.Rows(i).Cells(j).InnerHtml = "X"
                    End If
                End If

                If j = 3 Then
                    If (ds.Tables.Count > 0) And (ds.Tables(0).Rows.Count > 0) Then
                        Table1.Rows(i).Cells(j).InnerHtml = "<input id=""Button1"" type=""button"" value=""Edit"" />"
                    Else
                        Table1.Rows(i).Cells(j).InnerHtml = "<input id=""Button1"" type=""button"" value=""Add"" />"
                    End If
                End If

            End If


You can use the CSSClass property of the Row element:

Table1.Rows(i).CssClass = "YourClassName"

or you can set the Style attribute:

Table1.Rows(i).Attributes("style") = "color:red;"


You should be able to use the ForeColor property of either the TableRow or TableCell to set the color of the text.

However, if the colours for your rows or cells are pre-defined, e.g. you might have a row that has exceeded a limit or have invalid values with a red background and white text, a much better way would be to declare a style in your css file

tr.warning td {
    background:#f00;
    color:#fff;
}

And then assign the TableRow the CssClass warning

Table1.Rows(i).CssClass = "warning"

A real advantage of this is that if you want to change the style, you only need to modify your CSS file or style declaration


You can add styles information in Style attribute of the rows or cell here it is

Table1.Rows(i).Style.Add("color", "#035E8B)

or for cells

Table1.Rows(i).Cells(j).Style.Add("color", "#035E8B)

Style Attribute


You have to implement the RowDataBound event which is fired after each row has been data-bound and then you color upon your specified conditions.

See my answer HERE

EDIT: Hey guys, look out, he said code-behind so this is preferred to be C#/VB more than CSS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜