按列排序 header TD 经典 ASP

Sort by column header TD on Classic ASP

我喜欢按 TD 列排序。当我打开网页时,它按 ID 排序。然后,当我单击姓氏或名字时,它将按 ASC 排序,如果我再次单击它,它将按 DESC 排序。

这是我的代码,我有一个错误。

你能帮忙吗?

谢谢。

Dim sOrder
sOrder="ASC"
If Request("sort")="ASC" Then
 sOrder="DESC"
End If
%>
<table>
<tr>
<td>Class</td>
<% If sOrder = "ASC" Then %>
<td "<a href='?sort=" & sOrder & ">" >Last name</td>
<% Else %>
<td "<a href='?sort=" & sOrder & ">">Last name</td>
<% End If %>
<% If sOrder = "ASC" Then %>
<td "<a href='?sort=" & sOrder & ">" >First name</td>
<% Else %>
<td "<a href='?sort=" & sOrder & ">">First name</td>
<% End If %>
</tr>

<%
rsSQL.Open "select * from students ORDER BY " & sColumn & " " & sOrder, conn
If Not rsSQL.EOF Then
        nNo = 1
        While Not rsSQL.EOF
        ID          = rsSQL.Fields("ID")
        Lastname            = rsSQL.Fields("Lastname")
        Firstname       = rsSQL.Fields("Firstname")
%>

<tr>
<td><%= ID%></td>
<td><%= Lastname%></td>
<td><%= Firstname%></td>
</tr>
<%
        rsSQL.MoveNext  
        nNo = nNo + 1
        Wend    
rsSQL.Close
    End If
%>

尝试

Dim sOrder, sColumn
sColumn="ID"
sOrder=""
If Request.QueryString("sort") = "ASC" Then
    sOrder = "DESC"
End if
If Request.QueryString("col") = "Firstname" OR Request.QueryString("col") = "Lastname" Then 
    sColumn = Request.QueryString("col")
End If
%>
<table>
    <tr>
        <td>Class</td>
<% If sColumn = "Lastname" AND sOrder = "ASC" Then %>
        <td><a href="?col=Lastname&amp;sort=DESC">Last name</a></td>
<% Else %>
        <td><a href="?col=Lastname&amp;sort=ASC">Last name</a></td>
<% End If %>
<% If sColumn = "Firstname" AND sOrder = "ASC" Then %>
        <td><a href="?col=Firstname&amp;sort=DESC">First name</a></td>
<% Else %>
        <td><a href="?col=Firstname&amp;sort=ASC">First name</a></td>
<% End If %>
    </tr>

<%
rsSQL.Open "select * from students ORDER BY " & sColumn & " " & sOrder, conn
...
%>

    <tr>
        <td><%= ID %></td>
        <td><%= Lastname %></td>
        <td><%= Firstname %></td>
    </tr>
<%
...
%>