將動態分頁列表一次性產生為靜態 HTML 檔案 (如 `list_1.html`, `list_2.html`...) 以降低資料庫負載、提升瀏覽速度。
<!--#include file="conn.asp"-->
<html><head><title>靜態頁產生器</title></head><body>
<%
' 1. 取得總頁數
Dim sql, rs, totalPage, pageSize
sql = "SELECT * FROM News"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 1 ' adOpenKeyset, adLockReadOnly
pageSize = 20
rs.PageSize = pageSize
totalPage = rs.PageCount
rs.Close : Set rs = Nothing
' 2. 迴圈產生每一頁
Dim fso, file, strHead, strBody, strFoot, j, whichPage, howManyRecs
Set fso = Server.CreateObject("Scripting.FileSystemObject")
strHead = "新聞列表 "
strFoot = ""
For j = 1 To totalPage
whichPage = j
sql = "SELECT * FROM News"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 1
rs.PageSize = pageSize
rs.AbsolutePage = whichPage
howManyRecs = 0
strBody = ""
Do While Not rs.EOF And howManyRecs < rs.PageSize
strBody = strBody & ""
strBody = strBody & "" & rs(0) & " " ' ID
strBody = strBody & "" & rs(1) & " " ' 標題
strBody = strBody & "" & rs(2) & " " ' 日期
strBody = strBody & "" & rs(3) & " " ' 分類
strBody = strBody & " "
rs.MoveNext
howManyRecs = howManyRecs + 1
Loop
strBody = strBody & "
"
' 產生分頁導覽列
Dim navHtml
navHtml = ""
If whichPage > 1 Then
navHtml = navHtml & "上一頁 "
navHtml = navHtml & "首頁 "
End If
navHtml = navHtml & " 第 " & whichPage & " 頁 / 共 " & totalPage & " 頁 "
If whichPage < totalPage Then
navHtml = navHtml & "下一頁 "
navHtml = navHtml & "尾頁 "
End If
navHtml = navHtml & ""
' 寫入檔案
Dim fileName : fileName = Server.MapPath("list_" & j & ".html")
Set file = fso.CreateTextFile(fileName, True) ' True=覆蓋
file.Write strHead & strBody & navHtml & strFoot
file.Close
rs.Close : Set rs = Nothing
Response.Write "已產生 list_" & j & ".html <br>"
Next
Set fso = Nothing
Response.Write "
所有靜態頁面產生完畢!"
%>
</body></html>
// C# 靜態化範例
public void GenerateStaticPages()
{
var template = File.ReadAllText(Server.MapPath("~/Templates/ListTemplate.html"));
int pageSize = 20, total = GetTotalCount();
int totalPages = (int)Math.Ceiling((double)total / pageSize);
for (int p = 1; p <= totalPages; p++)
{
var data = GetPageData(p, pageSize);
string html = RenderTemplate(template, data, p, totalPages);
File.WriteAllText(Server.MapPath($"~/Static/list_{p}.html"), html, Encoding.UTF8);
}
}