asp:表以编程方式更改行颜色

时间:2011-02-07 20:09:36

标签: asp.net vb.net

我正在学习.net并从后面的代码构建一个表并尝试更改备用行颜色。

我有它工作但是当我想使用一个十六进制值时,他只使用Drawing.Color,有没有办法做到这一点?

以下是执行此操作的代码:

        If j Mod 2 = 1 Then
            r.BackColor = Drawing.Color.Aquamarine
            'Table1.Rows(j).Cells(i).CssClass = "odd"
        Else
            r.BackColor = Drawing.Color.Blue
            'Table1.Rows(j).Cells(i).CssClass = "even"
        End If

以下是完整代码:

    ' declare variables
    Dim numrows As Integer
    Dim numcells As Integer
    Dim i As Integer
    Dim j As Integer
    Dim r As TableRow
    Dim c As TableCell
    Dim hc As TableHeaderCell
    'Dim datepickJS As String

    ' generate rows and cells
    numrows = CaseTotal
    ' column count
    numcells = 7

    r = New TableRow()
    c = New TableCell()

    '' validation creation
    'Dim newValidatorSummary As New ValidationSummary()
    'newValidatorSummary.CssClass = "form-error"
    'c.Controls.Add(newValidatorSummary)
    'c.ColumnSpan = 5
    'r.Cells.Add(c)
    'Table1.Rows.Add(r)


    r = New TableRow()

    ' header row and titles
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl("Case Ref"))
    r.Cells.Add(hc)
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl("Name"))
    r.Cells.Add(hc)
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl("Company"))
    r.Cells.Add(hc)
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl("Last Updated"))
    r.Cells.Add(hc)
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl("Order Date"))
    r.Cells.Add(hc)
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl(" "))
    r.Cells.Add(hc)
    hc = New TableHeaderCell()
    hc.Controls.Add(New LiteralControl(" "))
    r.Cells.Add(hc)
    Table1.Rows.Add(r)

    r.BackColor = Drawing.Color.AntiqueWhite


    For j = 0 To numrows - 1

        r = New TableRow()
        'Table1.Rows(j).Cells(i).CssClass = "tablehead"

        'row data
        Dim ClientName, Company As String
        Dim OrderDate As Date
        ClientName = dsCases.Tables("CaseList").Rows(j).Item("firstname") & " " & dsCases.Tables("CaseList").Rows(j).Item("lastname")
        Company = dsCases.Tables("CaseList").Rows(j).Item("company")
        OrderDate = dsCases.Tables("CaseList").Rows(j).Item("CaseSent")

        'create details button
        Dim btnDetails As New Button()
        btnDetails.Text = "Details"
        btnDetails.ID = "btnDetails"
        btnDetails.Width = "60"
        btnDetails.Font.Size = "8"
        'AddHandler btnSubmit.Click, AddressOf sendDetails_Click

        'create schedule button
        Dim btnSchedule As New Button()
        btnSchedule.Text = "Schedule"
        btnSchedule.ID = "btnSchedule"
        btnSchedule.Width = "60"
        btnSchedule.Font.Size = "8"
        'AddHandler btnSubmit.Click, AddressOf sendDetails_Click

        For i = 0 To numcells - 1

            Dim iMod As Integer
            iMod = j Mod 2

            c = New TableCell()


            If i = 0 Then
                c.Controls.Add(New LiteralControl(iMod & " "))
                c.Controls.Add(New LiteralControl(UserId))

            ElseIf i = 1 Then

                c.Controls.Add(New LiteralControl(ClientName))

            ElseIf i = 2 Then

                c.Controls.Add(New LiteralControl(Company))

            ElseIf i = 3 Then



            ElseIf i = 4 Then

                c.Controls.Add(New LiteralControl(FormatDateTime(OrderDate, DateFormat.ShortDate)))

            ElseIf i = 5 Then

                c.Controls.Add(btnDetails)

            ElseIf i = 6 Then

                c.Controls.Add(btnSchedule)

            End If

            r.Cells.Add(c)
        Next i

        Table1.Rows.Add(r)


        If j Mod 2 = 1 Then
            r.BackColor = Drawing.Color.Aquamarine
            'Table1.Rows(j).Cells(i).CssClass = "odd"
        Else
            r.BackColor = Drawing.Color.Blue
            'Table1.Rows(j).Cells(i).CssClass = "even"
        End If

    Next j

    r = New TableRow()

    ' footer row
    c = New TableCell()
    c.ColumnSpan = 7
    c.HorizontalAlign = HorizontalAlign.Right
    r.Cells.Add(c)
    Table1.Rows.Add(r)

感谢您的帮助。

学家

1 个答案:

答案 0 :(得分:2)

您可以像这样使用ColorTranslator类:

r.BackColor = ColorTranslator.FromHtml("#0000FF")

此处的文档:http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.fromhtml.aspx