Regis
Regis

Reputation: 176

Using Classic Asp variable to update CSS

I have a classic ASP page. On this page I need to hide a table based on whether the database that fills that table returns any results. If the table is empty then the header is hidden. <table> doesn't have a visible or display element thus I am wrapping it in a <div>. However when the page executes the css isn't applied.

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>

Upvotes: 1

Views: 1831

Answers (3)

Murray W
Murray W

Reputation: 102

I think you have a few options. Here's an old fashioned method.

Option 1: Instead of having your CSS determine the Show or Hide of your table, have the If Count > 0 do the work server side .

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

If you must write CSS for your script you typically need to write the script twice, so you can have your CSS embedded in your header correctly.

Option 2: Placed in 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)
%>

Then you can place your table in the body.

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


</table>
</div>

Option 3: You can Inline your CSS and make it work. Or at least it should as long as your code sets the vis.

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

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


</table>
</div>

Often times in ASP Classic we need to write a small script to check if our table data is there. Remember to follow the left to right, top to bottom if you're not placing things in function or sub calls.

The count > 0 needs to trigger the building of your CSS so it can include the vis to your <Div> element.
If you're getting your Count value after running your SQL then you might need to setup that second script to test if you have data for your table then build your CSS. Example:

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

We then we can mix the examples above to only trigger when we have a table to show.

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

In the body you could then use something like the following.

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

In your post you are actually calling the <%=vis%> before you set it. Top to Bottom, Left to Right, reorder your code.

Upvotes: 2

Hung Nguyen
Hung Nguyen

Reputation: 1

Below codes work well in my computer

<%

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

Upvotes: 0

Hung Nguyen
Hung Nguyen

Reputation: 1

You should put below code at the top, then you test again:

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

Upvotes: 0

Related Questions