繁体   English   中英

使用Classic Asp变量更新CSS

[英]Using Classic Asp variable to update CSS

我有一个经典的ASP页面。 在此页面上,我需要根据填充该表的数据库是否返回任何结果来隐藏表。 如果表为空,则标题被隐藏。 <table>没有visibledisplay元素,因此我将其包装在<div> 但是,在页面执行时不应用CSS。

在此处输入图片说明

.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>

我认为您有一些选择。 这是一种老式的方法。

选项1:让If Count> 0做工作服务器端,而不是让CSS确定表的显示或隐藏。

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并使之工作。 或者至少只要您的代码设置了可见性即可。

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

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


</table>
</div>

在ASP Classic中,通常我们需要编写一个小脚本来检查表数据是否存在。 如果您没有在函数或子调用中放置内容,请记住遵循从左到右,从上到下的顺序。

count> 0需要触发CSS的构建,因此可以将vis包含到<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>

在您的帖子中,实际上您是在设置<%= vis%>之前调用它。 从上到下,从左到右,对代码重新排序。

您应该将下面的代码放在顶部,然后再次测试:

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

以下代码在我的计算机上运行良好

<%

' 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM