开发者

asp:Table change row color programmatically

开发者 https://www.devze.com 2023-02-08 21:39 出处:网络
I am learning .net and building a table from code behind and attempting to change alternate row colors.

I am learning .net and building a table from code behind and attempting to change alternate row colors.

I have it working but at he moment only using Drawing.Color when I would like to use a hexidecimal value, is there a way of doing this?

Here is the code thats doing it at the moment:

        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

Here is full code:

    ' 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.Horizon开发者_JAVA技巧talAlign = HorizontalAlign.Right
    r.Cells.Add(c)
    Table1.Rows.Add(r)

Thanks for any help.

J.


You can use the ColorTranslator class like this:

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

Documentation here: http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.fromhtml.aspx

0

精彩评论

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