目录 搜索
ASP 基础教程 在自己的 PC 上运行 ASP ASP 基本语法规则 ASP 变量 ASP 子程序 ASP 表单和用户输入 ASP Cookies ASP Session 对象 ASP Application 对象 ASP 引用文件 ASP Global.asa 文件 ASP 使用 CDOSYS 发送电子邮件 ASP 对象 ASP Response 对象 ASP Cookies 集合 ASP Buffer 属性 ASP CacheControl 属性 ASP Charset 属性 ASP ContentType 属性 ASP Expires 属性 ASP ExpiresAbsolute 属性 ASP IsClientConnected 属性 ASP PICS 属性 ASP Status 属性 ASP AddHeader 方法 ASP AppendToLog 方法 ASP BinaryWrite 方法 ASP Clear 方法 ASP End 方法 ASP Flush 方法 ASP Redirect 方法 ASP Write 方法 ASP Request 对象 ASP Cookies 集合 ASP Form 集合 ASP QueryString 集合 ASP ServerVariables 集合 ASP 教程 ASP TotalBytes 属性 ASP BinaryRead 方法 ASP Application 对象 ASP Contents 集合 ASP StaticObjects 集合 ASP Contents.RemoveAll 方法 ASP Lock 和 Unlock 方法 ASP Application OnStart 和 Application OnEnd 事件 ASP Session 对象 ASP Contents 集合 ASP StaticObjects 集合 ASP CodePage 属性 ASP LCID 属性 ASP SessionID 属性 ASP Timeout 属性 ASP Abandon 方法 ASP Contents.Remove 方法 ASP Contents.RemoveAll 方法 ASP Session OnStart 和 Session OnEnd 事件 ASP ScriptTimeout 属性 ASP CreateObject 方法 ASP Execute 方法 ASP Server 对象 ASP GetLastError() 方法 (ASP 3.0) ASP HTMLEncode 方法 ASP MapPath 方法 ASP Transfer 方法 ASP URLEncode 方法 ASP ASPError 对象 ASP ASPError 对象的属性 ASP FileSystemObject 对象 ASP Drives 属性 ASP BuildPath 方法 ASP CopyFile 方法 ASP CopyFolder 方法 ASP CreateFolder 方法 ASP CreateTextFile 方法 ASP DeleteFile 方法 ASP DeleteFolder 方法 ASP DriveExists 方法 ASP FileExists 方法 ASP FolderExists 方法 ASP GetAbsolutePathName 方法 ASP GetBaseName 方法 ASP GetDrive 方法 ASP GetDriveName 方法 ASP GetExtensionName 方法 ASP GetFile 方法 ASP GetFileName 方法 ASP GetFolder 方法 ASP GetParentFolderName 方法 ASP GetSpecialFolder 方法 ASP GetTempName 方法 ASP MoveFile 方法 ASP MoveFolder 方法 ASP OpenTextFile 方法 ASP TextStream 对象 ASP AtEndOfLine 属性 ASP AtEndOfStream 属性 ASP Column 属性 ASP Line 属性 ASP Close 方法 ASP Read 方法 ASP ReadAll 方法 ASP ReadLine 方法 ASP Skip 方法 ASP SkipLine 方法 ASP Write 方法 ASP WriteLine 方法 ASP WriteBlankLines 方法 ASP Drive 对象 ASP AvailableSpace 属性 ASP DriveLetter 属性 ASP DriveType 属性 ASP FileSystem 属性 ASP FreeSpace 属性 ASP IsReady 属性 ASP Path 属性 ASP RootFolder 属性 ASP SerialNumber 属性 ASP ShareName 属性 ASP TotalSize 属性 ASP VolumeName 属性 ASP File 对象 ASP Attributes 属性 ASP DateCreated 属性 ASP DateLastAccessed 属性 ASP DateLastModified 属性 ASP Drive 属性 ASP Name 属性 ASP ParentFolder 属性 ASP Path 属性 ASP ShortName 属性 ASP ShortPath 属性 ASP Size 属性 ASP Type 属性 ASP Copy 方法 ASP Delete 方法 ASP Move 方法 ASP OpenAsTextStream 方法 ASP Folder 对象 ASP Files 集合 ASP SubFolders 集合 ASP Attributes 属性 ASP DateCreated 属性 ASP DateLastAccessed 属性 ASP DateLastModified 属性 ASP Drive 属性 ASP IsRootFolder 属性 ASP Name 属性 ASP ParentFolder 属性 ASP Path 属性 ASP ShortName 属性 ASP ShortPath 属性 ASP Size 属性 ASP Type 属性 ASP Copy 方法 ASP Delete 方法 ASP Move 方法 ASP CreateTextFile 方法 ASP Dictionary 对象 ASP CompareMode 属性 ASP Count 属性 ASP Item 属性 ASP Key 属性 ASP Add 方法 ASP Exists 方法 ASP Items 方法 ASP Keys 方法 ASP Remove 方法 ASP RemoveAll 方法 ASP ADO ASP 组件 ASP AdRotator 组件 ASP Browser Capabilities 组件 ASP Content Linking 组件 ASP Content Rotator 组件 (ASP 3.0) ASP 与 AJAX AJAX 简介 ASP - AJAX 与 ASP AJAX 数据库实例 ASP 实例 ASP 实例 ASP 快速参考
文字

ASP 实例


基础

用ASP写文本

<!DOCTYPE html>
<html>
<body>

<%
response.write("Hello World!")
%>

</body>
</html>

向文本添加HTML

<!DOCTYPE html>
<html>
<body>
<%
response.write("<h2>You can use HTML tags to format the text!</h2>")
%>

<%
response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")
%>
</body>
</html>

变量

声明变量
变量用于存储信息。本例演示如何声明变量,为变量赋值,并在程序中使用这个变量。

<!DOCTYPE html>
<html>
<body>

<%
dim name
name="Donald Duck"
response.write("My name is: " & name)
%>

</body>
</html>
My name is: Donald Duck

声明数组
数组用于存储一系列相关的数据项目。本例演示如何声明一个存储名字的数组。

<!DOCTYPE html>
<html>
<body>

<%
Dim famname(5),i
famname(0) = "Jan Egil"
famname(1) = "Tove"
famname(2) = "Hege"
famname(3) = "Stale"
famname(4) = "Kai Jim"
famname(5) = "Borge"

For i = 0 to 5
      response.write(famname(i) & "<br>")
Next
%>

</body>
</html>
Jan Egil
Tove
Hege
Stale
Kai Jim
Borge

循环生成 HTML 标题
本例演示如何循环生成 6 个不同的 HTML 标题。

<!DOCTYPE html>
<html>
<body>

<%
dim i 
for i=1 to 6
   response.write("<h" & i & ">Heading " & i & "</h" & i & ">")
next
%>

</body>
</html>

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

使用 Vbscript 制作基于时间的问候语
本例演示如何根据服务器时间向用户显示不同的消息。

<!DOCTYPE html>
<html>
<body>
<%
dim h
h=hour(now())

response.write("<p>" & now())
response.write("</p>")
If h<12 then
   response.write("Good Morning!")
else
   response.write("Good day!")
end if
%>
</body>
</html>
9/20/2013 11:08:35 PM
Good day!

使用 JavaScript 制作基于时间的问候语
本例同上,演示如何根据服务器时间向用户显示不同的消息,只是语法不同而已。

<%@ language="javascript" %>
<!DOCTYPE html>
<html>
<body>
<%
var d=new Date()
var h=d.getHours()

Response.Write("<p>")
Response.Write(d)
Response.Write("</p>")
if (h<12)
   {
   Response.Write("Good Morning!")
   }
else
   {
   Response.Write("Good day!")
   }
%>
</body>
</html>
Fri Sep 20 23:08:38 EDT 2013
Good day!


Date/Time 函数(VBScript)

日期和时间

<!DOCTYPE html>
<html>
<body>

Today's date is: <%response.write(date())%>.
<br>
The server's local time is: <%response.write(time())%>.

</body>
</html>

获得天的名字

<!DOCTYPE html>
<html>
<body>

<p>
VBScripts' function <b>WeekdayName</b> is used to get a weekday:
</p>
<%
response.Write(WeekDayName(1))
response.Write("<br>")
response.Write(WeekDayName(2))
%>

<p>Abbreviated name of a weekday:</p>
<%
response.Write(WeekDayName(1,true))
response.Write("<br>")
response.Write(WeekDayName(2,true))
%>

<p>Current weekday:</p>
<%
response.Write(WeekdayName(weekday(date)))
response.Write("<br>")
response.Write(WeekdayName(weekday(date), true))
%>

</body>
</html>

获得月的名字

<!DOCTYPE html>
<html>
<body>

<p>VBScripts' function <b>MonthName</b> is used to get a month:</p>
<%
response.Write(MonthName(1))
response.Write("<br>")
response.Write(MonthName(2))
%>

<p>Abbreviated name of a month:</p>
<%
response.Write(MonthName(1,true))
response.Write("<br>")
response.Write(MonthName(2,true))
%>

<p>Current month:</p>
<%
response.Write(MonthName(month(date)))
response.Write("<br>")
response.Write(MonthName(month(date), true))
%>

</body>
</html>

获得今天的月和天

<!DOCTYPE html>
<html>
<body>

Today it is
<%response.write(WeekdayName(weekday(date)))%>,
<br>
and the month is
<%response.write(MonthName(month(date)))%>

</body>
</html>

3000年倒计时

<!DOCTYPE html>
<html>
<body>

<p>Countdown to year 3000:</p>

<p>
<%millennium=cdate("1/1/3000 00:00:00")%>

It is
<%response.write(DateDiff("yyyy", Now(), millennium))%>
years to year 3000!
<br>
It is
<%response.write(DateDiff("m", Now(), millennium))%>
months to year 3000!
<br>
It is
<%response.write(DateDiff("ww", Now(), millennium))%>
weeks to year 3000!
<br>
It is
<%response.write(DateDiff("d", Now(), millennium))%>
days to year 3000!
<br>
It is
<%response.write(DateDiff("h", Now(), millennium))%>
hours to year 3000!
<br>
It is
<%response.write(DateDiff("n", Now(), millennium))%>
minutes to year 3000!
<br>
It is
<%response.write(DateDiff("s", Now(), millennium))%>
seconds to year 3000!
</p>

</body>
</html>

从今天起计算N天后

<!DOCTYPE html>
<html>
<body>
<%
response.write(DateAdd("d",30,date()))
%>

<p>
Syntax for DateAdd: DateAdd(interval,number,date). You can use <b>DateAdd</b> to for example calculate a date 30 days from today.
</p>

</body>
</html>

格式化日期和时间

<!DOCTYPE html>
<html>
<body>

<%
response.write(FormatDateTime(date(),vbgeneraldate))
response.write("<br>")
response.write(FormatDateTime(date(),vblongdate))
response.write("<br>")
response.write(FormatDateTime(date(),vbshortdate))
response.write("<br>")
response.write(FormatDateTime(now(),vblongtime))
response.write("<br>")
response.write(FormatDateTime(now(),vbshorttime))
%>

<p>
Syntax for FormatDateTime: FormatDateTime(date,namedformat).
</p>

</body>
</html>

这是一个日期吗?

<!DOCTYPE html>
<html>
<body>

<%
somedate="10/30/99"
response.write(IsDate(somedate))
%>

</body>
</html>

其他函数(VBScript)

大写或小写的字符串

<!DOCTYPE html>
<html>
<body>

<%
name = "Bill Gates"
response.write(ucase(name))
response.write("<br>")
response.write(lcase(name))
%>

</body>
</html>

字符串去掉前后空格

<!DOCTYPE html>
<html>
<body>

<%
name = " jianzhan "
response.write("visit" & name & "now<br>")
response.write("visit" & trim(name) & "now<br>")
response.write("visit" & ltrim(name) & "now<br>")
response.write("visit" & rtrim(name) & "now")
%>

</body>
</html>

如何转化一个字符串?

<!DOCTYPE html>
<html>
<body>

<%
sometext = "Hello Everyone!"
response.write(strReverse(sometext))
%>

</body>
</html>

四舍五入

<!DOCTYPE html>
<html>
<body>

<%
i = 48.66776677
j = 48.3333333
response.write(Round(i))
response.write("<br>")
response.write(Round(j))
%>

</body>
</html>

一个随机数

<!DOCTYPE html>
<html>
<body>

<%
randomize()
response.write(rnd())
%>

</body>
</html>

从一个字符串返回特定的字符

<!DOCTYPE html>
<html>
<body>

<%
sometext="Welcome to this Web"
response.write(Left(sometext,5))
response.write("<br>")
response.write(Right(sometext,5))
%>

</body>
</html>

替换字符串的特定字符

<!DOCTYPE html>
<html>
<body>

<%
sometext="Welcome to this Web!!"
response.write(Replace(sometext, "Web", "Page"))
%>

</body>
</html>

从字符串中返回指定数目的字符

<!DOCTYPE html>
<html>
<body>

<%
sometext="Welcome to this Web!!"
response.write(Mid(sometext, 9, 2))
%>

</body>
</html>

程序

调用使用VBScript的子程序

<!DOCTYPE html>
<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>

<body>
<p>You can call a procedure like this:</p>
<p>Result: <%call vbproc(3,4)%></p>
<p>Or, like this:</p>
<p>Result: <%vbproc 3,4%></p>
</body>
</html>

调用使用JavaScript的子程序

<%@ language="javascript" %>
<!DOCTYPE html>
<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head>

<body>
<p>Result: <%jsproc(3,4)%></p>
</body>
</html>

调用使用VBScript和JavaScript的子程序

<!DOCTYPE html>
<html>
<head>
<%
sub vbproc(num1,num2)
Response.Write(num1*num2)
end sub
%>
<script  language="javascript" runat="server">
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
</script>
</head>

<body>
<p>Result: <%call vbproc(3,4)%></p>
<p>Result: <%call jsproc(3,4)%></p>
</body>

</html>

表单

使用method="get"的表单

<!DOCTYPE html>
<html>
<body>
<form action="demo_reqquery.asp" method="get">
Your name: <input type="text" name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.QueryString("fname")
If fname<>"" Then
      Response.Write("Hello " & fname & "!<br>")
      Response.Write("How are you today?")
End If
%>
</body>
</html>

使用method="post"的表单

<!DOCTYPE html>
<html>
<body>
<form action="demo_simpleform.asp" method="post">
Your name: <input type="text" name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
      Response.Write("Hello " & fname & "!<br>")
      Response.Write("How are you today?")
End If
%>
</body>
</html>

使用单选按钮的表单

<!DOCTYPE html>
<html>
<%
dim cars
cars=Request.Form("cars")
%>
<body>
<form action="demo_radiob.asp method="post">
<p>Please select your favorite car:</p>

<input type="radio" name="cars"
<%if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo</input>
<br>
<input type="radio" name="cars"
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab</input>
<br>
<input type="radio" name="cars"
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW</input>
<br><br>
<input type="submit" value="Submit" />
</form>
<%
if cars<>"" then
   Response.Write("<p>Your favorite car is: " & cars & "</p>")
end if
%>
</body>
</html>

Cookie

Welcome cookie

<%
dim numvisits
response.cookies("NumVisits").Expires=date+365 
numvisits=request.cookies("NumVisits")

if numvisits="" then
   response.cookies("NumVisits")=1
   response.write("Welcome! This is the first time you are visiting this Web page.")
else
   response.cookies("NumVisits")=numvisits+1
   response.write("You have visited this ")
   response.write("Web page " & numvisits)
   if numvisits=1 then
     response.write " time before!"
   else
     response.write " times before!"
   end if
end if
%>
<!DOCTYPE html>
<html>
<body>
</body>
</html>

Response 对象

使用 ASP 写文本
本例演示如何使用 ASP 来写文本。

<!DOCTYPE html>
<html>
<body>

<%
response.write("Hello World!")
%>

</body>
</html>
Hello World!

在 ASP 中使用 HTML 标签格式化文本
本例演示如何使用 ASP 将文本和 HTML 标签结合起来。

<!DOCTYPE html>
<html>
<body>
<%
response.write("<h2>You can use HTML tags to format the text!</h2>")
%>

<%
response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")
%>
</body>
</html>

You can use HTML tags to format the text!

This text is styled with the style attribute!

将用户重定向至一个不同的 URL
本例演示如何将用户重定向至一个不同的 URL。

<%
if Request.Form("select")<>"" then
       Response.Redirect(Request.Form("select"))
end if
%>   

<!DOCTYPE html>
<html>
<body>

<form action="demo_redirect.asp" method="post">

<input type="radio" name="select" 
value="demo_server.asp">
Server Example<br>

<input type="radio" name="select" 
value="demo_text.asp">
Text Example<br><br>
<input type="submit" value="Go!">

</form>

</body>
</html>

显示随机的链接
本例演示如何创建一个随机的链接。

<!DOCTYPE html>
<html>
<body>

<%
randomize()
r=rnd()
if r>0.5 then
  response.write("<a href='http://www.shouce.ren'>jianzhan.cc!</a>")
else
  response.write("<a href='http://www.refsnesdata.no'>Refsnesdata.no!</a>")
end if
%>

<p>
This example demonstrates a link, each time you load the page, it will display 
one of two links: jianzhan.cc! OR Refsnesdata.no! There is a 50% chance for 
each of them.
</p>

</body>
</html>

控制缓冲区
本例演示如何控制缓冲区。

<%
Response.Buffer=true
%>
<!DOCTYPE html>
<html>
<body>
<p>
This text will be sent to your browser when my response buffer is flushed.
</p>
<%
Response.Flush
%>
</body>
</html>

清空缓冲区
本例演示如何清空缓冲区。

<%
Response.Buffer=true
%>
<!DOCTYPE html>
<html>
<body>
<p>This is some text I want to send to the user.</p>
<p>No, I changed my mind. I want to clear the text.</p>
<%
Response.Clear
%>
</body>
</html>

在处理过程中终止脚本并返回结果
本例演示如何在处理过程中终止脚本。

<!DOCTYPE html>
<html>
<body>
<p>I am writing some text. This text will never be<br>
<%
Response.End
%>
finished! It's too late to write more!</p>
</body>
</html>

设置页面在失效前在浏览器中缓存时间
本例演示如何规定页面在失效前在浏览器中的缓存时间。

<%Response.Expires=-1%>
<!DOCTYPE html>
<html>
<body>
<p>This page will be refreshed with each access!</p>
</body>
</html>

设置页面缓存在浏览器中的失效日期或时间
本例演示如何规定页面缓存在浏览器中的失效时间日期或时间。

<%Response.Expires=-1%>
<!DOCTYPE html>
<html>
<body>
<p>This page will be refreshed with each access!</p>
</body>
</html>

检查用户是否仍然与服务器连接
本例演示如何检查用户是否已与服务器断开。

<!DOCTYPE html>
<html>
<body>

<%
If Response.IsClientConnected=true then
Response.Write("The user is still connected!")
else
Response.Write("The user is not connected!")
end if
%>

</body>
</html>

设置内容类型
本例演示如何规定内容的类型。

<%
Response.ContentType="text/html"
%>
<!DOCTYPE html>
<html>
<body>

<p>This is some text</p>

</body>
</html>

设置字符集名称
本例演示如何规定字符集的名称。

<%
Response.Charset="ISO8859-1"
%>
<!DOCTYPE html>
<html>
<body>

<p>This is some text</p>

</body>
</html>

Request 对象

当用户点击链接时发送查询信息
本例演示如何在链接中向页面发送查询信息,并在目标页面中取回这些信息(在本例中是同一页面)。

<!DOCTYPE html>
<html>
<body>

<a href="demo_simplequerystring.asp?color=green">Example</a>

<%
Response.Write(Request.QueryString)
%>

</body>
</html>

QueryString 集合的简单应用
本例演示如何使用 QueryString 集合从表单取回值。(此表单使用 GET 方法,这意味着所发送的信息对用户来说是可见的。)

<!DOCTYPE html>
<html>
<body>

<form action="demo_simplereqquery.asp" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

<%
Response.Write(Request.QueryString)
%>

</body>
</html>

如何使用来自表单的信息
本例演示如何使用从表单取回的值。此表单使用 GET 方法。

<!DOCTYPE html>
<html>
<body>

<form action="demo_simplereqquery.asp" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

<%
Response.Write(Request.QueryString)
%>

</body>
</html>

来自表单的更多信息
本例演示如果输入字段包含若干相同的名称,QueryString 集合会包含什么内容。它将展示如何使用 Count 关键词来对 "name" 属性进行计数。此表单使用 GET 方法。

<!DOCTYPE html>
<html>
<body>

<%
If Request.QueryString<>"" Then
      If Request.QueryString("name")<>", " Then
           name1=Request.QueryString("name")(1)
           name2=Request.QueryString("name")(2)
      end if
end if
%>

<form action="demo_reqquery2.asp" method="get">
First name:
<input type="text" name="name" value="<%=name1%>" />
<br>
Last name:
<input type="text" name="name" value="<%=name2%>" />
<br>
<input type="submit" value="Submit" />
</form>
<hr>
<%
If Request.QueryString<>"" Then
      Response.Write("<p>")
      Response.Write("The information received from the form was:")
      Response.Write("</p><p>")
      Response.Write("name=" & Request.QueryString("name"))
      Response.Write("</p><p>")
      Response.Write("The name property's count is: ")
      Response.Write(Request.QueryString("name").Count)
      Response.Write("</p><p>")
      Response.Write("First name=" & name1)
      Response.Write("</p><p>")
      Response.Write("Last name=" & name2)
      Response.Write("</p>")
end if
%>
</body>
</html>

在线实例

Form 集合的简单应用
本例演示如何使用 Form 集合从表单取回值。(此表单使用 POST 方法,这意味着所发送的信息对用户来说是不可见的。)

<!DOCTYPE html>
<html>
<body>

<form action="demo_simpleform1.asp" method="post">
First name:
<input type="text" name="fname" value="Donald" />
<br>
Last name:
<input type="text" name="lname" value="Duck" />
<br>
<input type="submit" value="Submit" />
</form>

<%
Response.Write(Request.Form)
%>

</body>
</html>

如何使用来自表单的信息
本例演示如何使用从表单取回的值。此表单使用了 POST 方法。

<!DOCTYPE html>
<html>
<body>
<form action="demo_simpleform.asp" method="post">
Your name: <input type="text" name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
      Response.Write("Hello " & fname & "!<br>")
      Response.Write("How are you today?")
End If
%>
</body>
</html>

来自表单的更多信息
本例演示如果输入字段包含若干相同的名称,Form 集合会包含什么内容。它将展示如何使用 Count 关键词来对 "name" 属性进行计数。此表单使用了 POST 方法。

<!DOCTYPE html>
<html>
<body>

<form action="demo_form2.asp" method="post">
First name:
<input type="text" name="name" value="Donald" />
<br>
Last name:
<input type="text" name="name" value="Duck" />
<br>
<input type="submit" value="Submit" />
</form>
<hr>

<p>The information received from the form above was:</p>
<%
If Request.Form("name")<>"" Then
      Response.Write("<p>")
      Response.Write("name=" & Request.Form("name"))
      Response.Write("</p><p>")
      Response.Write("The name property's count is: ")
      Response.Write(Request.Form("name").Count)
      Response.Write("</p><p>")
      Response.Write("First name=" & Request.Form("name")(1))
      Response.Write("</p><p>") 
      Response.Write("Last name=" & Request.Form("name")(2))
      Response.Write("</p>") 
End if
%>

</body>
</html>

带有单选按钮的表单
本例演示如何使用 Form 集合通过单选按钮与用户进行交互。此表单使用 POST 方法。

<!DOCTYPE html>
<html>
<%
dim cars
cars=Request.Form("cars")
%>
<body>
<form action="demo_radiob.asp method="post">
<p>Please select your favorite car:</p>

<input type="radio" name="cars"
<%if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo</input>
<br>
<input type="radio" name="cars"
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab</input>
<br>
<input type="radio" name="cars"
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW</input>
<br><br>
<input type="submit" value="Submit" />
</form>
<%
if cars<>"" then
   Response.Write("<p>Your favorite car is: " & cars & "</p>")
end if
%>
</body>
</html>

带有复选框的表单
本例演示如何使用 Form 集合通过复选框与用户进行交互。此表单使用 POST 方法。

<!DOCTYPE html>
<html>
<body>
<%
fruits=Request.Form("fruits")
%>

<form action="demo_checkboxes.asp" method="post">
<p>Which of these fruits do you prefer:</p>
<input type="checkbox" name="fruits" value="Apples"
<%if instr(fruits,"Apple") then Response.Write("checked")%>>
Apple
<br>
<input type="checkbox" name="fruits" value="Oranges"
<%if instr(fruits,"Oranges") then Response.Write("checked")%>>
Orange
<br>
<input type="checkbox" name="fruits" value="Bananas"
<%if instr(fruits,"Banana") then Response.Write("checked")%>>
Banana
<br>
<input type="submit" value="Submit">
</form>
<%
if fruits<>"" then%>
   <p>You like: <%Response.Write(fruits)%></p>
<%end if
%>

</body>
</html>

在线实例

获取服务器变量
本例演示如何取得访客的浏览器类型、IP 地址等信息。

<!DOCTYPE html>
<html>
<body>
<p>
<b>You are browsing this site with:</b>
<%Response.Write(Request.ServerVariables("http_user_agent"))%>
</p>
<p>
<b>Your IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_addr"))%>
</p>
<p>
<b>The DNS lookup of the IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_host"))%>
</p>
<p>
<b>The method used to call the page:</b>
<%Response.Write(Request.ServerVariables("request_method"))%>
</p>
<p>
<b>The server's domain name:</b>
<%Response.Write(Request.ServerVariables("server_name"))%>
</p>
<p>
<b>The server's port:</b>
<%Response.Write(Request.ServerVariables("server_port"))%>
</p>
<p>
<b>The server's software:</b>
<%Response.Write(Request.ServerVariables("server_software"))%>
</p>

</body>
</html>
You are browsing this site with:
Your IP address is: 42.120.45.233
The DNS lookup of the IP address is: 42.120.45.233
The method used to call the page: GET
The server's domain name: shouce.ren
The server's port: 80
The server's software: Microsoft-IIS/7.5

创建 welcome cookie
本例演示如何创建一个 Welcome Cookie。

<%
dim numvisits
response.cookies("NumVisits").Expires=date+365 
numvisits=request.cookies("NumVisits")

if numvisits="" then
   response.cookies("NumVisits")=1
   response.write("Welcome! This is the first time you are visiting this Web page.")
else
   response.cookies("NumVisits")=numvisits+1
   response.write("You have visited this ")
   response.write("Web page " & numvisits)
   if numvisits=1 then
     response.write " time before!"
   else
     response.write " times before!"
   end if
end if
%>
<!DOCTYPE html>
<html>
<body>
</body>
</html>

探测用户发送的字节总数
本例演示如何探测用户在 Request 对象中发送的字节总数。

<!DOCTYPE html>
<html>
<body>

<form action="demo_totalbytes.asp" method="post">
Please type something:
<input type="text" name="txt"><br><br>
<input type="submit" value="Submit">
</form>

<%
If Request.Form("txt")<>"" Then
   Response.Write("You submitted: ")
   Response.Write(Request.Form)
   Response.Write("<br><br>")
   Response.Write("Total bytes: ")
   Response.Write(Request.Totalbytes)
End If
%>

</body>
</html>

Session 对象

设置并返回 LCID
本例演示 "LCID" 属性。该属性设置并返回一个指示位置或者地区的整数。类似于日期、时间和货币等内容都要根据位置或者地区来显示。

<!DOCTYPE html>
<html>
<body>

<%  
response.write("<p>")
response.write("The default LCID for this page is: " & Session.LCID & "<br>")
response.write("The Date format for the above LCID is: " & date() & "<br>")
response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
response.write("</p>")

Session.LCID=1036

response.write("<p>")
response.write("The LCID is now changed to: " & Session.LCID & "<br>")
response.write("The Date format for the above LCID is: " & date() & "<br>")
response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
response.write("</p>")

Session.LCID = 3079

response.write("<p>")
response.write("The LCID is now changed to: " & Session.LCID & "<br>")
response.write("The Date format for the above LCID is: " & date() & "<br>")
response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
response.write("</p>")

Session.LCID = 2057

response.write("<p>")
response.write("The LCID is now changed to: " & Session.LCID & "<br>")
response.write("The Date format for the above LCID is: " & date() & "<br>")
response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
response.write("</p>")
%>

</body>
</html>
The default LCID for this page is: 1033
The Date format for the above LCID is: 9/20/2013
The Currency format for the above LCID is: $350.00
The LCID is now changed to: 1036
The Date format for the above LCID is: 20/09/2013
The Currency format for the above LCID is: 350,00 €
The LCID is now changed to: 3079
The Date format for the above LCID is: 20.09.2013
The Currency format for the above LCID is: € 350,00
The LCID is now changed to: 2057
The Date format for the above LCID is: 20/09/2013
The Currency format for the above LCID is: £350.00

返回 SessionID
本例演示 "SessionID" 属性。该属性为每位用户返回一个唯一的 id。这个 id 由服务器生成。

<!DOCTYPE html>
<html>
<body>

<%
Response.Write(Session.SessionID)
%>

</body>
</html>
619100248

session 的超时
本例演示 "Timeout" 属性。该属性设置并返回 session 的超时时间(分钟)。

<!DOCTYPE html>
<html>
<body>

<%  
response.write("<p>")
response.write("Default Timeout is: " & Session.Timeout & " minutes.")
response.write("</p>")

Session.Timeout=30

response.write("<p>")
response.write("Timeout is now: " & Session.Timeout & " minutes.")
response.write("</p>")
%>

</body>
</html>
Default Timeout is: 20 minutes.
Timeout is now: 30 minutes.

Server 对象

此文件最后被修改的时间是?
探测文件的最后修改时间。

<!DOCTYPE html>
<html>
<body>

<%
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set rs = fs.GetFile(Server.MapPath("demo_lastmodified.asp"))
modified = rs.DateLastModified
%>
This file was last modified on: <%response.write(modified)
Set rs = Nothing
Set fs = Nothing
%>

</body>
</html>

打开并读取某个文本文件
打开文件 "Textfile.txt" 以供读取。

<!DOCTYPE html>
<html>
<body>

<%
Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("text") & "\TextFile.txt",1)
While not rs.AtEndOfStream
      Response.Write RS.ReadLine
      Response.Write("<br>")
Wend 
%>

<p>
<a href="text/textfile.txt"><img src="/images/btn_view_text.gif"></a>
</p>

</body>
</html>

自制的点击计数器

<%
Set FS=Server.CreateObject("Scripting.FileSystemObject")
Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
fcount=RS.ReadLine
RS.Close

fcount=fcount+1

'This code is disabled due to the write access security on our server:
'Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
'RS.Write fcount
'RS.Close

Set RS=Nothing
Set FS=Nothing

%>
<!DOCTYPE html>
<html>
<body>
<p>
This page has been visited <%=fcount%>  times.
</p>
</body>
</html>

FileSystem 对象

指定的文件存在吗?
本例演示如何检查某个文件是否存在。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

If (fs.FileExists("c:\winnt\cursors\3dgarro.cur"))=true Then
      Response.Write("File c:\winnt\cursors\3dgarro.cur exists.")
Else
      Response.Write("File c:\winnt\cursors\3dgarro.cur does not exist.")
End If

set fs=nothing
%>

</body>
</html>

指定的文件夹存在吗?
本例演示如何检查某个文件夹是否存在。

<!DOCTYPE html>
<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

