asp.net - asp:Table 以编程方式更改行颜色

  显示原文与译文双语对照的内容
0 0

我正在学习. 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)

感谢你的帮助。

J 。

时间:原作者:1个回答

0 0

你可以像这样使用 ColorTranslator 类:

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

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

原作者:
...