簡體   English   中英

在ASP.NET中動態更改表格單元格的背景顏色

[英]Dynamically changing a table cell background color in asp.net

我的.aspx頁上有以下表格行:

<tr>
        <td valign="bottom" style="width: 157px">Initial Requirements:&nbsp;&nbsp;&nbsp;&nbsp;</td>
        <asp:Repeater ID="Repeater11" runat="server" DataSourceID="ObjectDataSource1">

        <ItemTemplate>
         <td valign="bottom" id="ReqStatus" runat="server" style="background-color: Gray">
         <%#ReqStatus(CType(CType(Container.DataItem, System.Data.DataRowView).Row, DataSet1.DataTable1Row))%>

         </td>

       </ItemTemplate>
       </asp:Repeater>

        </tr> 

在后面的代碼中,我具有以下功能:

Protected Function ReqStatus(ByVal Project As DataSet1.DataTable1Row) As String
        'Dim ReqTableCell As TableCell
        'ReqTableCell = form1.FindControl("ReqStatus")


        ' Check the status of the Development Completed
        Dim rightNow As Date = Now()
        Dim dateDifference As TimeSpan
        If Not Project.IsNull("Requirements_Target") Then

            Dim ReqTargetDate As Date = Project.Requirements_Target

            If Project.IsNull("Req_Date") Then
                dateDifference = ReqTargetDate.Subtract(rightNow)

                If dateDifference.Days > 0 Then

                    If dateDifference.Days >= 60 Then
                        'ReqTableCell.BackColor = Drawing.Color.Green
                        Return "<strong><font color='green'>" & dateDifference.Days & "</font></strong>"
                    ElseIf dateDifference.Days > 30 And dateDifference.Days < 60 Then
                        'ReqTableCell.BackColor = Drawing.Color.Yellow
                        Return "<strong><font color='yellow'>" & dateDifference.Days & "</font></strong>"
                    Else

                        'ReqTableCell.BackColor = Drawing.Color.Red
                        Return "<strong><font color='red'>" & dateDifference.Days & "</font></strong>"
                    End If

                Else
                    'ReqTableCell.BackColor = Drawing.Color.Red
                    Dim pastDue As Int16 = (dateDifference.Days * -1)
                    Return "<strong><font color='red'>" & pastDue & "</font></strong> days past"

                End If
            End If
        Else

        End If
    End Function

我可以根據條件語句更改返回值的顏色,但是無法找出更改表單元格背景的正確語法。 我的嘗試被注釋掉了。

如何正確聲明表格單元格? Findcontrol一定不是正確的方法。

您可以在Codebehind中進行完全控制(未經測試,僅是為了給您帶來啟發):

Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
        Select Case e.Item.ItemType
            Case ListItemType.Item
                Dim Project As DataSet1.DataTable1Row = DirectCast(DirectCast(e.Item.DataItem, System.Data.DataRowView).Row, DataSet1.DataTable1Row)
                Dim tdReqStatus As HtmlTableCell = DirectCast(e.Item.FindControl("tdReqStatus"), HtmlTableCell)
                Dim lblReqStatus As Label = DirectCast(e.Item.FindControl("lblReqStatus"), Label)
                ' Check the status of the Development Completed
                Dim rightNow As Date = Now()
                Dim dateDifference As TimeSpan
                If Not Project.IsNull("Requirements_Target") Then
                    Dim ReqTargetDate As Date = Project.Requirements_Target
                    If Project.IsNull("Req_Date") Then
                        dateDifference = ReqTargetDate.Subtract(rightNow)
                        lblReqStatus.Font.Bold = True

                        If dateDifference.Days > 0 Then

                            If dateDifference.Days >= 60 Then
                                tdReqStatus.BgColor = "Green"
                                lblReqStatus.Text = dateDifference.Days.ToString
                            ElseIf dateDifference.Days > 30 And dateDifference.Days < 60 Then
                                tdReqStatus.BgColor = "Yellow"
                                lblReqStatus.Text = dateDifference.Days.ToString
                            Else
                                tdReqStatus.BgColor = "Red"
                                lblReqStatus.Text = dateDifference.Days.ToString
                            End If

                        Else
                            tdReqStatus.BgColor = "Red"
                            lblReqStatus.Text = (dateDifference.Days * -1).ToString
                        End If
                    End If
                End If
        End Select
    End Sub

在aspx上:

   <table>
   <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <tr>
             <td valign="bottom" id="TdReqStatus" runat="server" >
                <asp:label ID="lblReqStatus" runat="server" ></asp:label>
             </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
    </table>

看一下我的解決方案片段:

TableRow tr = new TableRow();
TableCell tc = new TableCell { Text = "someText"};

tc.BackColor = ColorTranslator.FromHtml("#0000FF");
tc.ForeColor = ColorTranslator.FromHtml("#FFFFFF");
tc.Font.Bold = true;
tc.Font.Size = 16;

tr.Cells.Add(tc);
table.Rows.Add(tr);

完成此操作並控制所有語句后,您可以在網頁中呈現表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM