首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> ASP 程式設計 >> ASP 相關知識

ASP 編程入門:17 個核心基礎代碼模塊全解析

     編輯:ASP 相關知識

ASP 經典開發雖屬過去式,但其核心請求處理、資料庫操作、Session 管理等思想仍具參考價值。本文將 17 個高頻代碼片段整理為「可直接套用的功能模塊」,助初學者快速建立完整認知框架。

1. 表單資料獲取:GET vs POST

' GET:參數在 URL,用 Request.QueryString
name = Request.QueryString("Name")   ' https://site?Name=jack

' POST:參數在 HTTP Body,用 Request.Form
name = Request.Form("Name")

口訣:QueryString 對應 `

`,Form 對應 ``。

2. Request.Form 進階用法:多值字段與遍歷

' 同名字段多值(如 checkbox 群組)
For i = 1 To Request.Form("chkItem").Count
    Response.Write Request.Form("chkItem")(i) & "<br>"
Next

' 遍歷所有表單字段
For Each key In Request.Form
    Response.Write key & " = " & Request.Form(key) & "<br>"
Next

3. 客戶端資訊獲取

IP = Request.ServerVariables("REMOTE_ADDR")      ' 客戶端 IP
Port = Request.ServerVariables("SERVER_PORT")    ' 伺服器接收埠
Lang = Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") ' 瀏覽器語言

4. 日期時間顯示與格式化

Response.Write Now()           ' 完整日期時間
Response.Write Date()          ' 僅日期
Response.Write Time()          ' 僅時間
Response.Write FormatDateTime(Now, 2) ' 短日期格式 yyyy-MM-dd

5. DLL 組件註冊(傳統 COM 元件)

' 命令列執行
regsvr32 MyComponent.dll

6. 迴圈輸出與字串拼接

' 字體漸大
For i = 1 To 5
    Response.Write "快速ASP<br>"
Next

' 字串拼接用 &
a = "我" : b = "愛" : c = "你"
Response.Write a & b & c & "媽媽"

7. 基礎導向與腳本輸出

Response.Write "Welcome"           ' 直接輸出
Response.Redirect "homepage.asp"   ' 頁面跳轉(須在輸出前)
Response.Write "" ' 彈窗

8. 條件判斷:登入驗證範例

If Request("username") = "admin" And Request("pass") = "admin" Then
    Response.Redirect "admin.asp"
Else
    Response.Redirect "login.asp"
End If

9. 資料庫操作核心模塊(Access + ADO)

連接字串

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")

查詢、新增、修改、刪除

' 查詢
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM News WHERE Id=" & Request("id"), conn, 1, 1

' 新增
rs.AddNew
rs("Title") = Request.Form("title")
rs("Content") = Request.Form("content")
rs.Update

' 修改
rs("Title") = Request.Form("title")
rs.Update

' 刪除
conn.Execute "DELETE FROM News WHERE Id=" & Request("delid")

10. #include 檔案引入

<!--#include virtual="/inc/conn.asp"-->  ' 虛擬路徑(推薦)
<!--#include file="../inc/conn.asp"-->   ' 相對路徑

11. Session 與 Application:計數器範例

' Application 鎖定計數
Application.Lock
Application("Counter") = Application("Counter") + 1
Application.UnLock
Response.Write Application("Counter")

12. 防止重複提交:Session 標記法

If Not Session("Submitted") Then
    Session("Submitted") = True
    ' 執行業務邏輯
Else
    Response.Write "請勿重複提交"
End If

13. 顯示資料庫圖片與超連結

Set rs = conn.Execute("SELECT Name, Tel, Url, Image FROM AddressBook")
Do While Not rs.EOF
    Response.Write " " & rs("Name") & "<br>"
    rs.MoveNext
Loop

14. 字串處理常用函數速查

  • Trim/Left/Right/Mid:去空格、截取
  • LCase/UCase:大小寫轉換
  • Replace(str, old, new):替換
  • InStr(str, substr):查找位置
  • Len(str):長度(中文算 1)

15. 頁面緩存控制

Response.Expires = 0           ' 立即過期
Response.ExpiresAbsolute = #Jan 1, 2030# ' 絕對過期時間

16. 伺服端腳本標籤 `

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved