2016-01-22 65 views
1

我有一個經典的ASP頁面。在此頁面上,我需要根據填充該表的數據庫是否返回任何結果來隱藏表。如果表格是空的,那麼標題是隱藏的。 <table>沒有visibledisplay元素,因此我將它包裝在<div>中。但是當頁面執行時,css沒有被應用。使用經典的Asp變量來更新CSS

enter image description here

.hideDiv { 
    display: <%=vis%>; 
} 


<div class="hideDiv"> 
    <table> 
    <!-- Table elements --> 
<% 

' Other code 
If count > 0 Then 
    vis = "block" 
Else 
    vis = "none" 
End If 
' The vis variable is not updated past this point 

%> 

</table> 
</div> 
+0

爲[這個答案](http://stackoverflow.com/a/34938611/692942)指出傳統的ASP順序處理所以'的風格,那麼設置之後被調用時,糾正這一點,vis'將是空的需要在風格被操縱之前設置。 – Lankymart

回答

2

我想你有幾個選項。 這是一個老式的方法。

選項1: 而不是讓你的CSS決定你的表的顯示或隱藏,有如果計數> 0做工作服務器端。

If count > 0 Then 
Response.Write("<table>" & vbCrLf) 
'# Do you Table Tags and your code here. 
Response.Write("</table>" & vbCrLf) 
End If 

如果您必須編寫腳本您通常需要編寫腳本兩次CSS,所以你可以有你的CSS嵌入在你的頭正確。

選項2: 放在標題中。

<% 
Dim vis 
If count > 0 Then 
    vis = "block" 
Else 
    vis = "none" 
End If 

Response.Write("<style type=""text/css"">" & vbCrLf) 
Response.Write(" .hideDiv {" & vbCrLf) 
Response.Write(" display: "&vis&";" & vbCrLf) 
Response.Write("}" & vbCrLf) 
Response.Write("</style>" & vbCrLf) 
%> 

然後你可以把你的桌子放在身體裏。

<div class="hideDiv"> 
    <table> 
    <!-- Table elements --> 


</table> 
</div> 

選項3: 您可以將您的CSS內聯並使其工作。或者至少它應該只要你的代碼設置vis。

<% 
    Dim vis 
    If count > 0 Then 
     vis = "block" 
    Else 
     vis = "none" 
    End If 
    %> 

<div style="display:<%=vis%>;"> 
    <table> 
    <!-- Table elements --> 


</table> 
</div> 

經常在ASP經典的時候,我們需要寫一個小的腳本來檢查我們的表中的數據是存在的。請記住,如果您沒有將功能放在功能或子電話中,請按從左到右,從上到下的順序。

count> 0需要觸發構建CSS,因此它可以包含對您的<Div>元素的可見性。
如果你在運行SQL之後得到Count值,那麼你可能需要設置第二個腳本來測試你的表是否有數據然後建立你的CSS。 例子:

Function MyCount() 
Dim Count 
Count = 0 
SQL = SELECT Top 1 ID FROM Table WHERE FIELD1 Is Not NULL 
'# blah 
If rs.EOF=False Then 
count = 1 
End If 
MyCount = count 
End Function 

然後我們就可以當我們有一個表格,顯示混合上面只觸發例子。

<header> 
<% 
If MyCount() = 1 Then 
    Dim vis 
     vis = "block" 
Else 
     vis = "none" 
End If 
%> 
</header> 

在身體中,你可以使用類似下面的東西。

<div style="display:<%=vis%>;"> 
    <table> 
    <!-- Table elements --> 
</table> 
</div> 

在您的文章,你實際上是調用<%=顯示您設置之前%>。 從上到下,從左到右,重新排列您的代碼。

0

你應該把下面的代碼在上面,然後再次測試:

If count > 0 Then 
    vis = "block" 
Else 
    vis = "none" 
End If 
+0

之前來過這裏。不起作用。 – Regis

0

下面的代碼在我的電腦上運行良好

<% 

' Other code 
If count > 0 Then 
    vis = "block" 
Else 
    vis = "none" 
End If 
' The vis variable is not updated past this point 

%> 

.hideDiv { 
    display: <%=vis%>; 
} 


<div class="hideDiv"> 
    <table> 
    <!-- Table elements --> 


</table> 
</div> 
+0

它真的需要你發佈兩個相同的答案嗎?如果你想發佈更多的細節到第一個答案[編輯](http://stackoverflow.com/posts/34938400/edit)或刪除第一個答案,如果這個更簡潔。 – Lankymart