2015-09-14 31 views
0

嘿傢伙我需要一些我正在開發的應用程序的幫助。從代碼背後的中繼器中的樣式表

我想添加一個css類/樣式到一個表,這取決於我從數據庫中獲得的某個值(例如0-2)。

這是我需要改變表的樣式代碼

Public Function projectType(ByVal value As Integer) 
    Dim projectName As String 
    If value = 0 Then 
    projectName = "Project" 
    mytable.AddAttributes("Style", "Background-color:#444444") 
    ElseIf value = 1 Then 
    projectName = "Support" 
    Else 
    projectName = "Not available" 
    End If 
    Return projectName 
End Function 

標記:

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="1" 
       RepeatDirection="Vertical" CellPadding="0" CellSpacing="0"> 
       <ItemTemplate> 
        <table cellpadding="0" cellspacing="0" style="width: 100%; text-align: left; border-bottom: 1px solid #999999; 
        padding-bottom: 15px;"> 
        <tr> 
         <td style="width: 110px; vertical-align: top; padding-left: 20px; padding-top: 5px; 
         padding-bottom: 5px;"> 
         <asp:Label ID="Label5" runat="server" Text='<%# projectType(Eval("Type")) %>' Font-Names="Verdana" 
          Font-Size="9pt" EnableTheming="false" /> 
         </td> 
         <td style="width: 110px; vertical-align: top; padding-left: 20px; padding-top: 5px; 
         padding-bottom: 5px;"> 
         <asp:Label ID="Label2" runat="server" Text='<%# hoursCheck(Eval("Duration")) %>' 
          Font-Names="Verdana" Font-Size="9pt" Style="text-align: right" EnableTheming="false" /> 
         </td> 
        </tr> 
        <tr> 
         <td colspan="2" style="padding-bottom: 5px; padding-top: 5px; text-align: center"> 
         <asp:Label ID="Label7" runat="server" Text='<%# Eval("FullName") %>' Font-Names="Verdana" 
          Font-Size="7pt" EnableTheming="false" /> 
         </td> 
        </tr> 
        </table> 
       </ItemTemplate> 
       </asp:DataList> 

從這個功能,我需要通過獲取的父表訪問表發件人,因爲我不能明確地說我想Datalist1,因爲我有5個,我該怎麼做?

+3

問題肯定需要更多的數據。什麼是表格,發件人是什麼?任何相關的頁面標記? – Andrei

+0

@Andrei編輯了一些標記和更多信息。 – DieVeenman

回答

0

首先,請擺脫內聯樣式,並用CSS類替換它們。稍後會爲您節省很多頭痛。例如,有兩個CSS類,一個用於「Project」,另一個用於「支持」和「不可用」

至於實際問題,想到的最簡單的事情是將邏輯分成兩個函數,一個文本和一個CSS類:

<%-- This might actually need runat="server", not sure --%> 
<table cellpadding="0" cellspacing="0" style='<%# projectTypeClass(Eval("Type")) %>' 

<asp:Label ID="Label5" runat="server" Text='<%# projectType(Eval("Type")) %>' 

注:

Public Function projectTypeText(ByVal value As Integer) 
    ... 
    Return projectName 
End Function 

Public Function projectTypeClass(ByVal value As Integer) 
    ... 
    Return projectCssClass 
End Function 

然後像你一樣使用它正好,如果你絕對要應對內嵌類,你可以有projectTypeStyle返回Background-color:#444444,但隨後在標記內你將需要做這樣的討厭的東西:

<table cellpadding="0" cellspacing="0" style='<%# "width: 100%; rest of styles; " + projectTypeStyle(Eval("Type")) %>' 

這是可怕的,所以請不惜一切代價。