If fs.FolderExists("c:\temp") = true Then
      Response.Write("Folder c:\temp exists.")
Else
      Response.Write("Folder c:\temp does not exist.")
End If

set fs=nothing
%>

</body>
</html>

指定的驱动器存在吗?
本例演示如何检查某个驱动器是否存在。

<!DOCTYPE html>
<html>
<body>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

if fs.driveexists("c:") = true then
      Response.Write("Drive c: exists.")
Else
      Response.Write("Drive c: does not exist.")
End If

Response.write("<br>")

if fs.driveexists("g:") = true then
      Response.Write("Drive g: exists.")
Else
      Response.Write("Drive g: does not exist.")
End If

set fs=nothing
%>

</body>
</html>

取得某个指定驱动器的名称
本例演示如何获取某个指定的驱动器的名称。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetDriveName("c:\winnt\cursors\3dgarro.cur")

Response.Write("The drive name is: " & p)

set fs=nothing
%>

</body>
</html>

取得某个指定路径的父文件夹的名称
本例演示如何获取某个指定的路径的父文件夹的名称。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")

Response.Write("The parent folder name of c:\winnt\cursors\3dgarro.cur is: " & p)

set fs=nothing
%>

</body>
</html>

取得文件名
本例演示如何获取指定的路径中的最后一个成分的文件名。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write("The file name of the last component is: ")
Response.Write(fs.GetFileName("c:\winnt\cursors\3dgarro.cur"))
set fs=nothing
%>


</body>
</html>

取得文件扩展名
本例演示如何获取指定的路径中的最后一个成分的文件扩展名。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write("The file extension of the file 3dgarro is: ")
Response.Write(fs.GetExtensionName("c:\winnt\cursors\3dgarro.cur"))

set fs=nothing
%>

</body>
</html>

取得文件或文件夹的基名称
本例演示如何获取指定的路径中文件或者文件夹的基名称。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Response.Write(fs.GetBaseName("c:\winnt\cursors\3dgarro.cur"))
Response.Write("<br>")
Response.Write(fs.GetBaseName("c:\winnt\cursors\"))
Response.Write("<br>")
Response.Write(fs.GetBaseName("c:\winnt\"))

set fs=nothing
%>

</body>
</html>

TextStream 对象

读取文本文件
本例演示如何从文本文件中读取内容。

<!DOCTYPE html>
<html>
<body>
<p>This is the text in the text file:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.ReadAll)
f.Close

Set f=Nothing
Set fs=Nothing
%>
</body>
</html>

读取文本文件中的一个部分
本例演示如何仅仅读取一个文本流文件的部分内容。

<!DOCTYPE html>
<html>
<body>
<p>This is the first five characters from the text file:</p>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.Read(5))
f.Close

Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

读取文本文件中的一行
本例演示如何从一个文本流文件中读取一行内容。

<!DOCTYPE html>
<html>
<body>
<p>This is the first line of the text file:</p>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.ReadLine)
f.Close

Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

读取文本文件的所有行
本例演示如何从文本流文件中读取所有的行。

<!DOCTYPE html>
<html>
<body>
<p>This is all the lines in the text file:</p>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)

do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write("<br>")
loop

f.Close
Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

略过文本文件中的一部分
本例演示如何在读取文本流文件时跳过指定的字符数。

<!DOCTYPE html>
<html>
<body>
<p>The first four characters in the text file are skipped:</p>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
f.Skip(4)
Response.Write(f.ReadAll)
f.Close

Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

略过文本文件中的一行
本例演示如何在读取文本流文件时跳过一行。

<!DOCTYPE html>
<html>
<body>
<p>The first line in the text file is skipped:</p>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
f.SkipLine
Response.Write(f.ReadAll)
f.Close

Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

返回行数
本例演示如何返回在文本流文件中的当前行号。

<!DOCTYPE html>
<html>
<body>
<p>This is all the lines in the text file (with line numbers):</p>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)

do while f.AtEndOfStream = false
Response.Write("Line:" & f.Line & " ")
Response.Write(f.ReadLine)
Response.Write("<br>")
loop

f.Close
Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

取得列数
本例演示如何取得在文件中当前字符的列号。

<!DOCTYPE html>
<html>
<body>

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")

Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.Read(2))
Response.Write("<p>The cursor is now standing in position " & f.Column & " in the text file.</p>")
f.Close

Set f=Nothing
Set fs=Nothing
%>

</body>
</html>

Drive 对象

取得指定驱动器的总容量
本例演示如何使用 TotalSize 属性来获得指定驱动器的总容量。

<!DOCTYPE html>
<html>
<body>

<%
Dim fs,d,n
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set d=fs.GetDrive("c:")
n = "Drive: " & d
n = n & "<br>Total size in bytes: " & d.TotalSize
Response.Write(n)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的可用空间数
本例演示如何首先创建一个 FileSystemObject 对象,然后使用 AvailableSpace 属性来获得指定驱动器的可用空间。

<!DOCTYPE html>
<html>
<body>

<%
Dim fs, d, n
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set d=fs.GetDrive("c:")
n = "Drive: " & d
n = n & "<br>Available Space in bytes: " & d.AvailableSpace
Response.Write(n)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的剩余空间容量
本例演示如何使用 FreeSpace 空间属性来取得指定驱动器的剩余空间。

<!DOCTYPE html>
<html>
<body>

