ASP入门(七)-Response小案例

我们通过ASP来创建一个年月日的选择框,年份从1950到2000年,如果手动输入HTML代码,其中的<option>列表项目要写94个 (51年 + 12月 + 31天),很是繁琐。

代码比较简单如下:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit
Response.Buffer = True
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>出生日期选择功能</title>
</head>
<body>
<%
Dim i
'先输出年份的选择框
Response.Write("<select id=""year"" name=""year"">")
For i = 1950 To 2000
  Response.Write("<option value=""" & i & """>" & i & "</option>")
Next
Response.Write("</select>")
Response.Flush() '避免用户长时间等待而没有任何信息

'输出月份的选择框
Response.Write("<select id=""month"" name=""month"">")
For i = 1 To 12
  Response.Write("<option value=""" & i & """>" & i & "</option>")
Next
Response.Write("</select>")
Response.Flush() '避免用户长时间等待而没有任何信息

'输出日期的选择框
Response.Write("<select id=""day"" name=""day"">")
For i = 1 To 31
  Response.Write("<option value=""" & i & """>" & i & "</option>")
Next
Response.Write("</select>")
Response.Flush() '避免用户长时间等待而没有任何信

%>
</body>
</html>

总的来说有三个循环,分别为年、月、日。这里面需要注意的是Flush方法和Buffer属性的使用,我们首先开启了Buffer缓冲,以避免在循环中每Write一次就向客户款输出一次信息而消耗大量连接资源,而后为了避免循环耗费的时间过长使得用户长时间等待而没有任何信息,因此在每次循环完有一个Flush方法来立即输出缓冲。

代码的最终效果如下:

20150812017

示例代码下载:

ResponseSample.rar

posted on 2015-08-13 01:29  pchmonster  阅读(1218)  评论(0编辑  收藏  举报

导航