第10章 ASP 应用程序

188
第 10 第 ASP 第第第第 10.1 第第第第第第第第第第第第第第 10.2 第第第第第第第 10.3 第第第第第第 10.4 第第第第第第第第第第第 10.5 第第第第第第第第第第 10.6 第第第第第

description

第10章 ASP 应用程序. 10.1 在不同时段显示不同的页面内容 10.2 搜索引擎的使用 10.3 进行注册登记 10.4 将数据库的内容分页显示 10.5 选择数据库表中的数据 10.6 欢乐聊天室. 10.7 一个简单的留言簿 10.8 题库的输入、搜索和修改程序 10.9 进行投票统计的程序 习题. 10.1 在不同时段显示不同的页面内容. [例10-1]建立一个自动监测浏览时间、并根据不同时段动态显示不同页面内容的 ASP 程序。该程序的文件名为10\|1. asp。 - PowerPoint PPT Presentation

Transcript of 第10章 ASP 应用程序

Page 1: 第10章      ASP 应用程序

第 10 章 ASP 应用程序

10.1 在不同时段显示不同的页面内容

10.2 搜索引擎的使用

10.3 进行注册登记

10.4 将数据库的内容分页显示

10.5 选择数据库表中的数据

10.6 欢乐聊天室

Page 2: 第10章      ASP 应用程序

10.7一个简单的留言簿

10.8题库的输入、搜索和修改程序

10.9进行投票统计的程序

习题

Page 3: 第10章      ASP 应用程序

10.1 在不同时段显示不同的页面内容[ 例 10-1] 建立一个自动监测浏览时间、并根据不同时

段动态显示不同页面内容的 ASP 程序。该程序的文件名为 10\|1.asp 。

<%@ Language=″VBScript″ %><html><body bgColor=Plum><font color=″Green″><% if time <#12:00:00# and Time>= #06:00:00# then

%>早上好,很高兴见到你 !

Page 4: 第10章      ASP 应用程序

<img src=″ 花 2.gif ″ width=″120″ height=″90″> <% elseif Time < #15:00:00# and Time >= #12:00:00# then

%> 中午好,很高兴见到你 ! <img src=″ 人 .gif ″ width=″120″ height=″89″> <% elseif Time < #19:00:00# and Time >= #15:00:00# then

%> 下午好,很高兴见到你 ! <img src=″ 动物 .gif ″ width=″120″ height=″90″> <% elseif Time < #23:00:00# and Time >= #19:00:00# then

%> 晚上好,很高兴见到你 ! <img src=″4.gif ″ width=″120″ height=″90″>

Page 5: 第10章      ASP 应用程序

<% else %>

太晚了,你该休息啦 !

<img src=″2.gif ″ width=″123″ height=″86″>

<% end if %>

</body>

</html>

• 10.1.asp 执行的结果如图 10.1 所示。

Page 6: 第10章      ASP 应用程序

图 10.1

Page 7: 第10章      ASP 应用程序

10.2 搜索引擎的使用[ 例 10-2] 建立一个利用其他搜索引擎进行搜索

的 ASP 程序。该程序的文件名为 10\|2.asp 。<%@ Language=VBScript %>

<%

dim strKey

if Request.Form(″CmdSearch″) = ″ 开始搜索″ then

strKey = Request.Form(″TxtKey″)

if strKey <> ″″ then

Page 8: 第10章      ASP 应用程序

select case Request.Form(″CboEngine″)

case ″163″

Response.Redirect ″http://search.163.com/cgi bin/search/engine/search.fcgi?key=″ & strKey

case ″yahoo″

Response.Redirect ″http://search.chinese.yahoo.com/search/gb?p=″ & strKey & ″&u=B″

case ″sohu″

Response.Redirect http://search.sohu.com/cgi bin/search baidu.p? tn=sohu&si=gi&work=″ & strKey & ″&ct=0&cl=2&lm=0″

Page 9: 第10章      ASP 应用程序

case ″sina″

Response.Redirect ″http://search.sina.com.cn/cgi bin/search/search.cgi?searchkey=″ & strKey

case ″263″

Response.Redirect ″http://search.263.net/cgi bin/tsearch?func=3&p=″ & strKey

case ″cseek″

Response.Redirect ″http://www.cseek.com/cgi/srchengine.cgi?searchkey=″ & strKey

case ″gdcatv″

Page 10: 第10章      ASP 应用程序

Response.Redirect ″http://www.gdcatv.com.cn:4000/intra/owa/navigator.navi query?pagenumber=1&button=a&text1=″ & strKey

case ″EXCITE″Response.Redirect ″http://www.excite.com/search.gw?

look=default&c=web&trace=b&search=″ & strKeyend selectelseResponse.Write ″ 请先输入要搜索的关键字″end ifend if%>

Page 11: 第10章      ASP 应用程序

<html>

<head>

<meta name=″generator″ content=″Microsoft Visual Studio 6.0″>

<title>Internet 信息检索 </title>

</head>

<body bgcolor=linen>

<p align=center><font face= 宋体 size=5><strong> 搜索引擎 </strong></font></p>

<form action=SearchEngine.asp method=post name=FormSearch>

<p align=center> 要搜索的关键字 &nbsp;

Page 12: 第10章      ASP 应用程序

<input id=TxtKey name=TxtKey style=″HEIGHT: 22px; WIDTH: 167px″> </p>

<p align=center> 请选择搜索引擎 &nbsp;

<select id=CboEngine name=CboEngine style=″HEIGHT: 22px; WIDTH: 167px″>

<option value=″Title″ selected>=== 选择搜索引擎 ===</option>

<option value=″163″> 网易 163 搜索 </option>

<option value=″yahoo″> 中文雅虎搜索 </option>

<option value=″sohu″> 搜狐搜索 </option>

<option value=″sina″> 新浪搜索 </option>

<option value=″263″> 首都在线 263 搜索 </option>

Page 13: 第10章      ASP 应用程序

<option value=″cseek″> 搜索客 </option>

<option value=″gdcatv″> 视讯中文搜索 </option>

<option value=″excite″>EXCITE</option>

</select> </p>

<p align=center><font face= 宋体 ><input name=CmdSearch type=submit value=″

开始搜索″ ></p></font>

</form>

</body>

</html>

• 10.2.asp 执行的结果如图 10.2 所示。

Page 14: 第10章      ASP 应用程序

图 10.2

Page 15: 第10章      ASP 应用程序

• 这个程序可以使用户在页面上输入搜索的关键字,如“电脑硬件”,然后在“选择搜索引擎”下拉菜单中选择一个搜索引擎,最后单击“开始搜索”按钮,将上网进行搜索,如图 10.3 所示是选择“新浪搜索”搜索到的“电脑硬件”信息的页面。

• 这个程序分为两大部分,上面是 ASP 的语句,主要是建立与搜索引擎的链接以及搜索的方式,下面是 HTML 语句,主要是建立进行搜索的页面。

Page 16: 第10章      ASP 应用程序

图 10.3

Page 17: 第10章      ASP 应用程序

10.3 进行注册登记• 为了编写程序的方便,以下的例子中使用英文名字

作为程序的名字。进行注册登记的应用程序由五个文件组成: LogRegist.asp 新用户注册文件 、 LogCheck.asp 用户登录文件、 SysMain.asp 进入站点的主页面文件、数据库表文件 Tbl StudentInfo 和包含文件 ADOVBS.INC 。

• [ 例 10-3] 这是一个包含文件,文件名为ADOVBS.INC ,主要定义了应用程序中使用的变量,当哪个页面需要使用变量时即可插入该文件,这个文件就包含了很多变量的定义语句,其中大部分变量与这里的应用程序无关。这里只是为了说明包含文件的具体使用方法,可以不使用包含文件而直接在应用程序中定义变量。

Page 18: 第10章      ASP 应用程序

<%Const adOpenForwardOnly = 0Const adOpenKeyset = 1Const adOpenDynamic = 2Const adOpenStatic = 3′——CursorOptionEnum Values Const adHoldRecords = &H00000100Const adMovePrevious = &H00000200Const adAddNew = &H01000400Const adDelete = &H01000800Const adUpdate = &H01008000Const adBookmark = &H00002000Const adApproxPosition = &H00004000

Page 19: 第10章      ASP 应用程序

Const adUpdateBatch = &H00010000Const adResync = &H00020000Const adNotify = &H00040000′——LockTypeEnum Values Const adLockReadOnly = 1Const adLockPessimistic = 2Const adLockOptimistic = 3Const adLockBatchOptimistic = 4′——ExecuteOptionEnum Values Const adRunAsync = &H00000010′——ObjectStateEnum Values Const adStateClosed = &H00000000

Page 20: 第10章      ASP 应用程序

Const adStateOpen = &H00000001Const adStateConnecting = &H00000002Const adStateExecuting = &H00000004′——CursorLocationEnum Values Const adUseServer = 2Const adUseClient = 3′——DataTypeEnum Values Const adEmpty = 0Const adTinyInt = 16Const adSmallInt = 2Const adInteger = 3Const adBigInt = 20

Page 21: 第10章      ASP 应用程序

Const adUnsignedTinyInt = 17Const adUnsignedSmallInt = 18Const adUnsignedInt = 19Const adUnsignedBigInt = 21Const adSingle = 4Const adDouble = 5Const adCurrency = 6Const adDecimal = 14Const adNumeric = 131Const adBoolean = 11Const adError = 10Const adUserDefined = 132Const adVariant = 12

Page 22: 第10章      ASP 应用程序

Const adIDispatch = 9Const adIUnknown = 13Const adGUID = 72Const adDate = 7Const adDBDate = 133Const adDBTime = 134Const adDBTimeStamp = 135Const adBSTR = 8Const adChar = 129Const adVarChar = 200Const adLongVarChar = 201Const adWChar = 130

Page 23: 第10章      ASP 应用程序

Const adVarWChar = 202Const adLongVarWChar = 203Const adBinary = 128Const adVarBinary = 204Const adLongVarBinary = 205

′——EditModeEnum Values Const adEditNone = &H0000Const adEditInProgress = &H0001Const adEditAdd = &H0002Const adEditDelete = &H0004′——SearchDirection Values Const adSearchForward = 1

Page 24: 第10章      ASP 应用程序

Const adSearchBackward = 1′——ConnectPromptEnum Values Const adPromptAlways = 1Const adPromptComplete = 2Const adPromptCompleteRequired = 3Const adPromptNever = 4%>

[ 例 10-4] 新用户进行注册程序,文件名为 LogRegist.asp 。

<%@ Language=VBScript %><!-- #INCLUDE FILE=″ADOVBS.INC ″ -- ><%

Page 25: 第10章      ASP 应用程序

dim lStrSQL, lRecItem, lStrDbConnStrdim lErrNo, lErrMesdim lStrCode, lIsMod, lStudentIddim lStudent(6, 0)dim iiilStrDbConnStr = ″DSN=log″set lRecItem = Server.CreateObject(″ADODB.RecordSet

″)lErrNo = 0if Request.Form(″CmdClose″) = ″ 返回″ thenResponse.Redirect ″LogCheck.asp″end if

Page 26: 第10章      ASP 应用程序

select case Request.Form(″CmdReg″) case ″ 退出″Response.Redirect ″LogCheck.asp″case ″ 确认″if Request.Form(″txtStId″)=″ ″ -Or Request.Form(″txtStname″)=″ ″ -Or Request.Form(″txtStPws″)=″ ″ -Or Request.Form(″txtStPws2″)=″ ″ thenlErrNo = 14elseif Request.Form(″txtStPws″) <>

Request.Form(″txtStPws2″) thenlErrNo = 15

Page 27: 第10章      ASP 应用程序

end ifend iflStudent(0,0) = Request.Form(″TxtStId″)lStudent(1,0) = UCase(Request.Form(″TxtStname″))lStudent(2,0) = Request.Form(″TxtStCI″)lStudent(5,0) = Request.Form(″TxtStPws″)lStudent(3,0) = Request.Form(″TxtStUnit″)lStudent(4,0) = Request.Form(″TxtStContact″)lStudent(6,0) = Request.Form(″TxtStPhone″)lIsMod = Request.Form(″TxtMod″)if lErrNo = 0 thenlErrNo = 20

Page 28: 第10章      ASP 应用程序

lStrSQL = ″Select * From Tbl StudentInfo″& ″ Where StudentId=′″ & lStudent(0,0) & ″′;″lRecItem.Open lStrSQL,

lStrDbConnStr,adOpenDynamic,adLockOptimistic if lRecItem.EOF Or lRecItem.BOF thenlRecItem.AddNew elseif lIsMod = ″False″ thenlErrNo = 13′ 记录已经存在end ifend if

Page 29: 第10章      ASP 应用程序

if LErrNo = 20 thenlRecItem.Fields(″StudentId″) = lStudent(0,0)lRecItem.Fields(″Studentname″) = lStudent(1,0)lRecItem.Fields(″StudentCI″) = lStudent(2,0)lRecItem.Fields(″StudentPwd″) = lStudent(5,0)lRecItem.Fields(″StudentUnit″) = lStudent(3,0)lRecItem.Fields(″Contact″) = lStudent(4,0)lRecItem.Fields(″Telephone″) = lStudent(6,0)lRecItem.Update end iflRecItem.Closeend if

Page 30: 第10章      ASP 应用程序

case elselStudentId = ″ ″lIsMod = ″True″end selectset lRecItem = Nothing

select case lErrNocase 20lErrMes = ″ 您已经成功注册!″case 13lErrMes = ″ 该考号已经存在,请与系统管理员联系!″case 14

Page 31: 第10章      ASP 应用程序

lErrMes = ″ 账号、用户姓名及两次口令输入中不能有空值″

case 15

lErrMes = ″ 两次输入的口令不同″case else

ErrMes = ″ ″

end select

%>

<html>

<head>

<meta name=VI60 defaultClientScript content=VBScript>

Page 32: 第10章      ASP 应用程序

<meta name=″generator″ Content=″Microsoft Visual Studio 6.0 ″>

<title> 新用户注册 </title></head><body bgColor=silver><p align=center><font color=navy face= 宋体

size=4><%=lErrMes%></font></p><% if lErrNo <> 20 then %><p align=center><strong></strong></p><p align=center><font color=#000080 face= 宋体 size=6><strong> 新用户注册 </strong></font></p>

Page 33: 第10章      ASP 应用程序

<table align=center border=1 cellPadding=1 cellSpacing=1>

<form action=″LogRegist.asp″ id=FormReg method=post name=FormReg>

<tr><td> 账号 </td><td><input id=TxtStID name=TxtStID maxlength=15 value=

″<%=lStudent(0,0)%>″></td></tr><tr><td> 姓名 </td><td>

Page 34: 第10章      ASP 应用程序

<td><input id=TxtStname name=TxtStname maxlength=20

value=″<%=lStudent(1,0)%>″></td></tr><tr><td> 身份证号 </td><td><input id=TxtStCI name=TxtStCI maxlength=15 value=″<

%=lStudent(2,0)%>″></td></tr><tr><td> 单位 </td><td><input id=TxtStCI name=TxtStUnit value=″<

%=lStudent(3,0)%>″>

Page 35: 第10章      ASP 应用程序

</td></tr><tr><td> 联系方式 </td><td><input id=TxtStContact name=TxtStContact value=

″<%=lStudent(4,0)%>″></td></tr><tr><td> 电话 </td><td><input id=TxtStPhone name=TxtStPhone value=″<

%=lStudent(6,0)%>″></td></tr><tr>

Page 36: 第10章      ASP 应用程序

<td> 口令 </td><td><input id=TxtStPws name=TxtStPws type=password

maxlength=10></td></tr><tr><td> 口令确认 </td><td><input id=TxtStPws2 name=TxtStPws2 type=password

maxlength=10></td></tr><tr><td align=middle colSpan=2><font face= 宋体 size=3>

Page 37: 第10章      ASP 应用程序

<input accessKey=″<Enter>″ id=CmdOk name=CmdReg style=″height: 24px; width: 70px″ type=submit value=″ 确认″ width=″70″>

&nbsp;&nbsp;&nbsp; <input id=CmdExit name=CmdReg style=″height:

24px; width: 70px″ type=submit value=″ 退出″ width=″70″>

</font></td></tr></table><input id=TxtMod name=TxtMod style=″height:

22px; visibility: hidden; width: 50px″

Page 38: 第10章      ASP 应用程序

value=″<%=lIsMod%>″></form><%else %><form action=″LogRegist.asp″ method=POST

id=FormClose name=FormClose><p align=center><input type=″submit″ value=″ 返回″ id=CmdClose

name=CmdClose></p></form><%end if %></body></html>

Page 39: 第10章      ASP 应用程序

• 在浏览器地址栏中输入“ http://zhc/slp/LogRegist.asp” ,按回车键后,可看到如图 10.4所示页面,在这里填写你的“账号”、“姓名”、“口令”,并且要在“口令确认”栏再次填写你的口令,最后单击“确认”按钮,如果你填写的内容满足以上要求,可看到成功注册的页面,如图 10.5 所示,否则会让你重新填写,并会提示你应该在什么地方填写,如图 10.6 所示。

Page 40: 第10章      ASP 应用程序

图 10.4

Page 41: 第10章      ASP 应用程序

图 10.5

Page 42: 第10章      ASP 应用程序

图 10.6

Page 43: 第10章      ASP 应用程序

[ 例 10-5] 这是用户进行登录的程序,文件名为LogCheck.asp 。

<%@ Language=VBScript %><!-- #include file=″adovbs.inc″ -- ><%dim lStrSQL, lRecItem, lStrDbConnStrdim lUserId, lUserPws, lCmdFundim lErrNo, lErrMeslCmdFun = Request.Form(″CmdFun″)lErrNo = 0

select case lCmdFun

Page 44: 第10章      ASP 应用程序

case ″ 登录 ″lUserId = UCase(Request.Form(″TxtUserID″))

lUserPwd = Request.Form(″TxtUserPwd″)

lStrDbConnStr = ″DSN=log″

Set lRecItem = Server.CreateObject(″ADODB.RecordSet″)

lStrSQL = ″select * from Tbl StudentInfo where (StudentID=′″ & lUserId & ″′)″

lRecItem.Open LStrSQL, LStrDbConnStr, adOpenStatic

if lRecItem.BOF or lRecItem.EOF then

Page 45: 第10章      ASP 应用程序

lErrNo = 1′′ 没有该用户,请与系统管理员联系注册elseif lRecItem.Fields(″StudentPwd″) = lUserPwd thenSession(″StudentID″) = lUserIdSession(″Studentname″) = lRecItem.Fields(″Studentname

″)elselErrNo = 2′′ 用户口令输入错误end iflRecItem.Close set lRecItem = Nothing

Page 46: 第10章      ASP 应用程序

if lErrNo = 0 thenResponse.Redirect ″SysMain.asp″end ifcase ″ 新用户注册″Response.Redirect ″LogRegist.asp″end select

select case lErrNocase 1lErrMes = ″ 没有该用户,请先注册″case 2

Page 47: 第10章      ASP 应用程序

lErrMes = ″ 用户口令输入错误″end selectResponse.Write ″<center><big>″ & lErrMes &

″</big></center>″%>

<html><head><meta name=″generator″ Content=″Microsoft Visual

Studio 6.0″><title> 用户登录 </title></head><body bgColor=silver>

Page 48: 第10章      ASP 应用程序

<form action=″LogCheck.asp″ method=POST id=FormLog name=FormLog>

<p style=″font FAMILY: 宋体″ ><table align=center border=1 cellPadding=1

cellSpacing=1><tr><td align=middle><p>&nbsp;</p><p> 用户账号 <input id=TxtUserID

name=TxtUserID></p><p> 用户口令<input id=TxtUserPwd name=TxtUserPwd

type=password></p><p>&nbsp;</p></td></tr>

Page 49: 第10章      ASP 应用程序

<tr><td align=middle><input id=CmdLogin name=CmdFun type=submit

value=″ 登录 ″ >&nbsp;&nbsp;<input id=CmdRegist name=CmdFun

type=submit value=″ 新用户注册″ ></td></tr></table></p></form></body></html>

Page 50: 第10章      ASP 应用程序

• 当新用户注册成功后单击“返回”按钮可进入“用户登录”页面,如图 10.7 所示。一般情况下用户也是首先到这个页面,如果是第一次来这个站点要先进行注册,如果是注册过的老用户,可直接输入“账号”、“用户口令”,然后单击“登录”按钮,即可看到站点的主页了,如图 10.8 所示。

Page 51: 第10章      ASP 应用程序

图 10.7

Page 52: 第10章      ASP 应用程序

图 10.8

Page 53: 第10章      ASP 应用程序

[ 例 10-6] 这是用户登录主页的程序,文件名为SysMain.asp ,如图 10.8 所示。

<html><head><meta name=VI60 defaultClientScript content=VBScript><meta name=″generator″ Content=″Microsoft Visual

Studio 6.0″><title> 站点主页 </title></head><body bgColor=skyblue><br><br><font color=crimson face= 宋体 size=6>

Page 54: 第10章      ASP 应用程序

<center> 欢迎你来到我们的站点! </center></font><br>&nbsp;&nbsp; <img alt=″ ″ src=″file://C: \ Inetpub \ wwwroot \ asp

\ 2.gif ″><br><br><br><br><font color=navy face= 宋体 size=4><center>设计研制:北方交通大学经济管理学院 </

center></font><br><br><font color=navy face= 宋体 size=3><center>E-Mail :<a href=″mailto:[email protected]″>[email protected]</a>

Page 55: 第10章      ASP 应用程序

</center>

</font>

</body>

</html>

• 说明: 如果对站点的内容要进行限制,可使用进行注册的方式。在这里要注意: 当使用不同的数据库表时要建立新的 DSN连接,在上面的程序中建立了 DSN 为 log 的与数据库进行的新连接。

Page 56: 第10章      ASP 应用程序

10.4 将数据库的内容分页显示• 数据库里有些表的内容很多,如果想仔细浏览可使

用分页显示的方法。下面的例子 dispmulti.asp介绍了每页显示数据库表中 5 个记录的方法,并且可选择页数。

[ 例 10-7] 分别显示数据库表中记录的程序,文件名为dispmulti.asp 。

<%@ Language=VBScript %><!-- #include file=″adovbs.inc″ -- ><html><head>

Page 57: 第10章      ASP 应用程序

<meta name=″generator″ Content=″Microsoft Visual Studio 6.0″>

</head>

<body>

<%

Dim lStrScriptRepFile, lStrSQLCon, lStrFieldsnameArray(6)

Dim lIntPageSize

lStrScriptRepFile = ″ItemBaseEdit.asp″

lStrSQL = ″Select StudentID, Studentname, StudentCI, StudentUnit, Contact, Telephone, StudentPwd From Tbl StudentInfo;″

Page 58: 第10章      ASP 应用程序

lIntPageSize = 5lStrFieldsnameArray(0) = ″ 考号″lStrFieldsnameArray(1) = ″ 姓名″lStrFieldsnameArray(2) = ″ 身份证″lStrFieldsnameArray(3) = ″ 单位″lStrFieldsnameArray(4) = ″ 联系方式″lStrFieldsnameArray(5) = ″ 电话″lStrFieldsnameArray(6) = ″ 口令″

lIntPageSize = DispMultiPage(lStrSQL & lStrSQLCon, lStrFieldsnameArray, lIntPageSize, 0, lStrScriptRepFile)

%>

Page 59: 第10章      ASP 应用程序

</body></html>

<%Function DispMultiPage(vSourceSQL, vFieldsname,

vPageSize, vIdFieldNo, vStrScriptRepFile)Dim StrSQL, StrDbConnStrDim LngPageNo, LngPageSize, LngPageCount,

LngCounter, StrSelClassnameDim RecItemBaseDim LngRecNum, IntFieldsNumDim StrPad, StrScriptname, StrRefDim ii

Page 60: 第10章      ASP 应用程序

Dim StrIdFieldDim StrScriptRepFileDim StrLink

StrScriptRepFile = vStrScriptRepFileStrDbConnStr = ″DSN=st″LngPageNo = Request.QueryString(″LngPageNo″)LngPageSize = Request.QueryString(″LngPageSize″)StrSelClassname =

Request.QueryString(″ClientStrClassname″)StrSQL = vSourceSQL

Page 61: 第10章      ASP 应用程序

if LngPageNo = ″ ″ thenLngPageNo = 1end ifif LngPageSize = ″ ″ thenif vPageSize = ″ ″ thenLngPageSize = 10elseLngPageSize = vPageSizeend ifend if

On Error Resume next

Page 62: 第10章      ASP 应用程序

set RecItemBase = Server.CreateObject(″ADODB.RecordSet″)

RecItemBase.CursorLocation = adUseClient

RecItemBase.CacheSize = 5

RecItemBase.Open StrSQL, StrDbConnStr

RecItemBase.MoveFirst

RecItemBase.PageSize = LngPageSize

LngPageCount = CInt(RecItemBase.PageCount)

RecItemBase.AbsolutePage = LngPageNo

LngRecNum = 0

IntFieldsNum = RecItemBase.Fields.Count - 1

Page 63: 第10章      ASP 应用程序

if Err.Number <> 0 or (UBound(vFieldsname, 1)) <> IntFieldsNum then

′ErrMes = ″查询参数错误 !″

DispMultiPage = 1

On Error Goto 0

Exit Function

end if

Response.Write ″ 第 ″ & CStr(LngPageNo) & ″ 页 <br>″

StrIdField = RecItemBase(vIdFieldNo).name

Response.Write ″<table border = 1 style=′backgroung color: white border

Page 64: 第10章      ASP 应用程序

color=gray bordercolorlight=silver bordercolordark=Gray′><tr>″

′′Output title fields of the table firstly.for ii = 0 To IntFieldsNumResponse.Write ″<td align=center bgcolor=silver><b><strong>

″ & vFieldsname(ii) & ″</strong></b></td>″nextResponse.Write ″</tr>″

do while not RecItemBase.EOF and LngRecNum < RecItemBase.PageSize

Response.Write ″<tr>″for ii = 0 to IntFieldsNum

Page 65: 第10章      ASP 应用程序

Response.Write ″<td valign=top>″ & RecItemBase(ii) &

</td>″

next

Response.Write ″</tr>″

RecItemBase.MoveNext

LngRecNum = LngRecNum + 1

loop

RecItemBase.Close

set RecItemBase = Nothing

Response.Write ″</table><p>″

Page 66: 第10章      ASP 应用程序

StrPad = ″0″StrScriptname = Request.ServerVariables(″Script name

″)for LngCounter = 1 to LngPageCountif LngCounter >= 10 and LngCounter < 100 thenStrPad = ″0″elseIf LngCounter >= 100 thenStrPad = ″″end if

StrRef = ″<a href=′″ & StrScriptname & ″?ClientStrClassname=″ & StrSelClassname & ″&LngPageNo=″ & LngCounter

Page 67: 第10章      ASP 应用程序

StrRef = StrRef & ″&LngPageSize=″ & LngPageSize & ″′>″ & StrPad

& LngCounter & ″</a>″Response.Write StrRef & ″ ″if LngCounter Mod 20 =0 thenResponse.Write ″<br>″end ifnextDispMultiPage = 0end function%>• 图 10.9 所示为一个数据库表 Tbl StudentInfo 中的前

5 个记录,在页面的下方单击″ 03″ 链接,可打开图 10.10 所示的页面,显示了表中最后 3 个记录。

Page 68: 第10章      ASP 应用程序

图 10.9

Page 69: 第10章      ASP 应用程序

图 10.10

Page 70: 第10章      ASP 应用程序

10.5 选择数据库表中的数据• 有时你可能会选择数据库中某些数据项进行显示,

下面的例子可以帮助你进行数据项的选择。[ 例 10-8] 选择数据库中某些数据项的程序,文件名为

ListExam.asp 。

<%@ Language=VBScript %><%select case Request.Form(″CmdFun″)case ″ 确 定 ″Response.Redirect ″SysMain.asp″end select

Page 71: 第10章      ASP 应用程序

%>

<!-- #include file=″adovbs.inc″ -- ><html><head><meta name=VI60 defaultClientScript

content=VBScript><meta name=″generator″ Content=″Microsoft Visual

Studio 6.0″><title></title><%Dim lAvailable, lChosen, lAvailCount, lChosenCount,

lAvailKey, lItem

Page 72: 第10章      ASP 应用程序

Dim lAvailableSub, lChosenSub, lAvailSubCount, lChosenSubCount, lAvailSubKey

Dim StrSQL, StrDbConnStr, RecObjClassDim linitItems, linitSubItemsDim lErr, lErrMes, iii

Dim lTypeRate, lEasyRate

lErr = 0if Session(″FirstSetClass″) <> ″False″ thenStrDbConnStr = ″DSN=st″Set RecObjClass =

Server.CreateObject(″ADODB.RecordSet″)

Page 73: 第10章      ASP 应用程序

StrSQL = ″Select ObjectClass, Objectname From Tbl ObjectClass Order By

ObjectClass″

RecObjClass.Open StrSQL, StrDbConnStr, adOpenStatic

if RecObjClass.Eof Or RecObjClass.Bof then

lErr = 1

else

linitItems = RecObjClass.GetRows( , adBookmarkFirst)

Set lAvailable = Server.CreateObject(″Scripting.Dictionary″)

For iii = 0 To UBound(linitItems, 2)

lAvailable.Add linitItems(0, iii) & ″ ″ & linitItems(1, iii)

Page 74: 第10章      ASP 应用程序

″no″nextend ifRecObjClass.Close

Set Session(″Choice″) = lAvailableSession(″FirstSetClass″) = ″False″Set RecObjClass = NothingelseSet lAvailable = Session(″Choice″)lErrMes = Session(″ErrMes″)if Request.Form(″AddFromSource″) = ″增加 >>″ then

Page 75: 第10章      ASP 应用程序

for iii = 1 To Request.Form(″LstSource″).Count lItem = Request.Form(″LstSource″)(iii)lAvailable(lItem) = ″yes″nextend ifif Request.Form(″RemoveFromTarget″) = ″<< 删除″

thenfor iii = 1 To Request.Form(″LstTarget″).Count lItem = Request.Form(″LstTarget″)(iii)lAvailable(lItem) = ″no″Nextend ifSet Session(″Choice″) = lAvailable

Page 76: 第10章      ASP 应用程序

end if%>

</head><body bgColor=silver><%lChosenCount = 0lChosenSubCount = 0if lErr <> 0 thenResponse.Write ″ 无法设置试卷 , 可能由于以下原因 :″Response.Write ″<br>&nbsp;&nbsp;″ & ″1.试题类别表为

空 ;″Response.End else

Page 77: 第10章      ASP 应用程序

lAvailCount = lAvailable.Count + 1end if%><p><center><table border=1 cellPadding=1 cellSpacing=1 style=

″font FAMILY: 宋体 ; font size: medium″><form action=″ListExam.asp″ method=post

id=FormSelObj name=FormSelObj><Tbody><tr><td><strong><font

color=navy> 可选择类别名 </font></strong></td><td>&nbsp;</td>

Page 78: 第10章      ASP 应用程序

<td><font color=navy><strong>试卷涵盖类别 </strong></font></td></tr>

<tr><td><Select id=LstSource multiple name=LstSource

size=12 style=″fontfamily: 宋体 ; font size: x small; width: 200px″

Scrolling=″True″><% lAvailKey = lAvailable.Keys for iii = 0 to UBound(lAvailKey)lItem = lAvailKey(iii)if lAvailable(lItem) = ″no″ then%>

Page 79: 第10章      ASP 应用程序

<option Value=″<%=lItem%>″><%=lItem%></option><%end ifnext%></select></td><td><p>&nbsp;</p><p>&nbsp;<input id=AddFromSource

name=AddFromSource type=submit value=″增加 >>″>&nbsp;</p>

<p>&nbsp;</p>

Page 80: 第10章      ASP 应用程序

<p>&nbsp;<input id=AddFromSource name=RemoveFromTarget type=submit v

alue=″<< 删除″ >&nbsp;</p><p>&nbsp;</p></td><td><Select id=LstTarget multiple name=LstTarget

size=12 style=″fontFAMILY: 宋体 ; font size: x small; width: 200px″

scrolling=″true″><% ′lChosenCount = 0lAvailKey = lAvailable.Keysfor iii = 0 To UBound(lAvailKey)lItem = lAvailKey(iii)if lAvailable(lItem) = ″yes″ thenlChosenCount = lChosenCount + 1

Page 81: 第10章      ASP 应用程序

%><option Value=″<%=lItem%>″><%=lItem%></option><%end ifnext%></select></td></tr></form><tr><form action=″ListExam.asp″ method=POST

id=FormFun name=FormFun><td colSpan=3 align=middle><p>&nbsp;

Page 82: 第10章      ASP 应用程序

<input id=CmdCancel name=CmdFun style=″Z INDEX: 101″ type=sbmit value= ″ 确 定 ″ >&nbsp;</p></td>

</form>

</tr></Tbody>

</table>

</center>

</body>

</html>

Page 83: 第10章      ASP 应用程序

• 在图 10.11 所示页面的左边栏中显示了可选择的类别名,在其中的名称上单击可选中选择的类别,单击“增加”按钮,则将选择的类别显示在右边栏中,如图 10.12 所示。如果选择有误还可以在右边栏中选中然后单击“删除”按钮,即可将类别名从右边栏中删除。当你选择完毕后可单击“确定”按钮,可打开新的页面,如图 10.13 所示。

Page 84: 第10章      ASP 应用程序

图 10.11

Page 85: 第10章      ASP 应用程序

图 10.12

Page 86: 第10章      ASP 应用程序

图 10.13

Page 87: 第10章      ASP 应用程序

10.6 欢乐聊天室• 数据库文件为 user() 和 Content() 。[ 例 10-9] 该文件是根目录文件夹中的惟一的

Global.asa 文件,文件名为 Global.asa ,其中定义的变量和事件在该目录下的任何程序文件都可以使用,前面在介绍简单聊天室和使用计数器时也使用了该文件中的内容,现在再添加一些内容进去。

<object runat=Server scope=Session ID=MyInfo PROGID=″MSWC.MyInfo″>

</object>

Page 88: 第10章      ASP 应用程序

object runat=Server scope=Application ID=MyCount PROGID=″MSWC.Counters″>

</object><script Language=VBScript Runat=Server>Sub Application OnStartApplication(″VN″) = 0Application(″ly″) = ″ 欢迎你来到欢乐聊天室!″Application(″kx″)=″ 欢迎你来到欢乐聊天室!″End SubSub Session OnStartEnd SubSub Session OnEndEnd Sub

Page 89: 第10章      ASP 应用程序

</script

[ 例 10-10] 这是″欢乐聊天室″的登录页面,如图 10.14 所示。文件名为 ChatLogin.asp 。该程序限制在聊天室里登录的最大人数为 100 ,在登录页面必须填写用户名,如果该名字已经被人使用,则用户必须重新取名字。

Page 90: 第10章      ASP 应用程序

图 10.14

Page 91: 第10章      ASP 应用程序

<%@ Language=VBScript %>

<!-- #include FILE=″adovbs.inc″ -- >

<%

Dim ConnStr, RecUser, SQLstr

Dim strUserId

Dim ErrMes

ConnStr = ″DSN=chat2″

ErrMes = ″″

if Request.Form(″CmdLogin″) = ″ 登陆 ″ then

if Request.Form(″UserID″) <> ″″ then

Page 92: 第10章      ASP 应用程序

strUserId = Request.Form(″UserID″)Set RecUser =

server.CreateObject(″ADODB.RecordSet″)SQLstr = ″Select * From User Where UserID=′″ &

strUserId & ″′ And IsEnter=True″RecUser.Open SQLstr, ConnStr,adOpenDynamic,