<%
Dim fs, d, n
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set d=fs.GetDrive("c:")
n = "Drive: " & d
n = n & "<br>Free Space in bytes: " & d.FreeSpace
Response.Write(n)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的驱动器字母
本例演示如何使用 DriveLetter 属性来获得指定驱动器的驱动器字母。

<!DOCTYPE html>
<html>
<body>

<%
dim fs, d, n
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
Response.Write("The drive letter is: " & d.driveletter)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的驱动器类型
本例演示如何使用 DriveType 属性来获得指定驱动器的驱动器类型。

<!DOCTYPE html>
<html>
<body>

<%
dim fs, d, n
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
Response.Write("The drive type is: " & d.DriveType)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的文件系统信息
本例演示如何使用 FileSystem 来取得指定驱动器的文件系统信息。

<!DOCTYPE html>
<html>
<body>

<%
dim fs, d, n
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
Response.Write("The file system is: " & d.FileSystem)
set d=nothing
set fs=nothing
%>

</body>
</html>

驱动器是否已就绪?
本例演示如何使用 IsReady 属性来检查指定的驱动器是否已就绪。

<!DOCTYPE html>
<html>
<body>

<%
dim fs,d,n
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
n = "The " & d.DriveLetter
if d.IsReady=true then 
    n = n & " drive is ready."
else
    n = n & " drive is not ready."
end if 
Response.Write(n)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的路径
本例演示如何使用 Path 属性来取得指定驱动器的路径。

<!DOCTYPE html>
<html>
<body>

<%
dim fs,d
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
Response.Write("The path is " & d.Path)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的根文件夹
本例演示如何使用 RootFolder 属性来取得指定驱动器的根文件夹。

<!DOCTYPE html>
<html>
<body>

<%
dim fs,d
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
Response.Write("The rootfolder is " & d.RootFolder)
set d=nothing
set fs=nothing
%>

</body>
</html>

取得指定驱动器的序列号
本例演示如何使用 Serialnumber 属性来取得指定驱动器的序列号。

<!DOCTYPE html>
<html>
<body>

<%
dim fs,d
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
Response.Write("The serialnumber is " & d.SerialNumber)
set d=nothing
set fs=nothing
%>

</body>
</html>

File 对象

文件最后被修改的时间?
本例演示如何使用 DateLastModified 属性来取得指定文件最后被修改的日期和时间。

<!DOCTYPE html>
<html>
<body>

<%
dim fs, f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile(Server.MapPath("testread.txt"))
Response.Write("The file testread.txt was last modified on: " & f.DateLastModified)
set f=nothing
set fs=nothing
%>

</body>
</html>

文件最后被访问的时间?
此例演示如何使用 DateLastAccessed 属性来取得指定文件最后被访问的日期和时间。

<!DOCTYPE html>
<html>
<body>

<%
dim fs, f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile(Server.MapPath("testread.txt"))
Response.Write("The file testread.txt was last accessed on: " & f.DateLastAccessed)
set f=nothing
set fs=nothing
%>

</body>
</html>

返回指定文件的属性
本例演示如何使用 Attributes 来返回指定文件的属性。

<!DOCTYPE html>
<html>
<body>

<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile(Server.MapPath("testread.txt"))
Response.Write("The attributes of the file testread.txt are: " & f.Attributes)
set f=nothing
set fs=nothing
%>

</body>
</html>

Dictionary 对象

指定的键存在吗?
本例演示如何创建一个 Dictionary 对象,然后使用 Exists 方法来检查指定的键是否存在。

<!DOCTYPE html>
<html>
<body>

<%
dim d
set d=Server.CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
if d.Exists("n")= true then
    Response.Write("Key exists.")
else
    Response.Write("Key does not exist.")
end if
set d=nothing
%>

</body>
</html>

返回一个所有项目的数组
本例演示如何使用 Items 方法来返回一个所有项目的数组。

<!DOCTYPE html>
<html>
<body>

<%
dim d,a,i,s
set d=Server.CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"

Response.Write("<p>The values of the items are:</p>")
a=d.Items
for i = 0 To d.Count -1
    s = s & a(i) & "<br>"
next
Response.Write(s)

set d=nothing
%>

</body>
</html>

返回一个所有键的数组
本例演示如何使用 Keys 方法来返回一个所有键的数组。

<!DOCTYPE html>
<html>
<body>

<%
dim d,a,i,s
set d=Server.CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("<p>The value of the keys are:</p>")
a=d.Keys
for i = 0 To d.Count -1
    s = s & a(i) & "<br>"
next
Response.Write(s)
set d=nothing
%>

</body>
</html>

返回一个项目的值
本例演示如何使用 Item 属性来返回一个项目的值。

<!DOCTYPE html>
<html>
<body>

<%
dim d
set d=Server.CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("The value of the item n is: " & d.item("n"))
set d=nothing
%>

</body>
</html>

设置一个键
本例演示如何使用 Key 属性来在 Dictionary 对象中设置一个键。

<!DOCTYPE html>
<html>
<body>

<%
dim d
set d=Server.CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
d.Key("i") = "it"
Response.Write("The key i has been set to it, and the value is: " & d.Item("it"))
set d=nothing
%>

</body>
</html>

返回键/项目对的数量
本例演示如何使用 Count 属性来返回键/项目对的数量。

<!DOCTYPE html>
<html>
<body>

<%
dim d, a, s, i
set d=Server.CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("The number of key/item pairs is: " & d.Count)
set d=nothing
%>

</body>
</html>

上一篇: 下一篇: