2011-02-03 26 views
0

對不起,我是.net和學習的新手,所以這可能非常簡單。HTML表格forecolor後面的ASP.NET VB代碼

我有一個頁面上的HTML表,並從後面的代碼訪問它,並根據數據庫中的值更改顯示內容,我設法更改顯示的文本和背景顏色,但我很掙扎更改行或單元格字體/前景色。

我知道我的代碼可能不是這樣做的最有效的方式,但正如我說我正在學習。

在此先感謝 - 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 
+0

謝謝你們,他們都做到了。 – JBoom 2011-02-03 11:56:10

回答

1

您可以使用行元素的CssClass屬性:

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

,或者您還可以設置樣式屬性:

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

您應該可以使用TableRow或TableCell的ForeColor屬性來設置顏色的文字。

但是,如果您的行或單元格的顏色是預定義的,例如,你可能已經超過極限或具有紅色背景和白色文字無效值的行,一個更好的方法是申報的風格在你的CSS文件

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

然後指定的TableRow CssClass屬性警告

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

這樣做的真正的好處是,如果你想改變風格,你只需要修改你的CSS文件或樣式聲明

1

您可以在行的樣式屬性添加樣式的信息或cell 這裏是

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

或細胞

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

Style Attribute

0

您必須實現每一行已數據綁定後觸發的RowDataBound事件,然後你的顏色在您指定條件。

見我的回答HERE

編輯:嘿,夥計們,看的出來,他說代碼隱藏因此較好是C#/ VB比CSS更多。

+0

同意,但有點不相關,因爲他仍然在從後面的代碼分配它的外觀。也許這太快了,因爲他剛開始學習.Net,但如果他更快地選擇CSS,不是缺點 – davidsleeps 2011-02-03 11:54:09