adLockOptimistic if RecUser.EOF Or RecUser.BOF thenSession(″EnterTime″) = Now()Session(″UserID″) = strUserIDRecUser.AddNew RecUser.Fields(″UserID″) = strUserID

Page 93: 第10章      ASP 应用程序

RecUser.Fields(″EnterTime″) = Now()

RecUser.Fields(″IsEnter″) = True

RecUser.Update

′′ 如果人数为 100 ,则不能进入

if Application(″VisitorNum″)>=100 then

Response.Write ″对不起, 聊天室已满!″Response.End

Else

Application.Lock

Page 94: 第10章      ASP 应用程序

Application(″VisitorNum″) = Application(″VisitorNum″) + 1Application.UnLock end if

RecUser.Close Set RecUser = Nothing

Response.Redirect ″ChatRoom.asp″elseErrMes = ″ 该用户名已经被使用,请重新登录!″RecUser.Close Set RecUser = Nothingend if

Page 95: 第10章      ASP 应用程序

else

ErrMes = ″ 用户名不能为空,请重新登录!″end if

end if

%>

<html>

<head>

<meta Name=″Generator″ Content=″Microsoft Visual Studio 6.0″>

</head>

<body bgColor=thistle>

Page 96: 第10章      ASP 应用程序

<p align=center><font face= 宋体 size=5> 欢乐聊天室</font></p>

<p><hr><form action=″ChatLogin.asp″ method=post

id=formLog name=formLog>用户名 <input id=UserID name=UserID>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input id=CmdLogin name=CmdLogin type=submit value=″ 登录 ″ >

Page 97: 第10章      ASP 应用程序

<p> 口 &nbsp;&nbsp; 令 <input id=TxtUserPwd name=TxtUserPwd type=password></p>

</form><hr><%=ErrMes%><%=Application(″kx″)%></body></html>[ 例 10-11] 这是显示“欢乐聊天室”聊天场所的程序,

件名为 ChatRoom.asp ,如图 10.15 所示。其使用HTML 语言的 <frameset>框架标记将聊天的页面分为四个窗口,用来分别显示不同的信。

Page 98: 第10章      ASP 应用程序

图 10.15

Page 99: 第10章      ASP 应用程序

%@ Language=VBScript %><html><head><meta HTTP EQUIV=″Content Type″ CONTENT=

″text/html; charset=gb2312″><meta name=″generator″ content=″Microsoft

FrontPage 4.0″><meta name=″ProgId″ content=

″FrontPage.Editor.Document″><title> 欢乐聊天室 </title></head><frameset cols=″180,*″>

Page 100: 第10章      ASP 应用程序

<frameset rows=″80%,*″>

<frame name=″FrameList″ scrolling=″auto″ noresize src=″ChatList.asp″

>

<frame name=″FrameAction″ src=″ChatAction.asp″ scrolling=″auto″ noresize>

</frameset>

<frameset rows=″80%,*″>

<frame name=″FrameIssue″ src=″ChatIssue.asp″ scrolling=″auto″ noresize>

Page 101: 第10章      ASP 应用程序

<frame name=″FrameFun″ src=″ChatFun.asp″ scrolling=″auto″ noresize>

</frameset>

<noframes>

<body>

<p>此网页使用了框架,但您的浏览器不支持框架。</p>

</body>

</noframes>

</frameset>

</html>

Page 102: 第10章      ASP 应用程序

[ 例 10-12] 统计聊天室人数的程序,文件名为ChatList.asp ,该程序出现在聊天室页面的左上角,如图 10.15 所示。可显示当前聊天室里的人数以及进入聊天室里的人名。

<!-- #include file=″adovbs.inc″ -- ><html><head><meta http equiv=″refresh″ content=

″30;url=ChatList.asp″><style>body {font family: ″ 宋体″ ; font size:

14.8px};a:link {text decoration: none}; a:visited {text decoration: none}; </style>

</head>

Page 103: 第10章      ASP 应用程序

<body bgcolor=″#FFDCA0″><% Dim ConnStr, RecUser, SQLstrConnStr = ″DSN=chat2″Set RecUser = server.CreateObject(″ADODB.RecordSet″)SQLstr = ″Select * From User Where UserId Not In

(Select UserId From User WhereIsEnter=False)″RecUser.Open SQLstr, ConnStr,adOpenStatic Response.Write ″聊天室有:″ & ″<Font color=red>″ &

RecUser.RecordCount & ″</Font> 人 <br>″Do While Not RecUser.EOF

Page 104: 第10章      ASP 应用程序

Response.Write RecUser.Fields(″UserId″) & ″<br>″

RecUser.MoveNext

Loop

RecUser.Close

Set RecUser = Nothing

%>

</body>

</html>

• 说明: 在前面简单聊天室程序中没有刷新的功能,这里使用了下面的语句 ChatList.asp 文件过 30秒自动刷新一次。

<meta http equiv=″refresh″ content=″30;url=ChatList.asp″>

Page 105: 第10章      ASP 应用程序

[ 例 10-13] 显示进入或离开聊天室人名的程序,文件名为 ChatAction.asp 。该程序出现在聊天室页面

的左下角,如图 10.15 所示。

<!-- #include file=″adovbs.inc″ -- >

<html>

<head>

<meta http equiv=″refresh″ content=″30;url=ChatAction.asp″>

<style type=text/css> body {font family: 宋体 ; font size: 10.5pt}</style>

Page 106: 第10章      ASP 应用程序

</head><body bgcolor=#efffff><%Dim ConnStr, RecUser, SQLstrConnStr = ″DSN=chat2″Set RecUser =

server.CreateObject(″ADODB.RecordSet″)SQLstr = ″Select * From User Where EnterTime>=#″

& Session(″EnterTime″) & ″# Order By EnterTime″RecUser.Open SQLstr, ConnStr,adOpenDynamic,

adLockOptimistic

Page 107: 第10章      ASP 应用程序

Do While Not RecUser.EOFif RecUser.Fields(″IsEnter″) = True thenResponse.Write RecUser.Fields(″UserID″) & ″ 进入聊天室″ & ″<br>

Elseesponse.Write RecUser.Fields(″UserID″) & ″ 离开聊天室″ & ″<br>

End IfRecUser.MoveNext LoopRecUser.Close

Page 108: 第10章      ASP 应用程序

Set RecUser = Nothing%></body></html>

[ 例 10-14] 显示聊天内容的程序,文件名为ChatIssue.asp ,该程序在页面的右上方,如图 10.15 所示。它是聊天室的主要场所,是页面中最大的地方,可显示每个人说 ( 写 ) 的话,并且可以显示说话人的名字。

<%@ Language=VBScript %><!-- #include file=″adovbs.inc″ -- >

Page 109: 第10章      ASP 应用程序

<html><head><meta http equiv=″refresh″ content=

″10;url=ChatIssue.asp″><style type=text/css> body {font family: 宋体 ;

font size: 10.5pt}</style>

</head><body bgcolor=#efffff><%Dim ConnStr, RecUser, SQLstrConnStr = ″DSN=chat2″

Page 110: 第10章      ASP 应用程序

Set RecUser = server.CreateObject(″ADODB.RecordSet″)

SQLstr = ″Select * From Content Where SpeakTime>=#″ & Session(″EnterTime″) & ″# Order By SpeakTime″

RecUser.Open SQLstr, ConnStr,adOpenStatic

Do While Not RecUser.EOF

If RecUser.Fields(″IsPrivate″) = True Then

if Not (RecUser.Fields(″UserID″)<>Session(″UserID″) AND RecUser.

Fields(″FriendID″)<>Session(″UserID″)) Then

Page 111: 第10章      ASP 应用程序

Response.Write ″<Font color=blue>″ & RecUser.Fields(″UserID″) & ″ 私下对 ″ & RecUser.Fields(″FriendID″) & ″ 说: </Font>″ & RecUser.Fields(″Content″) & ″<br>″

End IfElseResponse.Write RecUser.Fields(″UserID″) & ″ 对 ″

& RecUser.Fields(″FriendID″) & ″ 说:″ & RecUser.Fields(″Content″) & ″<br>″

End IfRecUser.MoveNext Loop

Page 112: 第10章      ASP 应用程序

RecUser.CloseSet RecUser = Nothing%></body></html>[ 例 10-15] 输入聊天内容的程序,文件名为

ChatFun.asp ,该程序出现在聊天室页面的右下角,如图 10.15 所示。在这里可以输入聊天的内容,并且可以选择聊天的对象,在两个人之间进行私聊,使其他人看不到聊天的内容。

<%@ Language=VBScript %><!-- #Include file=″adovbs.inc″ -- >

Page 113: 第10章      ASP 应用程序

<html><head><meta name=″generator″ Content=″Microsoft Visual

Studio 6.0″><style>body {font family: ″ 宋体″ ; font size:

14.8px};a:link {text decoration: none}; a:visited {text decoration: none}; </style>

</head><body bgColor=powderblue><%Dim ii, jj, intNumDim strname, FriendID

Page 114: 第10章      ASP 应用程序

Dim Username(100)

Dim col, TempOrder

Dim ConnStr, RecUser, SQLstr

ConnStr = ″DSN=chat2″

Set RecUser = server.CreateObject(″ADODB.RecordSet″)

SQLstr = ″Select * From User Where UserId Not In (Select UserId From User Where IsEnter=False)″

RecUser.Open SQLstr, ConnStr,adOpenStatic

Page 115: 第10章      ASP 应用程序

intNum = 0Do While Not RecUser.EOF intNum = intNum + 1Username(intNum) = RecUser.Fields(″UserID″)RecUser.MoveNext LoopRecUser.Close

If Request.Form(″CmdSend″) = ″ 提交″ thenSQLstr = ″Select * From Content″RecUser.Open SQLstr, ConnStr,adOpenDynamic,

adLockOptimistic

Page 116: 第10章      ASP 应用程序

RecUser.AddNew RecUser.Fields(″UserID″) = Session(″UserID″)RecUser.Fields(″Content″) =

Request.Form(″TxtMark″)RecUser.Fields(″SpeakTime″) = Now()RecUser.Fields(″FriendID″) =

Request.Form(″CboFriend″)If Request.Form(″chkPrivate″) = ″ON″ thenRecUser.Fields(″IsPrivate″) = TrueElseRecUser.Fields(″IsPrivate″) = FalseEnd If

Page 117: 第10章      ASP 应用程序

RecUser.Update

RecUser.Close

End If

Set RecUser = Nothing

%>

<table align=center border=0 cellPadding=1 cellSpacing=1>

<tr>

<form method=″post″ action=″ChatFun.asp″ name=FormFun>

Page 118: 第10章      ASP 应用程序

<td>发言: <input name=TxtMark style=″height: 22px; width: 348px″></td>

<td><input name=CmdSend type=submit value= 提交></td></tr>

<tr><td><font face= 宋体 >私聊: <input name=chkPrivate type=checkbox

value=ON>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp;聊天对象: </font><Select name=CboFriend style=″height: 22px; width:

140px″>

Page 119: 第10章      ASP 应用程序

<option selected value= 大家 >全部 </option>

<% For ii = 1 To intNum %>

<option value=″<%=Username(ii)%>″><%=Username(ii)%></option>

<% Next %>

</select></td>

</form>

<td>

<form method=″post″ action=″ChatExit.asp″ name=FormExit target= parent>

Page 120: 第10章      ASP 应用程序

<font face= 宋体 ><input name=CmdExit type=submit

value=离开 ></font></form></td></tr></table></body></html>[ 例 10-16]离开聊天室出现的页面的程序,文件名为

ChatExit.asp ,并在用户离开聊天室的同时将聊天室的人数减去 1 。

<%@ Language=VBScript %><!-- #include file=″adovbs.inc″ -- >

Page 121: 第10章      ASP 应用程序

<html>

<head>

<meta name=″generator″ Content=″Microsoft Visual Studio 6.0″>

</head>

<body bgColor=antiquewhite>

<p align=center>

<%

Dim ii, jj, intNum

Dim strname, FriendID

Dim Username(100)

Page 122: 第10章      ASP 应用程序

Dim col, TempOrder, UserOrderDim ConnStr, RecUser, SQLstrConnStr = ″DSN=chat2″If Request.Form(″CmdExit″) = ″离开″ ThenSet RecUser =

server.CreateObject(″ADODB.RecordSet″)SQLstr = ″Select * From User″RecUser.Open SQLstr, ConnStr,adOpenDynamic,

adLockOptimistic RecUser.AddNew RecUser.Fields(″UserID″) = Session(″UserID″)RecUser.Fields(″EnterTime″) = Now()

Page 123: 第10章      ASP 应用程序

RecUser.Fields(″IsEnter″) = False

RecUser.Update

Application.Lock

Application(″VisitorNum″) = Application(″VisitorNum″) -1

Application.UnLock

Session.Abandon

RecUser.Close

Set RecUser = Nothing

End If

%>

Page 124: 第10章      ASP 应用程序

<font face= 宋体 size=5

tyle=″BACKGROUND color: #faebd7″>

欢迎下次继续光临欢乐聊天室! </font></p>

</body>

</html>

Page 125: 第10章      ASP 应用程序

10.7 一个简单的留言簿

• 这是基于数据库的 ASP留言簿程序,你可在mydb1.mdb 数据库中再建立一个表 lyb ,结构为:

xm char(8), ly char(255) , sj 日期 / 时间[ 例 10-17] 一个简单的留言簿程序,文件名为 10\|1

6.asp ,执行结果如图 10.16 所示。

Page 126: 第10章      ASP 应用程序

图 10.16

Page 127: 第10章      ASP 应用程序

<html><head><title>lyb</title></head><body bgColor=LavenderBlush><p align=″center″><font face=″隶书″ size=″5″>留言簿 </font></p>

<form method=″post″ action=″9 29.asp″><input type=″hidden″ name=″task″ value=″insert″><p> 姓名 :<input type=″text″ size=″20″ name=″xm

″></p>

Page 128: 第10章      ASP 应用程序

<p>留言 :</p><p><textarea rows=″2″ cols=″80″

name=″ly″></textarea></p><p><input type=″submit″ value=″ 记入留言簿″

name=″B1″><input type=″reset″ value=″复原″

name=″B2″></p></form><%set conn = Server.CreateObject(″ADODB.Connection

″)conn.Open ″ql″,″ ″,″ ″if Request(″task″)=″insert″ then

Page 129: 第10章      ASP 应用程序

xm=Request(″xm″)

sj = Date()

sj = sj & ″ ″ & Hour(Time()) & ″:″ & Minute(Time())

if Request(″ly″)=″ ″ then

Response.write(″ 输入一个留言吧″ )

Response.end

else

ly=Request(″ly″)

end if

Page 130: 第10章      ASP 应用程序

sql=″insert into lyb (xm,sj,ly) Values (′″ & xm & ″′,′″ & sj & ″′,

″ & ly & ″′) ″

conn.execute sql

end if

sql=″Select * from lyb order by sj DESC″

set rs = conn.Execute(sql) %>

<% On Error Resume Next

rs.MoveFirst

do while Not rs.eof %>

<hr>

Page 131: 第10章      ASP 应用程序

姓名 :<%=rs.Fields(″xm″)%><br>

留言写于 :<%=rs.Fields(″sj″)%><br>

留言内容 :<%=rs.Fields(″ly″)%>

<% rs.MoveNext

loop %>

</body>

</html>• 注意: 如果你的表不是建立在原来的数据库中,

需要进行新的连接,即重新定义 DSN 数据库源名!

Page 132: 第10章      ASP 应用程序

10.8 题库的输入、搜索和修改程序[ 例 10-18] 一个对题库 S3 按题型和年份进行搜索的程序,文

件名为 10\|17.asp, 执行结果如图 10.17 所示。

图 10.17

Page 133: 第10章      ASP 应用程序

<html><body bgColor=pink><form method=″post″ action=″10.17.asp″ id=form1

name=form1 value=″″ ><p><font color=″#800000″><b><span style=

″background color: #C0C0C0″> 选择查询的题型 </span></b></font><select size=″1″ name=

″tx″><option selected value=″1″>1. 填空题 </option><option value=″2″>2. 单选题 </option><option value=″3″>3.判断题 </option><option value=″4″>4. 多选题 </option>

Page 134: 第10章      ASP 应用程序

<option value=″5″>5.简述题 </option>

<option value=″6″>6.论述题 </option>

<option value=″7″>7.设计题 </option>

</select> <strong><font size=4 color=″#800000″><span style=″background color: #C0C0C0″> 选择年份 </span></font><font size=4><input size=4 name=″nf″ value年份 =″2001″ value=″2001″ >

&nbsp;</font></strong></p>

<p><font size=″4″ color=″orangered″

Page 135: 第10章      ASP 应用程序

<strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

</strong></font><font size=4><strong><input type=″submit″ value=″查询″ name=″B1″><input type=″reset″ value=″复原″ name=″B2″></p> </form>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查询到的题目 </strong>

<%

Page 136: 第10章      ASP 应用程序

sql=″Select 序号 ,题目 ,标准答案 From S2 Where 题型 = ′″& Request(″tx″) & ″′and 年份 = ′″&Request(″nf″) & ″′″

Call q(sql) %>

<!-- #include virtual=″/asp/q.inc″-- ></font>

</body> </html>

[ 例 10-19] 一个对题库 S3 按课程名和年份进行搜索的程序,文件名为 10\|18.asp ,执行结果如图 10.18所示。

Page 137: 第10章      ASP 应用程序

图 10.18

Page 138: 第10章      ASP 应用程序

<html><head></head><body bgColor=pink>

<form method=″post″ action=″10.18.asp″ id=form1 name=form1 value=″″ >

<p><b><span style=″background color: #C0C0C0″>选择查询的课程名 </span></b><select size=″1″ name=″kcm″>

<option selected value=″mis″>1. 管理信息系统 </option>

<option value=″sjk″>2. 数据库 </option>

<option value=″Java″>3.Java</option>

<option value=″wl″>4.计算机网络 </option>

Page 139: 第10章      ASP 应用程序

<option value=″web″>5.Web 编程 </option>

<option value=″6″>6.论述题 </option>

<option value=″7″>7.设计题 </option>

</select><strong><font size=4 color=″#800000″>

<span style=″background color: #C0C0C0″> 选择年份 </span>

</font><font size=4>

<input size=4 name=″nf″ value年份 =″2001″ value=″2001″ >

</font></strong>

</p>

Page 140: 第10章      ASP 应用程序

<p><input type=″submit″ value=″查询″ name=″B1″>

<input type=″reset″ value=″复原″name=″B2″></p>

</form>

<font color=orangered><font size=4><strong>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 按课程名和年份查询的结果 </strong>

<%

if request(″kcm″)<>″ ″ then

set cm= Server.CreateObject(″ADODB.Command″)

Page 141: 第10章      ASP 应用程序

set cn = Server.CreateObject(″ADODB.Connection″) cn.Open ″ql;″ set cm.ActiveConnection =cn cm.CommandText = ″select ID,题型 ,题目 ,标准答案

from S3 where 课程名 =? and 年份 =? order by 题型″

cm.CommandType =1 cm.Parameters.Append cm.CreateParameter(″课程

名″ ,200,,25) cm.Parameters.Append cm.CreateParameter(″年份″ ,

200,,25) cm(″课程名″ )=Request(″kcm″)

Page 142: 第10章      ASP 应用程序

cm(″年份″ )=Request(″nf″)

cm.Execute

set rs=cm.Execute

call t(rs)

end if

%>

<!-- #include file=″9 27.asp″-->

</body>

</html>

[ 例 10-20] 一个对题库 S3 按课程名和章节进行搜索的程序,文件名为 10\|19.asp ,执行结果如图 10.19所示。

Page 143: 第10章      ASP 应用程序

图 10.19

Page 144: 第10章      ASP 应用程序

<html><head></head><body bgColor=pink>

<form method=″post″ action=″10.19.asp″ id=form1 name=form1 value=″″ >

<p align=″center″><b> 选择查询的课程名 </b><select size=″1″ name=″kcm″>

<option selected value=″mis″>1. 管理信息系统 </option>

<option value=″sjk″>2. 数据库 </option>

<option value=″Java″>3.Java</option>

<option value=″wl″>4.计算机网络 </option>

<option value=″web″>5.Web 编程 </option>

Page 145: 第10章      ASP 应用程序

</select> <b> 章节 </b> <input type=″text″ name=″zj″ size=″20″ value=″1.1″></p>

<p align=″center″><input type=″submit″ value=″查询″ name=″B1″><input typ=″reset″ value=″复原″

name=″B2″></p></form><p align=″center″><font color=orangered><font size=4><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font face=″隶书″ > 按课程名和章节查询的结果 </font> </strong>

<%

Page 146: 第10章      ASP 应用程序

if reques(″zj″)<>″″ then set cm= Server.CreateObject(″ADODB.Command″)set cn = Server.CreateObject(″ADODB.Connection″) cn.Open ″ql;″ set cm.ActiveConnection =cn cm.CommandText = ″select ID,题型 ,题目 ,标准答案

from S3 where 课程名 =? and 课程章节 =? order by 题型″

cm.CommandType =1 cm.Parameters.Append cm.CreateParameter(″课程

名″ ,200,,25)

Page 147: 第10章      ASP 应用程序

cm.Parameters.Append cm.CreateParameter(″课程章节″ ,200,,25)

cm(″课程名″ )=Request(″kcm″) cm(″课程章节″ )=Request(″zj″) cm.Execute set rs=cm.Execute Call t(rs) end if %><!-- #include file=″9 27.asp″--></body></html>

Page 148: 第10章      ASP 应用程序

[ 例 10-21]从这个例子开始到例 10-23 是对题库 S3 按题型和年份进行修改的 3 个程序。本例是选择要修改题目的题型和年份的程序,文件名为 10\|20.asp ,执行结果如图 10.20 所示。在图 10.20 中首先选择题型和年份,然后单击“查询”按钮,会出现选择到的题目,在要修改的题目右边单击“修改”超链接,会进入图 10.21 所示的修改页面。

Page 149: 第10章      ASP 应用程序

图 10.20

Page 150: 第10章      ASP 应用程序

图 10.21

Page 151: 第10章      ASP 应用程序

<html><body bgcolor=″#eeeeff″><form method=″post″ action=″10.20.asp″ id=form1

name=form1 value=″″ ><p> 选择想修改题目的题型及年份 <select size=″1″

name=″tx″><option selected value=″1″>1. 填空题 </option><option value=″2″>2. 单选题 </option><option value=″3″>3.判断题 </option><option value=″4″>4. 多选题 </option><option value=″5″>5.简述题 </option><option value=″6″>6.论述题 </option>

Page 152: 第10章      ASP 应用程序

<option value=″7″>7.设计题 </option></select><select id=select1 name=nf> <option selected value=1999>1999</option><option value=1998>1998</option><option

value=2000>2000</option><option value=1996>1996</option></select></p><p><input type=″submit″ value=″查询″ name=

″B1″><input type=″reset″ value=″复原″name=″B2″></p></form>

Page 153: 第10章      ASP 应用程序

<hr size=″1″ color=″#808080″>

<%

if request(″tx″)<>″ ″ then

%>

div align=″center″>

<center>

<table border=″1″ width=″100%″ bordercolorlight=″#000000″ cellspacing″

0″ bordercolordark=″#ffffff″ bordercolor=″#ffffff″>

<tr>

Page 154: 第10章      ASP 应用程序

<td width=″16%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff″ size=″3″> 序号 </font></td>

<td width=″17%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff

″ size=″3″>题型 </font></td>

<td width=″17%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff

size=″3″>题目 </font></td>

<td width=″17%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff

size=″3″>标准答案 </font></td>

Page 155: 第10章      ASP 应用程序

<td width=″17%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff

size=″3″>修改 </font></td></tr><%set conn=server.CreateObject (″ADODB.Connection

″)conn.Open ″DSN=ql″set rs=server.CreateObject (″ADODB.Recordset″)MySql=″select * from S3 where 题型 =′″&

request(″tx″) &″′ and 年份 =′″&request(″nf″)&″′″

Page 156: 第10章      ASP 应用程序

rs.Open MySql,conn,3,3if rs.EOF then Response.Write ″<font size=3 color=red><b>对不起,

数据库没有任何纪录!</b></font>″ Response.End else for i=0 to rs.RecordCount 1 Response.Write ″<tr>″ Response.Write ″<form action=10.21.asp

method=post name=mody>″

Page 157: 第10章      ASP 应用程序

Response.Write ″<td width=16% align=center><b><font face= 宋体 color=#80008

0 size=2>″&rs(″ 序号″ )&″</font></b></td>″

Response.Write ″<td width=16% align=center><b><font face= 宋体 color=#80008

0 size=2>″&rs(″题型″ )&″</font></b></td>″

Response.Write ″<td width=16% align=center><b><font face= 宋体 color=#800080

size=2>″&rs(″题目″ )&″</font></b></td>″

Page 158: 第10章      ASP 应用程序

Response.Write ″<td width=16% align=center><b><font face= 宋体 color=#800080ize=2>″&rs(″标准答案″ )&″</font></b></td>″

Response.Write ″<tdalign=center><font size=2><a HREF=9-33m1.asp?

ID=″&rs(″id″)&″>修改 </a></font></td>″ rs.MoveNext Response.Write ″</tr>″ Response.Write ″</form>″ if rs.EOF then exit for next

Page 159: 第10章      ASP 应用程序

end ifrs.Closeset rs=nothingset conn=nothing%></tr></table></center></div><%end if%>

</body></html>

Page 160: 第10章      ASP 应用程序

[ 例 10-22]本例是修改选定记录的程序,文件名为 10\|21.asp 。修改后单击“确定修改”按钮,程序会执行 10-22.asp 程序,并将修改后的数据提交到服务器的数据库文件中,然后自动返回到图 10.20所示的选择修改页面中,可继续进行选择修改的工作。

<%Response.Buffer=true

myid=request(″id″)

set conn=server.CreateObject(″ADODB.Connection″)

conn.Open ″DSN=ql″

Page 161: 第10章      ASP 应用程序

setrs=server.CreateObject(″ADODB.Recordset″)

Sql=″select * from s3 where ID=″&myid&″″

rs.Open Sql,conn,3,3

if not rs.EOF then

myxh=rs(″ 序号″ )

mytx=rs(″题型″ )

myda=rs(″标准答案″ )

mytm=rs(″题目″ )

end if

rs.Close

set rs=nothing

Page 162: 第10章      ASP 应用程序

set conn=nothing%><html><head><title>修改记录 </title></head><body bgcolor=″#FFFFCC″><p align=″center″>修改记录的页面 </p><form method=″POST″ action=″10.22.asp″ name=

″form1″><input type=″hidden name=″vti group″ value=″0″><input type=″hidden″ name=″id″value=<%=myid

%>>

Page 163: 第10章      ASP 应用程序

<p align=″center″> 序号 <input type=″text″ name=″xh″ size=″20″ style=″border style: solid; border color: #808080″ value=<%=myxh%>></p>

<p align=″center″>题型 <input type=″text″ name=″tx″ size=″20″ style=″border style: solid; border color: #808080″ value=<%=mytx%>></p>

<p align=″center″>题目 <textarea name=″tm″ rows=3 cols=30 ><%=mytm%></textarea></p>

<p align=″center″>标准答案 <textarea name=″da″ rows=2 cols=30

Page 164: 第10章      ASP 应用程序

><%=myda%></textarea></p>

<p align=″center″><input type=″submit″ value=″ 确定修改″ name=″enter″>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type=″reset″ value=″全部重写″ name=″cancel″ onclick=″javascript:history.back()″></p>

</form>

<p align=″center″>   </p>

</body>

</html>

Page 165: 第10章      ASP 应用程序

[ 例 10-23]本例是确定修改记录内容后自动更新数据库文件记录的程序,文件名为 10\|22.asp 。执行程序后,将返回 10.20 所示的选择修改页面。

<%Response.Buffer =falsemyID=request(″id″)myxh=request(″xh″)mytx=request(″tx″)mytm=request(″tm″)myda=request(″da″)set conn=server.CreateObject(″ADODB.Connection″)

Page 166: 第10章      ASP 应用程序

conn.Open ″DSN=ql″

setrs=server.CreateObject(″ADODB.Recordset″)

Sql=″Select * From s3 where ID=″& myID &″″

rs.Open Sql,Conn,3,3

if Not Rs.EOF then

rs(″ 序号″ )=myxh

rs(″题型″ )=mytx

rs(″标准答案″ )=myda

rs(″题目″ )=mytm

rs.Update

Page 167: 第10章      ASP 应用程序

end if

rs.Close

set rs=nothing

set Conn=nothing

Response.Redirect ″10.20.asp″

%>

Page 168: 第10章      ASP 应用程序

10.9 进行投票统计的程序

• 下面的 3 个程序是进行投票统计的。注意要先进行数据源的连接,这里使用了一个新的数据库 ASP 。

[ 例 10-24] 这是投票候选人的显示页面程序,文件名为 vote.asp 。在满意的人后面单击“投票”即可,(如图 10.22 所示)将执行votedo.asp 程序,并会显示投票的情况及得票情况,如图 10.23 所示页面。

Page 169: 第10章      ASP 应用程序

图 10.22

Page 170: 第10章      ASP 应用程序

图 10.23

Page 171: 第10章      ASP 应用程序

<html><body bgcolor=″#eeeeff″><p><font color=mediumseagreen size=5><strong>投票候选人如下 </strong></font>

</p><p>

<hr size=″1″ color=″#808080″><%′if request(″myid″)<>″″ then%><p></p>

Page 172: 第10章      ASP 应用程序

<div align=″center″>

<center>

<table border=″1″ width=″50%″ bordercolorlight=″#000000″ cellspacing=″0″ bordercolordark=″#ffffff″ bordercolor=″#ffffff″ style=″height: 42px; width: 280px″>

<tr>

<td width=″16%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff ″size=″3″>候选人编号 </font></td>

Page 173: 第10章      ASP 应用程序

<td width=″17%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff ″ size=″3″>候选人名 </font></td>

<td width=″17%″ align=″middle″><font face=″ 新宋体″ color=″#0000ff ″

size=″3″>投票 </font></td></tr><%set conn=server.CreateObject (″ADODB.Connection

″)conn.Open ″DSN=asp″set rs=server.CreateObject (″ADODB.Recordset″)MySql=″Select name,ID from Vote order by id″

Page 174: 第10章      ASP 应用程序

rs.Open MySql,conn,3,3

if rs.EOF then

Response.Write ″<font size=3 color=red><b>对不起,数据库没有任何纪录! </b></font>″

Response.End

else

for i=0 to rs.RecordCount 1

Response.Write ″<tr>″

Response.Write ″<form action=votedo.asp method=POST name=mody>″

Page 175: 第10章      ASP 应用程序

Response.Write ″<td width=16% align=center><b><font face= 宋体 color=#800080 size=2>″&rs(″id″)&″</font></b></td>″

Response.Write ″<td width=16% align=center><b><font face= 宋体 color=#800080 size=2>″&rs(″name″)&″</font></b></td>″

Response.WRITE ″<tdalign=center><font size=2><a HREF=votedo.asp?ID=″&rs(″id″)&″>投票 </a></font></td>″

rs.MoveNext Response.Write ″</tr>″ Response.Write ″</form>″

Page 176: 第10章      ASP 应用程序

if rs.EOF then exit for

next

end if

rs.Close

set rs=nothing

set conn=nothing

%></tr>

</table>

</center>

</div>

<%′end if%>

Page 177: 第10章      ASP 应用程序

<%if session(″myname1″)<>″ ″ then%>

<p align=center><font face=″隶书″ size=4 color=red><b>谢谢您,你已经投了 <%=session(″myname1″)%> 一票了! </font></p>

<%end if%>

</b>

</body>

</html>

[ 例 10-25] 这是统计投票数的程序,文件名为votedo.asp 。

Page 178: 第10章      ASP 应用程序

<%

Response.Buffer =false

myID=request(″id″)

set conn=server.CreateObject(″ADODB.Connection″)

conn.Open ″DSN=asp″

setrs=server.CreateObject(″ADODB.Recordset″)

Sql=″Select * from Vote where id=″&myid&″″

rs.Open Sql,Conn,3,3

if Not Rs.EOF then

Page 179: 第10章      ASP 应用程序

mm=rs(″VoteNum″)

session(″myname1″)=rs(″name″)

mm=cint(mm)

rs(″VoteNum″)=mm+1

rs.Update

end if

rs.Close

set rs=nothing

set Conn=nothing

Response.Redirect ″voteresult.asp″

%>

Page 180: 第10章      ASP 应用程序

[ 例 10-26] 这是统计投票结果的页面程序,文件名为voteresult.asp ,执行结果如图 10.23 所示。

<html><head><title>本年度交通大学最佳男朋友评选 </title></head><bodylink=″#ffffff″ vlink=″#ffffff″ alink=″#ffffff″><%Set

conn=server.CreateObject(″ADODB.Connection″)

conn.Open ″dsn=asp″

Page 181: 第10章      ASP 应用程序

Set rs=server.CreateObject (″ADODB.Recordset″)sql1=″Select Sum(Vote.VoteNum) AS VoteSum From

Vote″rs.Open sql1,conn,3,3if rs.EOF thenResponse.Write ″对不起,本年度评选工作还没开始呢!″

elsecount=rs(″VoteSum″) end if rs.Close %>

Page 182: 第10章      ASP 应用程序

<div align=″center″>

<center>

<table border=″1″ width=″547″ bordercolorlight=″#800080″ cellpadding=″2″ bordercolordark=″#ffffff″ bordercolor=″#808080″>

<tr>

<td width=″547″ colspan=″3″ bgcolor=″#eae1e8″>

<p align=″center″><font face=″幼圆″ size=″6″ color=″#ff0000″><b>

Page 183: 第10章      ASP 应用程序

2001</b></font><font face=″幼圆″ size=″6″ color=″#ff0000″><b>年度交通大学

最佳男朋友评选 </b></font></p></td>

</tr>

<tr>

<td width=″112″ bgcolor=″#eae1e8″ align=″middle″><b><font face=″隶书″ color=″#0000ff ″>候选人 </font></b></td>

<td width=″108″ bgcolor=″#eae1e8″ align=″middle″><b><font face=″隶书″ color=″#0000ff ″>得票数 </font></b></td>

Page 184: 第10章      ASP 应用程序

<td width=″261″ bgcolor=″#eae1e8″ align=″middle″><b><font face=″隶书″ color=″#0000ff ″>得票率 </font></b></td>

</tr>

<%sql=″select * from vote order by votenum desc″

rs.Open sql,conn,3,3

if not rs.EOF then

rs.MoveFirst

for i=0 to rs.RecordCount

name=rs(″name″)

Page 185: 第10章      ASP 应用程序

number=rs(″votenum″)

yratio=number*100/count

myratio=formatnumber(myratio,2,true)

Response.Write ″ <tr p align=center><td width=112 bgcolor=#EAE1E8″&name&″</td>″

Response.Write ″<align=center><td width=108 bgcolor=#EAE1E8>″&number&″</td>″

Response.Write ″<align=center><td width=261 bgcolor=#EAE1E8 >″&myratio&″%</td>″

rs.MoveNext

if rs.Eof then exit for

Page 186: 第10章      ASP 应用程序

next

else

Response.Write ″对不起,本年度评选工作还没开始呢!″

end if

%>

</table>

</center>

</div>

<center><p>

Page 187: 第10章      ASP 应用程序

<a href=vote.asp><font color=mediumvioletred>继续投票 </font></a></p></center>

</body>

</html>

Page 188: 第10章      ASP 应用程序

习 题

1. 建立一个综合功能的网站,例如一个带有通信记录的班级,可进行输入、查询、修改,有留言簿、聊天室的网站。