使用经典 Asp 变量更新 CSS
Using Classic Asp variable to update CSS
我有一个经典的 ASP 页面。在此页面上,我需要隐藏一个 table 基于是否填充 table returns 任何结果的数据库。如果 table 为空,则 header 被隐藏。 <table>
没有 visible
或 display
元素,因此我将其包装在 <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>
你应该把下面的代码放在最上面,然后你再测试一下:
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>
我想你有几个选择。
这是一个老式的方法。
选项 1:
不要让 CSS 确定 table 的显示或隐藏,而是让 If Count > 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 正确地嵌入到您的 header 中。
选项 2:
放在 header .
<%
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)
%>
然后你可以把你的 table 放在 body.
<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 Classic 中我们需要编写一个小脚本来检查我们的 table 数据是否存在。如果您不在函数或子调用中放置东西,请记住遵循从左到右、从上到下的顺序。
计数 > 0 需要触发 CSS 的构建,以便它可以包含对 <Div>
元素的可见性。
如果您在 运行 您的 SQL 之后获得计数值,那么您可能需要设置第二个脚本来测试您是否有 table 的数据,然后构建您的 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
然后我们可以混合上面的例子,只在我们有 table 显示时触发。
<header>
<%
If MyCount() = 1 Then
Dim vis
vis = "block"
Else
vis = "none"
End If
%>
</header>
在 body 中,您可以使用如下内容。
<div style="display:<%=vis%>;">
<table>
<!-- Table elements -->
</table>
</div>
在您的 post 中,您实际上是在设置 <%=vis%> 之前调用它。
从上到下,从左到右,重新排列您的代码。
我有一个经典的 ASP 页面。在此页面上,我需要隐藏一个 table 基于是否填充 table returns 任何结果的数据库。如果 table 为空,则 header 被隐藏。 <table>
没有 visible
或 display
元素,因此我将其包装在 <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>
你应该把下面的代码放在最上面,然后你再测试一下:
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>
我想你有几个选择。 这是一个老式的方法。
选项 1: 不要让 CSS 确定 table 的显示或隐藏,而是让 If Count > 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 正确地嵌入到您的 header 中。
选项 2: 放在 header .
<%
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)
%>
然后你可以把你的 table 放在 body.
<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 Classic 中我们需要编写一个小脚本来检查我们的 table 数据是否存在。如果您不在函数或子调用中放置东西,请记住遵循从左到右、从上到下的顺序。
计数 > 0 需要触发 CSS 的构建,以便它可以包含对 <Div>
元素的可见性。
如果您在 运行 您的 SQL 之后获得计数值,那么您可能需要设置第二个脚本来测试您是否有 table 的数据,然后构建您的 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
然后我们可以混合上面的例子,只在我们有 table 显示时触发。
<header>
<%
If MyCount() = 1 Then
Dim vis
vis = "block"
Else
vis = "none"
End If
%>
</header>
在 body 中,您可以使用如下内容。
<div style="display:<%=vis%>;">
<table>
<!-- Table elements -->
</table>
</div>
在您的 post 中,您实际上是在设置 <%=vis%> 之前调用它。 从上到下,从左到右,重新排列您的代码。