第1章 ASP基础例1-1-1:
<HTML>
<HEAD>
<TITLE>HTML示例</TITLE>
</HEAD>
<BODY>
<FONT SIZE=7>欢迎访问!</FONT>
</BODY>
</HTML>
例1-2-1:
<%@ LANGUAGE = "VBScript" %>
<HTML>
<BODY>
<% For i = 3 To 7 %>
<FONT size=<% Response.Write i %>>
大家好!<BR>
</FONT>
<% Next %>
</BODY>
</HTML>
第2章 HTML基础例2-1-1:
<HTML>
<HEAD>
<TITLE>这里显示标题信息</TITLE>
</HEAD>
<BODY>
<P>这里是正文部分。</P>
</BODY>
</HTML>
例2-2-1:
<HTML>
<HEAD>
<TITLE>BODY属性示例</TITLE>
</HEAD>
<BODY background="img/0003.JPG" bgcolor="#FF0000" text="#FF3300" leftmargin="50" topmargin="50">
WWW(World Wide Web或万维网)是世界上最大的电子信息仓库,由众多的Web站点组成。每个Web站点都包含一些特定的资源,这些资源存放于一台或多台被称为Web服务器的计算机上。正是由于大量的Web站点提供了丰富多彩的资源,才使得用户能够通过网络快速、高效地获得他们需要的信息。
</BODY>
</HTML>
例2-3-1:
<HTML>
<HEAD><!-- 文档头部 -->
<TITLE>古诗鉴赏</TITLE>
</HEAD>
<BODY><!-- 文档主体部分 -->
<CENTER>
<H2>望庐山瀑布</H2><H5>李 白</H5>
<P>日照香炉生紫烟,<BR>遥看瀑布挂前川。<BR>飞流直下三千尺,<BR>
疑是银河落九天。</P>
</CENTER>
<HR align="center" width="90%" noshade>
<PRE>
[注释]
1.庐山:在江西省九江市南,是我国著名的风景区。
2.香炉:即香炉峰,在庐山西北,因形似香炉且山上经常笼罩着云烟而得名。
3.挂前川:挂在前面的水面上。
4.九天:古代传说天有九重,九天是天的最高层。
</PRE>
<DIV align="left">
这是诗人李白五十岁左右隐居庐山时写的一首风景诗。这首诗形象地描绘了庐山瀑布雄奇壮丽的景色,反映了诗人对祖国大好河山的无限热爱。
</DIV>
</BODY>
</HTML>
例2-3-2:
<HTML>
<HEAD><TITLE>物理字体和逻辑字体</TITLE></HEAD>
<BODY>
<DIV align="center">
<P><FONT color="#FF0000" size="5" face="华文新魏,隶书">物理字体和逻辑字体</FONT></P>
<TABLE width="75%" border="1">
<TR align="center"><TD>物理字体</TD><TD>逻辑字体</TD></TR>
<TR align="center">
<TD><B>粗体字</B><BR><I>斜体字</I><BR><U>增加下划线</U><BR><TT>打字机字体</TT><BR>普通文字<SUP>上标文字</SUP><BR>普通文字<SUB>下标文字
</SUB><BR><S>删除线文字</S></TD>
<TD><EM>强调文字</EM><BR><STRONG>字体加重</STRONG><BR><CODE>显示编程代码</CODE><BR><SAMP>输出示例文字</SAMP><BR><KBD>键盘按键文字</KBD>
<BR><VAR>定义变量或值</VAR><BR><DFN>定义文字</DFN><BR><CITE>引用文字
</CITE><BR><SMALL>缩小文字</SMALL><BR><BIG>放大文字</BIG></TD>
</TR>
</TABLE>
</DIV>
</BODY>
</HTML>
例2-4-1:
<HTML>
<HEAD><TITLE>列表的应用</TITLE></HEAD>
<BODY>
<TABLE width="100%" border="1">
<TR align="center"><TD>定义列表</TD><TD>有序列表</TD><TD>无序列表</TD></TR>
<TR><TD>
<DL>
<DT>HTML
<DD>超文本标记语言
<DD>是一种网络通用语言
<DT>互联网
<DD>网络的网络
</DL></TD>
<TD>
<OL>
<LI TYPE=A>星期一
<LI TYPE=a>星期二
<LI TYPE=I>星期三
<LI TYPE=i>星期四
<LI TYPE=1>星期五
</OL></TD>
<TD>
<UL>
<LI TYPE=circle>星期一
<LI TYPE=square>星期二
<LI TYPE=disc>星期三
<LI>星期四
<LI>星期五
</UL></TD></TR>
</TABLE>
</BODY>
</HTML>
例2-5-1:
<HTML>
<HEAD><TITLE>多媒体和超级链接的应用</TITLE></HEAD>
<BODY>
<TABLE width="100%" border="0">
<TR>
<TD><EMBED src="01.WMV" width=300 height=270 autostart=true></TD>
<TD>这是微软公司基于DirectShow基础之上开发的媒体播...以便更好的播放。</TD>
</TR>
</TABLE>
<P align="center"><A href="www.microsoft.com" target="_blank">官方网站</A> | <A href="wmplayer.exe">软件下载</A> | <A href="mailto:wpj@nciae.edu.cn">联系我们</A>
</BODY>
</HTML>
例2-6-1:
<HTML>
<HEAD><TITLE>表格的应用</TITLE></HEAD>
<BODY>
<TABLE width="500" cellspacing="1" cellpadding="5" border="1">
<TR>
<TD colspan="7" align="center" height="60"><H1>2005年9月</H1>
</TD></TR>
<TR bgcolor="#999999" align="center">
<TD>星期日</TD><TD>星期一</TD><TD>星期二</TD><TD>星期三</TD>
<TD>星期四</TD><TD>星期五</TD><TD>星期六</TD>
</TR>
<TR align="center">
<TD> </TD><TD> </TD><TD> </TD><TD> </TD>
<TD>1</TD><TD>2</TD><TD>3</TD>
</TR>
…
</TABLE>
</BODY>
</HTML>
例2-7-1:
<HTML>
<HEAD><TITLE>表单的应用</TITLE></HEAD>
<BODY>
<CENTER>注册个人信息</CENTER>
<FORM action="register.asp" method="post" name="register" target="_self">
姓名:<INPUT name="name" type="text" id="myname" maxlength="10" size="20">
<BR><INPUT name="myid" type="hidden" id="myid" value="personal">
性别:<SELECT name="sex">
<OPTION value="male" selected>男</OPTION>
<OPTION value="female">女</OPTION>
</SELECT><BR>
密码:<INPUT name="pwd" type="password" size="20" maxlength="20"><BR>
特长:<INPUT name="speciality" type="checkbox" value="computer" checked>计算机
<INPUT name="speciality" type="checkbox" value="sport">体育运动
<INPUT name="speciality" type="checkbox" value="music">音乐
<INPUT name="speciality" type="checkbox" value="art">艺术<BR>
年龄:<INPUT type="radio" name="age" value="1120">11-20
<INPUT name="age" type="radio" value="2130" checked>21-30
<INPUT type="radio" name="age" value="3140">31-40
<INPUT type="radio" name="age" value="4150">41-50<BR>
照片:<INPUT name="photo" type="file" id="photo" size="27" maxlength="50">
<BR>简介,<TEXTAREA name="resume" cols="36" rows="3">请如实填写!</TEXTAREA>
<BR><CENTER><INPUT type="submit" name="Submit" value="提交">
<INPUT type="reset" name="reset" value="重置"></CENTER>
</FORM>
</BODY>
</HTML>
例2-8-1:
<FRAMESET rows="…,…">
<FRAME>
<FRAMESET cols="…,…">
<FRAME>
<FRAME>
</FRAMESET>
</FRAMESET>
例2-9-1:
<HTML>
<HEAD>
<STYLE type="text/css">
<!--
BODY{font-family:"华文行楷";font-size:18pt;color:#ff0000}
-->
</STYLE>
<TITLE>CSS样式应用</TITLE>
</HEAD>
<BODY>
<P align="center">CSS样式基础</P>
<P align="center">CSS样式可以定义任何一个HTML标记。</P>
</BODY>
</HTML>
例2-9-2:
<HTML>
<HEAD>
<TITLE>用id属性定义样式表</TITLE>
<STYLE type="text/css">
<!--
#idstyle_1{font-family:"隶书";font-size:15pt;color:green}
#idstyle_2{font-family:"楷体";font-size:20pt;color:#00ff33}
-->
</STYLE>
</HEAD>
<BODY>
<CENTER>
<P><FONT id="idstyle_1">FONT的id属性用于应用CSS样式表</FONT></P>
<P><FONT id="idstyle_2">id属性的取值为事先定义的样式名称!</FONT></P>
</CENTER>
</BODY>
</HTML>
例2-9-3:
<HTML>
<HEAD>
<TITLE>使用class定义样式表</TITLE>
<STYLE type="text/css">
<!--
.classstyle_1{font-family:"黑体";font-size:24pt;color:blue}
.classstyle_2{font-family:"华文新魏";font-size:20pt;color:#ff6600}
-->
</STYLE>
</HEAD>
<BODY>
<CENTER>
<P class="classstyle_1">通过class属性应用样式表</P>
<P class="classstyle_2">可以同时定义多个class!</P>
</CENTER>
</BODY>
</HTML>
第3章 VBScript脚本语言例3-1-1:
<HTML>
<HEAD>
<SCRIPT language="VBScript">
Sub button1_OnClick
'判断用户名是否为空
If Trim(myform.username.value) = Empty Then
document.myform.elements(0).focus()
MsgBox "用户名不能为空!"
Exit Sub
End if
'判断用户密码是否正确
If Trim(myform.Pass1.value) = Empty Then
document.myform.elements(1).focus()
MsgBox "密码不能为空!"
Exit Sub
Else If len(myform.Pass1.value)>12 Then
document.myform.elements(1).focus()
MsgBox "密码不能超过12个字符!"
Exit Sub
Else If len(myform.Pass1.value)<6 Then
document.myform.elements(1).focus()
MsgBox "密码不能小于6个字符!"
Exit Sub
End If
End if
End if
myform.Submit
End Sub
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform" method="post" action="checklogin.asp">
<p>名称:<INPUT name="username" type="text" id="username"> </p>
<p>密码:<INPUT name="pass1" type="password" id="pass1"> </p>
<p><INPUT type="button" name="button1" id=button1 value="提交">
<INPUT type="reset" name="Submit2" value="重置"> </p>
</FORM>
</BODY>
</HTML>
checklogin.asp页面的具体代码如下:
<HTML>
<HEAD>
<TITLE>检查登录信息</TITLE>
</HEAD>
<BODY>
<%
'取得用户名和密码,并删除用户名和密码中的首尾空格
username=Trim(Request.Form("username"))
userpass=Trim(Request.Form("Pass1"))
'检查用户名和密码是否正确
If username="admin" then
If userpass="123456" then
Response.Write "欢迎你,管理员"
Else
Response.Write "<P>对不起,密码错误!</P>"
End If
Else
Response.Write "<P>你好!欢迎访问!</P>"
End If
Response.Write "<A href='login.htm'>返回</A>"
%>
</BODY>
</HTML>
例3-2-1:
<% @ LANGUAGE=VBScript %>
<% Option Explicit %>
<%
Dim intDegree
Dim intDegre
%>
例3-2-2:
<%
Option Explicit
Dim intX '声明脚本级变量
intX=1 '给脚本级变量赋值
SetLocalVariab1e '调用过程修改过程级变量的值
Response.Write intX '将脚本级变量的值发送到浏览器,值仍为1
Sub SetLocalVariab1e
Dim intX '声明过程级变量
intX=2 '给过程级变量赋值
End Sub
%>
例3-2-3:
<%
Option Explicit
Dim intX '声明脚本级变量
intX=1 '给脚本级变量赋值
SetLocalVariable '调用过程修改变量的值
Response.Write intX '将脚本级变量的值发送到浏览器,值为2
Sub SetLocalVariable
intX=2 '给脚本级变量赋值
End Sub
%>
例3-2-4:
<HTML>
<HEAD>
<TITLE>MsgBox使用方法</TITLE>
<Script Language="VBScript">
<!—
’定义子过程
Sub button1_OnClick
Dim intResult
intResult=MsgBox("你真的确定要提交吗?"&chr(13)&_
chr(10)&"提交后数据不可再更改。",4+32,"请你选择:")
’当点击不同按钮时给出不同响应
if intResult=6 then
MsgBox("提交成功!"&chr(13)&chr(10)&"请点击确定关闭窗口!")
else
MsgBox("数据未提交成功!")
end if
End Sub
-->
</Script>
</HEAD>
<BODY>
<INPUT type="button" name="button1" value="提交">
</BODY>
</HTML>
例3-2-5:
<HTML>
<HEAD>
<TITLE>InputBox函数的使用</TITLE>
</HEAD>
<BODY>
<Script Language="VBScript">
<!--
Dim strUserName,strUserAddress
strUserName=InputBox("请输入您的名字:","用户信息记录")
strUserAddress=InputBox("请输入您的住址:","用户信息记录")
MsgBox("您的基本信息为:"&chr(13)&chr(10)&"姓名:"&strUserName_
&chr(13)&chr(10)&"住址:"&chr(13)&chr(10)&strUserAddress)
-->
</Script>
</BODY>
</HTML>
例3-4-1:
<HTML>
<HEAD><TITLE>IF...ELSE嵌套语句</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub button1_OnClick()
Dim score
grade=trim(InputBox("请输入等级:(优、良、中、可、差)","学生等级","中"))
If grade="差" Then
score="成绩低于60分"
Else If grade="可" Then
score="成绩在60到70分之间。"
Else If grade="中" Then
score="成绩在70到80分之间。"
Else If grade="良" Then
score="成绩在80到90分之间。"
Else If grade="优" Then
score="成绩在90到100分之间。"
Else
score="输入的等级应该为(优、良、中、可、差)!"
End If
End If
End If
End If
End If
MsgBox score
End Sub
</SCRIPT></HEAD>
<BODY>
<INPUT type="button" name="button1" value="查看">
</BODY>
</HTML>
例3-4-2:
<HTML>
<HEAD><TITLE>Select Case语句</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub button1_OnClick()
Dim score
grade=trim(InputBox("请输入等级:(优、良、中、可、差)","学生等级","中"))
Select Case grade
Case "差" score="成绩低于60分,不及格!"
Case "可" score="成绩在60到70分之间,及格!"
Case "中" score="成绩在70到80分之间,中!"
Case "良" score="成绩在80到90分之间,良好!"
Case "优" score="成绩在90到100分之间,优秀!"
Case Else score="输入的等级应该为(优、良、中、可、差)!"
End Select
MsgBox score
End Sub
</SCRIPT></HEAD>
<BODY>
<INPUT type="button" name="button1" value="查看">
</BODY>
</HTML>
程序的执行结果与例3-4-1相同。
例3-4-3:
<HTML>
<HEAD>
<TITLE>流程控制语句的使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
Sub button1_OnClick
Document.Write("<Pre>")
Document.Write("*")
i=1
While i<10
Document.Write(Space(5)&i)
i=i+1
Wend
Document.WriteLn
For j=1 To 9
Document.Write(j&" ")
For k=1 To j
temp=j*k
If Len(temp)=1 Then
Document.Write(Space(4)&temp&" ")
Else
Document.Write(Space(3)&temp&" ")
End If
Next
Document.WriteLn
Next
Document.Write("</Pre>")
i=1
Do While i<60
Document.Write("*")
i=i+1
loop
End Sub
-->
</Script>
<BODY>
<INPUT type="button" name="button1" value="开始">
</BODY>
</HTML>
例3-5-1:
<HTML>
<HEAD>
<TITLE>函数的定义和使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
'产生固定范围并且没有重复的随机数
Function rndnum(a,k,w)
randomize
num=cint(fix(rnd*k))
m=0
while m<=w-1
if cint(a(m))=cint(num) then
randomize
num=cint(fix(rnd*k))
m=0
else
m=m+1
end if
wend
rndnum=num
End Function
Sub button1_OnClick
Dim arr(10)
For i=0 to 9
n=rndnum(arr,100,i)
arr(i)=n
Document.Write(n&" ")
Next
End Sub
-->
</Script>
<BODY>
<INPUT type="button" name="button1" value="开始">
</BODY>
</HTML>
例3-5-2:
<HTML>
<HEAD>
<TITLE>过程的定义和使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
Sub button1_OnClick
MsgBox("请等待2秒钟,马上开始!")
Call DelayTime(2)
MsgBox("窗前明月光,疑是地上霜。")
DelayTime(3)
MsgBox("举头望明月,低头思故乡。")
End Sub
Sub DelayTime(Delay)
Secondsinday=24*60*60
EndTime=Timer+Delay
If EndTime>Secondsinday Then
EndTime=EndTime-Secondsinday
Do While Timer>EndTime
Loop
End If
Do While Timer<EndTime
Loop
End Sub
-->
</Script>
<BODY>
<INPUT type="button" name="button1" value="开始">
</BODY>
</HTML>
例3-7-1:
<HTML>
<HEAD>
<TITLE>Window对象的使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
Sub button1_OnClick
Window.Alert now()
End Sub
Sub button2_OnClick
Window.close
End Sub
Sub openwin()
Window.open "login.htm","新窗口","toolbar=no","rsizeable=no",
width=200,height=200
Window.status=”窗口已经打开!”
End Sub
-->
</Script>
<BODY OnLoad='openwin()'>
<INPUT type="button" name="button1" value="显示时间"><p>
<INPUT type="button" name="button2" value="关闭"><p>
</BODY>
</HTML>
例3-7-2:
<HTML>
<HEAD>
<TITLE>按顺序输入注册信息</TITLE>
<Script id=clientEventHandlersVBS Language="VBScript">
<!--
Sub Windows_onload
Form1.xm.Focus
End Sub
Sub xm_onkeypress
If Window.Event.KeyCode=13 then
Form1.mm.Focus
End If
End Sub
Sub mm_onkeypress
If Window.Event.KeyCode=13 then
Form1.qrmm.Focus
End If
End Sub
Sub qrmm_onkeypress
If Window.Event.KeyCode=13 then
Form1.nl.Focus
End If
End Sub
Sub nl_onkeypress
If Window.Event.KeyCode=13 then
Form1.dh.Focus
End If
End Sub
Sub dh_onkeypress
If Window.Event.KeyCode=13 then
Form1.zz.Focus
End If
End Sub
Sub zz_onkeypress
If Window.Event.KeyCode=13 then
Form1.email.Focus
End If
End Sub
Sub email_onkeypress
If Window.Event.KeyCode=13 then
Form1.Button1.Focus
End If
End Sub
Sub button1_OnClick
Dim txt1,txt2,txt3,txt4,txt5,txt6,txt7,alltxt
txt1=Form1.xm.value
txt2=Form1.nl.value
txt3=Form1.dh.value
txt4=Form1.zz.value
txt5=Form1.mm.value
txt6=Form1.qrmm.value
txt7=Form1.email.value
If txt1="" then
MsgBox "姓名不能为空!"
Form1.xm.Focus
Exit Sub
End If
If txt5="" then
MsgBox "密码不能为空!"
Form1.mm.Focus
Exit Sub
End If
If Len(txt5)<6 or Len(txt5)>12 Then
MsgBox "密码应该在6到12位之间。"
Form1.mm.Focus
Exit Sub
End If
If txt6="" then
MsgBox "确认密码不能为空!"
Form1.qrmm.Focus
Exit Sub
End If
If txt5<>txt6 Then
MsgBox "密码和确认密码不相同!"
Form1.qrmm.Focus
Exit Sub
End If
If txt3="" then
MsgBox "电话不能为空!"
Form1.dh.Focus
Exit Sub
End If
If not isNumeric(txt2) Then
MsgBox "请输入数字!"
Form1.nl.Focus
Exit Sub
End If
If (InStr("@",txt7)=0 and InStr(".",txt7)=0) then
MsgBox "请输入有效的email地址!"
Form1.email.Focus
Exit Sub
End If
alltxt="姓名为:"+txt1+vbcrlf
alltxt=alltxt+"年龄为:"+txt5+vbcrlf
alltxt=alltxt+"年龄为:"+txt2+vbcrlf
alltxt=alltxt+"电话为:"+txt3+vbcrlf
alltxt=alltxt+"住址为:"+txt4
intb=MsgBox(alltxt,4+32,"确认提交")
If intb=6 Then
Form1.submit
End If
End Sub
Sub button1_onkeypress
If Windows.Event.KeyCode=13 then
button1_onclick
End If
End Sub
-->
</Script>
</HEAD>
<BODY bgcolor=White>
<FORM id=Form1 method="post" action="zhuce.asp">
<LEFT>
<p>姓名:<INPUT id=xm name=xm></p><br>
<p>密码:<INPUT type="password" id=mm name=mm>
确认密码:<INPUT type="password" id=qrmm name=qrmm></p><br>
<p>年龄:<INPUT id=nl name=nl>
电话:<INPUT id=dh name=dh></p><br>
<p>住址:<INPUT id=zz name=zz>
email:<INPUT id=email name=email></p><br>
<INPUT id=button1 name=button1 type=button value="提交">
<INPUT id=button2 name=button2 type=reset value="重填">
</LEFT>
</FORM>
</BODY>
</HTML>
例3-8-1:
<HTML>
<HEAD>
<TITLE>文档加载错误</TITLE>
<Script Language="VBScript">
<!--
Dim intVar,intResult,strVar
intVar=120
strVar="Tom"
’在进行计算时类型不匹配
intResult=intVar*strVar
MsgBox("结果为:"&intResult)
-->
</Script>
</HEAD>
<BODY>
...
</BODY>
</HTML>
例3-8-2:
<HTML>
<HEAD>
<TITLE>网页运行错误</TITLE>
<Script Language="VBScript">
<!--
Sub config()
MsgBox("你好!")
End Sub
Sub btn1_OnFocus
Window.status="欢迎光临!"
End Sub
Sub btn1_OnBlur
Window.status="欢迎下次再来!"
’将对象的名字拼写错误
Windows.close
End Sub
-->
</Script>
</HEAD>
<BODY>
<INPUT type="button" name="btn1" value="点击" onClick="config()">
</BODY>
</HTML>
第4章 Response与Request对象例4-2-1:
<%@ LANGUAGE="VBScript" %>
<% Response.Buffer=True %>
<HTML>
<HEAD>
<TITLE>使用缓冲示例</TITLE>
</HEAD>
<BODY>
<%
firsttime=Timer
For i = 1 To 5000
Response.Write "i的值为:"&i&space(4)
if i mod 4=0 then Response.Write "<br>"
Next
secondtime=Timer
jg=secondtime-firsttime
Response.write "用时:"&jg&" 秒"
%>
</BODY>
</HTML>
<%@ LANGUAGE="VBScript" %>
<% Response.Buffer=False %>
<HTML>
<HEAD>
<TITLE>不使用缓冲示例</TITLE>
</HEAD>
<BODY>
<%
firsttime=Timer
For i = 1 To 5000
Response.Write "i的值为:"&i&space(4)
if i mod 4=0 then Response.Write "<br>"
Next
secondtime=Timer
jg=secondtime-firsttime
Response.write "用时:"&jg&" 秒"
%>
</BODY>
</HTML>
例4-2-2:
<%
'输出字符串
Response.Write "这是一个字符串,"
'输出HTML标记
Response.Write "<BR>"
'输出字符和HTML标记
Response.Write "<p><font color='red'>这是一个红色的字符串</font></p>"
Response.Write "<p>欢迎访问<a href='http://www.myhome.com.cn'>我的网页</a></p>"
'输出数字、字符和HTML标记
Response.Write "The sum is," & 1+2 & "<BR>"
'输出变量的值
a="这是一个字符串,"
b="<font color='red'>这是一个红色的字符串</font>"
Response.Write a & b & "<BR>结束!"
%>
例4-2-3:
<%
'显示字符串
Response.Write " 同学们好! "&"<BR>"
'显示带引号的字符串
Response.Write """"&" 同学们好! "&""""&",他说到,"
'显示"%>"
Response.Write """"&"服务器脚本以'%\>'结束 "&""""&"<BR>"
%>
例4-2-4:
<%
Response.Buffer=True
%>
<HTML>
<BODY>
<%
'获取系统当前时间
dtmHour = Hour(Now())
'判断是否为上午
If dtmHour >= 8 and dtmHour <= 18 Then
Response.Redirect "work.htm"
Else
Response.Redirect "rest.htm"
End If
%>
</BODY>
</HTML>
例4-2-5:
<% Response.Buffer=True %>
<HTML>
<BODY>
<%
today=weekday(Date())
If today=1 or today=7 Then
Response.write "<H1>今天休息!</H1>"
Response.End
End If
%>
<H1>欢迎光临!</H1>
</BODY>
</HTML>
例4-3-1:
Input1.htm,代码如下:
<HTML>
<BODY>
<FORM method="POST" action="Output1.asp">
<h4 align="center">欢迎访问,请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10"></P>
<P align="center"><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
Output1.asp,代码如下,
<%
name=Request.Form("Username")
pass=Request.Form(2)
If name<>"" then
Response.Write "<P>谢谢!</P>"
Response.Write "<P>您输入的用户名是:" & name & "</P>"
Response.Write "<P>您输入的密码是:" & pass & "</P>"
Else
Response.Write "<P>对不起,请重新输入!"
Response.Write "<a href='input1.htm'>上一页</a>"
End If
%>
io.asp,代码如下:
<%
If Request.Form("ok")<>"确定" then
%>
<HTML>
<BODY>
<FORM method="POST" action="io.asp">
<h4 align="center">欢迎访问,请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10"></P>
<P align="center"><INPUT name="ok" type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
<%
Else
name=Request.Form("Username")
pass=Request.Form("Userpass")
If name<>"" then
Response.Write "<P>谢谢!</P>"
Response.Write "<P>您输入的用户名是:" & name & "</P>"
Response.Write "<P>您输入的密码是:" & pass & "</P>"
Else
Response.Write "<P>对不起,请重新输入!"
Response.Write "<a href='io.asp'>上一页</a>"
End If
End If
%>
例4-3-2:
Input2.htm,代码如下:
<HTML>
<BODY>
<FORM method="POST" action="Output2.asp">
<TABLE border="1" width="100%" bordercolor="#C0C0C0">
<TR>
<TD width="18%">姓名:<INPUT type="text"name="Username" size="10"></TD>
<TD width="82%">性别:<INPUT type="radio" name="sex" value="男" checked>男
<INPUT type="radio" name="sex" value="女" >女</TD>
</TR>
<TR>
<TD width="100%" valign="baseline" colspan="2">近期的购买计划,
<INPUT name="buy" type=checkbox value="计算机">计算机
<INPUT name="buy" type=checkbox value="电视 ">电视
<INPUT name="buy" type=checkbox value="汽车">汽车
<INPUT name="buy" type=checkbox value="房子">房子
</TD>
</TR>
<TR>
<TD width="18%" valign="top">您上网的主要目的:
<p>(按CTRL键可多选)</p>
</TD>
<TD width="82%"><SELECT name="purpose" multiple size="4">
<option value="查资料">查资料</option>
<option value="聊天">聊天</option>
<option value="学习">学习</option>
<option value="娱乐">娱乐</option>
</SELECT>
</TD>
</TR>
</TABLE>
<BR>
<INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
Output2.asp,代码如下:
<HTML>
<BODY>
<%
user=request.form("Username")
sex=request.form("sex")
If sex="女" then
user=user&"女士"
Else
user=user&"先生"
End If
response.write user&":您好!<BR>"
count=Request.Form("buy").count
Response.Write "<HR><BR>根据我们的调查,您的购买计划有" & count &"种,其中包括:<BR>"
For i=1 to count
Response.Write Request.Form("buy")(i)&"<BR>"
Next
count=Request.Form("purpose").count
Response.Write "<HR><BR>您上网主要目的" & count & "种,主要有:<BR>"
For i=1 to count
Response.Write Request.Form("purpose")(i)&"<BR>"
Next%>
</BODY>
</HTML>
例4-3-3:
Input3.asp,代码如下:
<HTML>
<BODY>
<H4>欢迎访问,请完成您的选择</H4>
<HR>
<A HREF="Output3.asp?work=查询数据&user=普通用户">查询数据</A>
<A HREF="Output3.asp?work=添加数据&user=权限用户">添加数据</A>
<%
Response.Write "<A HREF='Output3.asp?work=修改数据&user=权限用户'>修改数据</A> "
Response.Write "<A HREF='Output3.asp?work=删除数据&user=权限用户'>删除数据</A>"
%>
</BODY>
</HTML>
Output3.asp,代码如下:
<%
username=Request.QueryString("user")
userwork=Request.QueryString("work")
Response.Write username & " 您好!您选择了:" & userwork
%>
例4-3-4:
Input4.htm,代码如下:
<HTML>
<BODY>
<FORM method="GET" action="Output4.asp">
<P>姓名:<INPUT type="text"name="Username" size="10"></P>
<P>性别:<SELECT name="Sex" size="1">
<option value="男">男</option>
<option value="女">女</option>
</SELECT>
</P>
<P><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</P>
</FORM>
</BODY>
</HTML>
Output4.asp,代码如下:
<HTML>
<BODY>
<P>您的姓名是:<%Response.Write Request.QueryString("Username")%>。</P>
<P>您的性别是:<%Response.Write Request.QueryString("Sex")%>!</P>
</BODY>
</HTML>
例4-3-5:
<%
name=Request.Cookies("username")
pass=Request.Cookies("username")
%>
<HTML>
<BODY>
<FORM method="POST" action="cookiewrite.asp">
<h4 align="center">欢迎访问,请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10" value=<%=name%>></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10" value=<%=pass%>></P>
<P align="center"><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
cookiewrite.asp,代码如下:
<%
username=Request.form("username")
userpass=Request.form("userpass")
Response.Cookies("username")=username
Response.Cookies("username").Expires=Date()+7
Response.Cookies("userpass")=userpass
Response.Cookies("userpass").Expires=Date()+7
%>
例4-3-6:
<%
Dim strip
strip=Request.ServerVariables("REMOTE_ADDR")
If strip="127.0.0.1" then
Response.Write "谢谢您的访问!"
Else
Response.Write "对不起,拒绝访问!"
End If
%>
4.4 综合实例
userlogin.asp,代码如下:
<HTML>
<HEAD>
<TITLE>用户登录</TITLE>
</HEAD>
<BODY><CENTER>
<H4>请输入用户名并密码</H4>
<HR>
<TABLE border=0>
<FORM action="userdeal.asp" method="POST">
<TR>
<TD>用户名:</TD>
<TD><INPUT type="INPUT" NAME="Username" size="10" VALUE=<%=Request.QueryString("username")%> ></TD>
</TR>
<TR>
<TD>密 码:</TD>
<TD><INPUT type="PASSWORD" NAME="UserPass" size="10" ></TD>
</TR>
<TR>
<TD colspan=2 align="center"><INPUT type="SUBMIT" value="登录">
<INPUT type="RESET" value="取消"></TD>
</TR>
</FORM>
</TABLE>
<P><FONT color="red"><%=Request.QueryString("msg")%></FONT></P>
</CENTER>
</BODY>
</HTML>
userdeal.asp,代码如下
<%
' 设置提示信息
strNo = "用户名或密码为空,请输入正确的用户名和密码!"
strBadName = "对不起!输入的用户名不存在!"
strBadPass = "对不起!输入的密码错误!"
' 取得网页表单的值
strName = Request.Form("Username")
strPass = Request.Form("Userpass")
' 是否输入用户名和密码
If strName = "" or strPass = "" Then
' strName=""
Response.Redirect "userlogin.asp?msg=" & strNo & "&UserName=" & strName
End If
' 检查密码
If strname="guest" or strname="admin" Then
' 密码正确,找到用户
If strname="guest" and strPass="001" Then
'进入网站的网页
Response.Redirect "main.asp?user=guest"
Else If strname="admin" and strPass="002" Then
Response.Redirect "main.asp?user=admin"
Else
'密码错误
Response.Redirect "userlogin.asp?msg="&strBadPass &"&UserName=" & strName
End If
End If
Else
'用户错误
strName=""
Response.Redirect "userlogin.asp?msg=" & strBadName & "&UserName=" & strName
End If
%>
main.asp,代码如下:
<%
user=Request.QueryString("user")
Response.write "<H3>欢迎 " & user &" 访问本网站!</H3>"
%>
第5章 Session、Application和Server对象例5-1-1:
session.htm,代码如下:
<HTML><BODY>
<FORM method="POST" action="session.asp">
<h4 align="center">请输入您的用户名和年龄</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">年 龄:<INPUT type="text" name="Userage" size="10"></P>
<P align="center"><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY></HTML>
session.asp,代码如下,
<%
user=Request.Form("Username")
age=Request.Form("Userage")
Response.Write "回显用户输入的数据:<BR>"
Response.Write "用户名:" & user
Response.Write "年龄:" & age & "岁<BR>"
session("user")=user
session("age")=age
Response.Write "<a href='nextpage.asp'>下一页</a>"
%>
nextpage.asp,代码如下:
<%
Response.Write "session中的数据为:<BR>"
Response.Write "用户名:" & session("user")
Response.Write "年龄:" & session("age")
%>
例5-1-2:
<%
Response.Write "SessionID为:" & Session.SessionID & " "
Session("MyName") = "Mary"
Response.Write "Session变量的值为:" & Session("MyName")
Session.Abandon
Response.Write "<P><a href='abandon1.asp'>Abandon</a></P>"
Response.Write "SessionID为:" & Session.SessionID & " "
Response.Write "Session变量的值为:" & Session("MyName")
%>
当用户点击“Abandon”超链接时,将访问abandon1.asp文件,该文件代码如下:
<%
Response.Write "SessionID为:" & Session.SessionID & " "
Response.Write "Session变量的值为:" & Session("MyName")
%>
例5-2-1:
网站登录页面代码如下:
<%
If Request.Form("ok")="确定"then
user=Request.Form("Username")
pass=Request.Form("Userpass")
If user<>"" then
session("user")=user
session("pass")=pass
Response.Redirect "counter.asp"
End If
End If
%>
<HTML>
<BODY>
<FORM method="POST" action="app.asp">
<h4 align="center">请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10"></P>
<P align="center"><INPUT type="submit" name="ok" value="确定">
<INPUT type="reset" value="取消"> </P>
</FORM>
</BODY>
</HTML>
counter.asp,代码如下:
<%
If session("user")<>"" then
Response.Write "欢迎"&Session("user")&"访问本网站"&"<BR>"
session("user")=""
Application("counter")=Application("counter")+1
Response.Write "您是本网站的第"&Application("counter")&"个访问者"
Else
Response.Write "对不起,请首先访问网站的登录页面!<BR>"
Response.Write "<P><a href='login.asp'>登录页面</a></P>"
End If
%>
例5-3-1:
<%
Response.Write "表单的基本格式为:<BR>"
Response.Write Server.HTMLEncode("<FORM action= URL method=postmethod >")&"<BR>"
Response.Write "…<BR>"
Response.Write Server.HTMLEncode("</FORM>")&"<BR>"
%>
例5-3-2:
<%
'取得当前Web站点的物理路径
physicalpath=Server.Mappath("\")
Response.Write physicalpath&"<BR>"
'取得当前ASP文件所在目录下example目录的物理路径
physicalpath=Server.Mappath("example")
Response.Write physicalpath&"<BR>"
'取得当前Web站点下名为asp虚拟目录下login.asp文件的物理路径
physicalpath=Server.Mappath("/asp/login.asp")
Response.Write physicalpath&"<BR>"
'取得当前asp文件的物理路径
physicalpath=Server.Mappath(Request.ServerVariables("PATH_INFO"))
Response.Write physicalpath&"<BR>"
'取得当前asp文件的父目录
physicalpath=Server.Mappath("..\")
Response.Write physicalpath&"<BR>"
%>
5.4 网上聊天室
Global.asa,代码如下:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
Dim temptalk(5)
Application("talk")=temptalk
End Sub
</SCRIPT>
login.asp,代码如下:
<HTML>
<HEAD>
<TITLE>欢迎使用</TITLE>
</HEAD>
<BODY>
<H4 align="center">欢迎进入网上聊天室</H4>
<HR>
<P align="center">请输入您的呢称:</P>
<FROM method="POST" action="check.asp">
<P align="center"><input type="text" name="username" size="20"></P>
<P align="center"><input type="submit" value="进入">
<input type="reset" value="重写" ></P>
</FROM>
<P align="center"><FONT color="red"><%=Request.QueryString("msg")%>
</FONT></P>
</BODY>
</HTML>
check.asp,代码如下:
<%
user=trim(Request.Form("username"))
If user="" then
Response.Redirect "login.asp?msg=对不起,用户名不能为空!"
Else
items=split(application("people"),",")
'检查用户名是否重名
If instr(1,Application("people"),user&",")<>0 then
Response.Redirect "login.asp?msg=对不起,用户名重名!"
End If
'检查聊天室是否满员
If ubound(items)>3 then
Response.Redirect "login.asp?msg=对不起,聊天室满员!"
End If
Session("curruser")=user
Application.lock
Application("people")=application("people")&user&","
temptalk=Application("talk")
For i=5 to 1 step -1
temptalk(i)=temptalk(i-1)
Next
temptalk(0)="(" & time & ")" & user & "说:大家好!"
Application("talk")=temptalk
Application.unlock
Response.Redirect "main.asp"
End If
%>
main.asp,代码如下:
<HTML>
<HEAD>
<TITLE>网上聊天室</TITLE>
</HEAD>
<Frameset rows="70%,*" onunload=open("quit.asp")>
<Frameset cols="69%,*">
<Frame name="ltop" target="ltop" scrolling="auto" noresize src="talk.asp">
<Frame name="rtop" target="rtop" scrolling="auto" noresize src="userlist.asp">
</Frameset>
<Frameset cols="100%">
<Frame name="bottom" scrolling="auto" noresize src="talking.asp">
</Frameset>
<Noframes>
<BODY>
<P>此网页使用了框架,但您的浏览器不支持框架。</P>
</BODY>
</Noframes>
</Frameset>
</HTML>
talking.asp,代码如下:
<%
If Request.Form("Quit")="退出" Then
Response.Redirect "quit.asp"
End If
If request("content")<>"" then
name=Session("curruser")
temptalk=Application("talk")
Str="("&time&")"&name&"说:" & Server.HtmlEncode(Request.Form("content"))
Application.lock
For i=5 to 1 step -1
temptalk(i)=temptalk(i-1)
Next
temptalk(0)=str
Application("talk")=temptalk
Application.unlock
End If
%>
<HTML>
<BODY bgcolor="rgb(230,300,100)">
<Form method="post" action="talking.asp">
<INPUT type="text" name="content" size="50">
<INPUT type="submit" name="ok" value="提交">
<INPUT type="submit" name="quit" value="退出">
</FORM>
</BODY>
</HTML>
talk.asp,代码如下:
<HTML>
<HEAD>
<META http-equiv="refresh" content="5; url=talk.asp">
<TITLE>聊天内容</TITLE>
<BASE target="ltop">
</HEAD>
<BODY bgcolor="rgb(200,200,200)">
<%
temptalk=Application("talk")
For i=0 to 5
Response.Write temptalk(i) & "<BR>"
Next
%>
</BODY>
</HTML>
userlist.asp,代码如下:
<HTML>
<HEAD>
<META http-equiv="refresh" content="5; url=userlist.asp">
<TITLE>聊天成员</TITLE>
<BASE target="rtop">
</HEAD>
<BODY bgcolor="rgb(224,241,227)">
<FONT color="#ff00ff">在线人员名单:</FONT><BR>
<%
'显示在线人员
users=split(application("people"),",")
For i=0 To ubound(users)
Response.write "<FONT color=green>"&"-"&users(i)&"-"&"</FONT><BR>"
Next
%>
</BODY>
</HTML>
本页面要将application("people")变量中的内容显示出来,并定时“刷新”页面。
quit.asp,代码如下:
<%
name=Session("curruser")
If name<>"" then
Application.unlock
Application("people")=Replace(Application("people"),name&",","")
temptalk=Application("talk")
For i=5 to 1 step -1
temptalk(i)=temptalk(i-1)
Next
temptalk(0)="(" & time & ")" & name & "说:我走了!"
Application("talk")=temptalk
Application.unlock
Session.abandon
End If
%>
第6章 ASP组件例6-2-1:
adrot.txt,代码如下:
REDIRECT AdRedirect.asp
WIDTH 200
HEIGHT 100
BORDER 1
*
baidu.gif
http://www.baidu.com
百度搜索
30
google.gif
http://www.google.com
goole搜索
40
sogua.gif
http://www.sogua.com
搜刮
30
AdRedirect.asp,代码如下:
<%
'重定向到对应广告图像指定的URL
Response.Redirect (Request.QueryString("url"))
%>
AdRotatorEx.asp,代码如下:
<HTML>
<HEAD><TITLE>梦想家园!</TITLE></HEAD>
<BODY>
<BR>
<H1 align="center">欢迎光临“梦想家园”</H1>
<HR>
<%
Set Ad=Server.CreateObject("MSWC.AdRotator")
%>
<CENTER><%=Ad.GetAdverTisement("adrot.txt")%></CERTER>
</BODY>
</HTML>
例6-3-1:
link.txt,代码如下:
chapter1.asp 第一章:ASP基础
chapter2.asp 第二章:HTML基础
chapter3.asp 第三章:VBSCRIPT脚本语言
chapter4.asp 第四章:RESPONSE和REQUEST对象
chapter5.asp 第五章:SESSION、APPLICATION和SERVER对象
chapter6.asp 第六章:ASP组件
chapter7.asp 第七章:关系数据库基础
chapter8.asp 第八章:ADO对象
chapter9.asp 第九章:设计实例——网上论坛
ContentLinkingEx.asp,代码如下:
<HTML>
<HEAD>
<TITLE>电子图书之《ASP程序设计》</TITLE>
</HEAD>
<BODY>
<H2>《ASP程序设计》</H2>
<%
Set bookLink=Server.CreateObject("MSWC.NextLink")
count=bookLink.GetListCount("link.txt")
%>
<P>本书共有<%=count%>章,点击查看详细内容。</P>
<UL>
<%
For i=1 To count
url=bookLink.GetNthURL("link.txt",i)
descript=bookLink.GetNthDescription("link.txt",i)
%>
<LI><A HREF="<%=url%>"><%=descript%></LI>
<%next%>
</UL>
</BODY>
</HTML>
chapter6.asp,代码如下:
<HTML>
<HEAD>
<TITLE>第六章:ASP组件</TITLE>
</HEAD>
<BODY>
<H2>第六章:ASP组件</H2>
<!--此处是本章内容-->
<B>本章将详细介绍多种ASP组件的功能与实际应用.....,</B>
<%Set bookLink=Server.CreateObject("MSWC.NextLink")
count=bookLink.GetListCount("link.txt")
current=bookLink.GetListIndex("link.txt")
If current>1 Then
%>
<P>
<A HREF="<%=bookLink.GetPreviousURL("link.txt")%>">上一页</A>
<%End If%>
<%
If current<count Then
%>
<A HREF="<%=bookLink.GetNextURL("link.txt")%>">下一页</A>
<%End if%>
<A HREF="ContentLinkingEx.asp">返回目录</A>
</P>
</BODY>
</HTML>
例6-4-1,
<HTML>
<BODY>
本页面已经被浏览了<%=WebCounter.Increment("PageCount") %>次
</BODY>
</HTML>
例6-4-2,
CouterEx.asp,代码如下:
<HTML>
<HEAD><TITLE>民意调查!</TITLE></HEAD>
<BODY><FORM method="post" action="result.asp">
您对本网站的满意程度是:
<P><INPUT TYPE="radio" VALUE="V1" NAME="R1">非常满意</P>
<P><INPUT TYPE="radio" VALUE="V2" NAME="R1">比较满意</P>
<P><INPUT TYPE="radio" VALUE="V3" NAME="R1">不满意</P>
<P><INPUT TYPE="submit" VALUE="提交" NAME="B1"></P>
</FORM></BODY></HTML>
result.asp,代码如下:
<HTML><BODY>
<%result=Request.Form("R1")
If result="V1" Then WebCounter.Increment("Perfect_Count")
If result="V2" Then WebCounter.Increment("Common_Count")
If result="V3" Then WebCounter.Increment("Bad_Count")
%>
<P>选择“非常满意”的有:<%=WebCounter.Get("Perfect_Count")%>人
<P>选择“比较满意”的有:<%=WebCounter.Get("Common_Count")%>人
<P>选择“不满意”的有:<%=WebCounter.Get("Bad_Count")%>人
</BODY></HTML>
例6-5-1:
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
var MyTextFile=MyFileObject.CreateTextFile("f:\test1.txt",true) '创建文件
MyFileObject.CopyFile "f:\test1.txt","f:\test2.txt" '复制文件
MyFileObject.MoveFile "f:\test1.txt","f:\test3.txt" '移动文件
MyFileObject.DeleteFile "f:\test2.txt" '删除文件
MyFileObject.DeleteFile "d:\test3.txt" '删除文件
%>
例6-5-2:使用File对象中的方法进行文件操作。
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set NewFile=MyFileObject.GetFile("f:\test1.txt") '创建File对象
NewFile.Copy "f:\test1.txt" '复制文件
NewFile.Move "f:\test2.txt" '移动文件
NewFile.Delete '删除文件
%>
例6-5-3:使用FileSystemObject对象中的方法进行文件夹操作。
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
MyFileObject.CreateFolder("f:\test1.txt") '创建文件夹
MyFileObject.MoveFolder "f:\test1","d:\test2" '移动文件夹
MyFileObject.DeleteFolder "d:\test2" '删除文件夹
%>
例6-5-4:使用Folder对象中的方法进行文件夹操作。
<HTML><BODY>
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyFolder=MyFileObject.GetFolder("c:\programfiles") '创建Folder对象
For Each thing in MyFolder.Files '循环显示其中文件
Response.Write("<p>"&thing)
Next
%>
</BODY></HTML>
例6-5-5:
<HTML><BODY>
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
For Each thing in MyFileObject.Drives '对Drives集合进行循环
%>
<BR>驱动器号:<%=thing.DriveLetter%>
<BR>总容量:<%=thing.TotalSize%>
<BR>可用容量:<%=thing.AvailableSpace%>
<HR>
<%NEXT%>
</BODY>
</HTML>
例6-5-6:
<HTML>
<BODY>
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyDrive=MyFileObject.GetDrive("D:") '对Drives集合进行循环
%>
<BR>D盘的总容量:<%= MyDrive.TotalSize%>
<BR>D盘的可用容量:<%= MyDrive.AvailableSpace%>
<HR>
</BODY></HTML>
例6-5-7:读取文本文件的内容。
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyTextFile=MyFileObject.OpenTextFile("f:\test1.txt")
While not MyTextFile.AtEndOfStream '判断是否到了该文件的结尾
Response.Write(MyTextFile.ReadLine+"<br>")
Wend
MyTextFile.Close
%>
例6-5-8:向文本文件中连续写入多行信息
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyTextFile=MyFileObject.CreateTextFile("f:\test.txt") '创建文件
MyTextFile.WriteLine("向文本文件中写入多行信息!") '在文件中添加字符串
For i=1 To 10
Response.Write(MyTextFile.WriteLine("欢迎光临"))
Next
MyTextFile.Close
%>
例6-5-9:
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'以追加方式打开一个已存在的文件
Set MyTextFile=MyFileObject.OpenTextFile("f:\asp\weblog.log",8,true)
'追加本次用户浏览网页的时间
MyTextFile.WriteLine("欢迎来到本网站!时间:"&now())
'追加客户端的IP地址
MyTextFile.WriteLine(Request.ServerVariables("REMOTE_ADDR"))
MyTextFile.Close
%>
例6-5-10:
<HTML><HEAD><TITLE>梦想家园!</TITLE></HEAD>
<BODY><BR>
<H2 align="center">欢迎光临“梦想家园”</H2>
<HR>
<%Dim Visitors
FileAdress=Server.mappath("count.txt") '获取计数器文件的绝对地址
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'以下是读取文件计数器的数据并加1
Set CounterFile=MyFileObject.OpenTextFile(FileAdress) '得到TextStream对象
Visitors=CounterFile.readline '读取计数器文件中的访问次数
CounterFile.close
Visitors=Visitors+1 '将访问次数加1
'以下是将新数据重新写入文件计数器
Set CouterFile=MyFileObject.CreateTextFile(FileAdress)
CouterFile.WriteLine(Visitors)
CouterFile.close
Set MyFileObject=nothing
%>
<CENTER>您是本站第<%=Visitors%>位访问者!</CENTER>
</BODY></HTML>
例6-5-11,
<HTML>
<HEAD>
<TITLE>梦想家园!</TITLE>
</HEAD>
<BODY>
<BR>
<H2 align="center">欢迎光临“梦想家园”</H2>
<HR>
<%
Dim Visitors
FileAdress=Server.mappath("count.txt")'获取计数器文件的绝对地址
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'以下是读取文件计数器的数据并加1
Set CounterFile=MyFileObject.OpenTextFile(FileAdress) '得到TextStream对象
Visitors=CounterFile.readline '读取计数器文件中的访问次数
CounterFile.close
If IsEmpty(Session("Complete")) Then '加1前先判断Session变量
Visitors=Visitors+1 '将访问次数加1
End If
Session("Complete")=True '若已经将计数器加1,则创建Session变量
'以下是将新数据重新写入文件计数器
Set CouterFile=MyFileObject.CreateTextFile(FileAdress)
CouterFile.WriteLine(Visitors)
CouterFile.close
Set MyFileObject=nothing
%>
<CENTER>
<%
CountLen=len(Visitors) '取得计数器数据的位数
For i=1 To Countlen
'用相应的数字图像显示计数器数据
Response.Write"<IMG SRC=" & Mid(Visitors,i,1) & ".gif></IMG>"
Next
%>
</CENTER>
</BODY>
</HTML>
例6-6-1,
JmailEx.asp,代码如下:
<HTML>
<HEAD><TITLE>使用JMail组件发送邮件</TITLE></HEAD>
<BODY>
<H2 align="center">请填写邮件信息</H2><HR>
<FORM method="POST" action="sendEmail.asp">
<CENTER>
<TABLE border=1 width="80%">
<TR><TD>收件人地址:</TD>
<TD><input type="text" name="To" size="30%"></TD>
</TR>
<TR><TD><P>发件人地址:</TD>
<TD><input type="text" name="From" size="30%"></TD>
</TR>
<TR><TD>主题:</TD>
<TD><input type="text" name="Subject" size="30%"></TD>
</TR>
<TR><TD>内容:</TD>
<TD><textarea rows="8" name="Body" cols="35"></textarea></TD>
</TR>
<TR><TD colspan="2" align="center">
<INPUT type="submit" value="发送" name="Send">
<INPUT type="reset" value="重写" name="Cancel"></TD>
</TR>
</TABLE>
</CENTER>
</FORM>
</BODY>
</HTML>
sendEmail.asp,代码如下:
<HTML>
<BODY>
<CENTER>
<%
If Request.Form("From")<>"" And Request.Form("To")<>"" And Request,_
Form("Subject")<>"" Then
Set Message=Server.CreateObject( "JMail.Message")
Message.Logging=true '启用日志记录
Message.From=Request.Form("From")
Message.Subject=Request.Form("Subject")
Message.Body=Request.Form("Body")
Message.AddRecipient Request.Form("To")
If Not Message.Send("lijie:123456@smtp.163.com") Then
Response.Write("<PRE>" & Message.log & "</PRE>") '若发送不成功则显示日志
Else
Response.Write("邮件发送成功!")
End If
Else
Response.Write("必须填写收件人、发件人和邮件主题!")
End If
%>
</CENTER>
</BODY>
</HTML>
例6-6-2:
ASPUploadEx.asp,代码如下:
<HTML>
<HEAD>
<TITLE>使用ASPUpload组件上传文件</TITLE>
</HEAD>
<BODY>
<H2 align="center">文件上传</H2>
<HR>
<FORM method="POST" action="upload.asp" enctype="multipart/form-data">
<CENTER>
<TABLE border=1 width="80%">
<TR>
<TD>选择文件:</TD>
<TD><input type="file" name="upFile" size="30%"></TD>
</TR>
<TR>
<TD><P>文件名称:</TD>
<TD><input type="text" name="fileName" size="30%"></TD>
</TR>
<TR>
<TD>文件说明:</TD>
<TD><input type="text" name="fileIntro" size="30%"></TD>
</TR>
<TR>
<TD colspan="2" align="center">
<INPUT type="submit" value="上传">
</TR>
</TABLE>
</CENTER>
</FORM>
</BODY>
</HTML>
upload.asp,代码如下:
<HTML>
<BODY>
<%Dim Upload
Set Upload=Server.CreateObject("Persits.UPload.1")
Upload.SetMaxSize 1024*1024,False '限制文件大小不超过1M
Upload.OverwriteFiles=True '允许覆盖
Upload.Save "F:\web\upload" '上传到指定的位置
Response.Write "文件上传成功!<BR>"
Response.Write "上传文件为:" & Upload.Files("upFile").Path & "<BR>"
Response.Write "文件大小为:" & Upload.Files("upFile").Size & "<BR>"
Response.Write "文件名称为:" & Upload.Form("fileName").Value & "<BR>"
Response.Write "文件说明为:" & Upload.Form("fileIntro").Value & "<BR>"
%>
</BODY>
</HTML>
第8章 ADO对象例8-2-1:
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=stusql;UID=sa;PWD="
%>
例8-2-2:
<%
Set conn = Server.CreateObject("ADODB.Connection")
'设置ODBC连接字符串。Driver表示数据库的ODBC驱动程序,DBQ表示数据库的位置。
'UID表示用户帐号,PWD表示密码
strodbc = "Driver={Microsoft Access Driver (*.mdb)}; DBQ="&_
Server.MapPath("\") & "\student.mdb;UID=;PWD=; "
conn.Open strodbc
%>
例8-2-3:
<!--#include virtual ="adovbs.inc" -->
<% '打开连接数据库
Set conn = Server.CreateObject("ADODB.Connection")
strProvider="Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'建立查询,下面表示选择t_student表的所有项。
Command = "select * from t_student"
'执行查询。rs是RecordSet对象。
Set rs = conn.Execute(command,,adcmdtext)
Set conn = nothing
%>
例8-2-4:
register.htm,代码如下:
<HTML><BODY>
欢迎注册,请输入用户名和密码
<FORM method="POST" action="register.asp">
<P>姓名:<INPUT type="text" name="Name" size="10"></P>
<P>密码:<INPUT type="Password" name="Password" size="10"></P>
<P><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消"></P>
</FORM>
</BODY></HTML>
例8-2-5:
register.asp,代码如下:
<!--#include file = "adovbs.inc" -->
<%'取得用户名和密码,并删除用户名和密码中的首尾空格
username=Trim(Request.Form("Name"))
userpass=Trim(Request.Form("Password"))
'如果用户名或密码为空,重定向到注册页面
If username="" or userpass="" then
Response.Redirect "register.htm"
End If
'打开数据库,建立连接
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'检查用户是否已经存在
Commandtext = "Select * From t_user where d_user='" & username & "'"
conn.Execute commandtext,recordsAffected,adcmdtext
If recordsAffected=0 then
'向表t_user添加数据
Commandtext= "Insert into t_user(d_user,d_pass) VALUES('" & username & "','" & userpass & "')"
conn.Execute commandtext,recordsAffected,adcmdtext
Response.Write "注册成功!<BR>"
'修改注册信息
Commandtext="Update t_user Set d_pass='001' Where d_user='"&username&"'"
conn.Execute commandtext,recordsAffected,adcmdtext
Response.Write "已成功修改了" & recordsAffected & "条记录!<BR>"
'删除注册信息
Commandtext = "Delete t_user Where d_user='" & username & "'"
conn.Execute commandtext,recordsAffected,adcmdtext
Response.Write "已成功删除了" & recordsAffected & "条记录!"
Else
Response.Write "用户已存在,请选择新的用户名重新注册!"
Response.Write "<A href='register.htm'>注册页面</A><BR>"
End If
Set conn = nothing %>
例8-2-6:
<!--#include file = "adovbs.inc" -->
<% username=Trim(Request.Form("Name"))
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'开始一个新的事物
conn.BeginTrans
Commandtext = "Delete t_user Where d_user='" & username & "'"
conn.Execute commandtext,recordsAffected,adcmdtext
'判断是否有错误发生
if conn.Errors.count=0 then
conn.CommitTrans
Response.Write "已成功执行!"
Else
conn.RollbackTrans
Response.Write "发生错误,取消操作!"
End If
Set conn = nothing
%>
例8-2-7:
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=stusql;UID=sa;PWD="
Response.Write "<TABLE border=3>"
Response.Write "<TR><TD>相关属性</TD>"
Response.Write "<TD>属性值</TD>"
Response.Write "</TR>"
For each item In conn.properties
Response.Write "<TR><TD>"&item.name&"</TD>"
Response.Write "<TD>"&item.value&"</TD></TR>"
Next
Response.Write "</TABLE>"
Set conn = nothing
%>
例8-3-1:
<!--#include virtual ="adovbs.inc" -->
<%
'建立数据库连接
Set conn=Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'创建Command对象
Set comm=Server.CreateObject("ADODB.Command")
'设置Command对象属性
comm.ActiveConnection=conn
comm.commandtext="select * from t_student"
comm.commandtype=adCmdText
comm.prepared=True
'执行指定查询
set rs=comm.Execute()
'显示查询结果代码(略)
……
Set conn = nothing
%>
例8-3-2:
commselect.htm,代码如下:
<HTML>
<BODY>
<DIV Align="center">
<H4>请输入查询条件</H4>
<HR>
<FORM method="POST" action="commselect.asp">
<P>学号:<INPUT type="text" name="number" size="10">
性别:<SELECT name="Sex" size="1">
<option value="男">男</option>
<option value="女">女</option>
</SELECT>
</P>
<P><INPUT type="submit" value="查询">
<INPUT type="reset" value="取消">
</P>
</FORM>
</DIV>
</BODY>
</HTML>
commselect.asp,代码如下:
<% '接收用户输入的查询条件
sex=Request.Form("sex")
number=Request.Form("number")
If sex="" or number="" then
Response.Redirect "commselect.htm"
End If
'创建数据库连接
Set conn=Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
'创建Command对象并设置其属性
Set comm=Server.CreateObject("ADODB.Command")
comm.ActiveConnection=conn
'SQL语句中用?表示将接收parameter对象传递的参数
comm.commandtext="select * from t_student where sex=? and s_number=?"
comm.commandtype=adCmdText
comm.prepared=True
'建立Parameters集合
Set params=comm.parameters
'建立第一个Parameter对象,并用变量sex为其赋值
Set sex = comm.CreateParameter("sex",adVarChar,adParamInput,2,sex)
'将新建的Parameter对象追加到Parameters集合中
Params.Append sex
'建立第二个Parameter对象
Set num=comm.CreateParameter("s_num",adVarChar,adParamInput,10,number)
Params.Append num
set rs=comm.Execute()
'显示查询结果代码
If not rs.EOF then
While not rs.EOF
Response.Write "<TABLE border=3>"
Response.Write "查询结果为:<BR><TR>"
For i=0 To rs.fields.count-1
Response.Write "<TD>"&rs(i).value&"</TD>"
Next
rs.MoveNext
Response.Write "<TR>"
Wend
Else
Response.Write "对不起,没有找到匹配的结果!<BR>"
End If
Set conn=nothing
%>
例8-4-1:
<!--#include file ="adovbs.inc" -->
<%
'利用Server对象的CreateObject方法建立RecordSet对象。
Set rs=Server.CreateObject("ADODB.RecordSet")
'设置参数
rs.ActiveConnection = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
rs.Source="t_student"
rs.CursorType = adOpenStatic
rs.LockType = adLockreadOnly
'连接数据库
rs.Open
'断开连接
Set rs = nothing
%>
例8-4-2:
<%
strUserName = Request.Form("Username")
Set objDBConn = Server.Createobject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
objdbconn.Open strProvider
' 取得记录集
strSQL = "SELECT * FROM t_user WHERE UserName='" & strUserName & "'"
Set RS = objDBConn.Execute(strSQL)
' 检查用户
If RS.EOF and RS.BOF Then
' 无此用户
Response.Write "可以使用该用户名"
Else
Response.Write "该用户名已存在,请选择新的用户名"
End If
Set objRS = nothing
Set objDBConn = nothing
%>
例8-4-3:
<!--#include file ="adovbs.inc" -->
<%
'利用Server对象的CreateObject方法建立RecordSet对象。
Set rs=Server.CreateObject("ADODB.RecordSet")
'设置参数
Source="t_student"
Activeconn = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
'连接数据库
rs.Open Source,Activeconn,adOpenStatic,adLockreadOnly,adCmdTable
'断开连接
Set rs = nothing
%>
例8-4-4:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
Command = "select * from t_student order by s_number"
Set rs = conn.Execute(command,recordsAffected,adcmd)
'建立一个表,把查询得到的结果保存到表中。
Response.Write "<TABLE border=3>"
'表头
Response.Write "<TR>"
For i=0 To rs.fields.count-1
Response.Write "<TD>"&Ucase(rs(i).Name)&"</TD>"
Next
Response.Write "</TR>"
'把查询结果填入表中。
While not rs.EOF
Response.Write "<TR>"
For i=0 To rs.fields.count-1
Response.Write "<TD>"&rs(i).value&"</TD>"
Next
rs.MoveNext
Response.Write "<TR>"
Wend
Response.Write "</TABLE>"
Set conn = nothing
%>
例8-4-5:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from t_student where s_name='青山'"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
'修改记录值
If not rs.eof then
rs("POLITY").value = "党员"
'执行更新操作
rs.Update
End If
rs.close
Set conn = nothing
%>
例8-4-6:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
'创建一个空记录集,以节约系统资源
sql="select * from t_student where 1=2"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
rs.addnew
rs("s_number")="9812111"
rs("s_name")="test"
'执行更新操作
rs.Update
rs.close
Set conn = nothing
%>
例8-4-7:
<!--#include file = adovbs.inc -->
<%
Set rs=Server.CreateObject("Adodb.recordSet")
Con="Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu;User ID=sa; Password=; "
sql="select * from t_student order by s_number"
rs.Open sql,Con,AdOpenStatic,adlockreadonly,adcmdtext
'判断记录集是否为空
If rs.EOF then
Response.Write "记录集为空!"
Response.End
End If
'设置RecordSet对象的每一页数据记录条数的大小
rs.PageSize=4
'设置当前页
NowPage=Request.QueryString("page")
If NowPage="" then NowPage=1
NowPage=CLng(NowPage)
If NowPage<1 then NowPage=1
If NowPage>rs.PageCount then NowPage=rs.PageCount
rs.AbsolutePage=NowPage
%>
<HTML>
<HEAD><TITLE>分页显示示例</TITLE></HEAD>
<BODY><DIV Align="Center">
<H3>学生基本情况一览表</H3>
当前页:<%=NowPage%> (共 <%=rs.PageCount%> 页)
<HR>
<TABLE border="1">
<%'输出表头%>
<TR align=center valign=middle height=23>
<TD ><B>学号</B></TD>
<TD ><B>姓名</B></TD>
<TD ><B>性别</B></TD>
<TD ><B>出生日期</B></TD>
<TD ><B>政治面貌</B></TD>
</TR>
<%'输出当前页面记录
For j=0 To rs.PageSize-1
Response.Write "<TR align=center valign=middle>"
For i=0 To rs.fields.count-1
Response.Write "<TD>" &rs.fields(i).value & "</TD>"
Next
Response.Write "</TR>"
rs.MoveNext
If rs.Eof Then Exit For
Next
Response.Write "</TABLE><BR>"
'输出第一页、上一页、下一页和最后一页对应的超链
If NowPage<>1 then
Response.Write "<A href='pageshow.asp?page=1'>第一页 </A>"
Response.Write " "
Response.Write "<A href='pageshow.asp?page=" & NowPage-1 & "'>上一页</A>"
End If
If NowPage<>rs.PageCount then
Response.Write " "
Response.Write "<A href='pageshow.asp?page=" & NowPage+1 & "'>下一页</A>"
Response.Write " "
Response.Write "<A href='pageshow.asp?page=" & rs.PageCount &"'>最后一页</A>"
End If
%>
<Form action="pageshow.asp" method="GET">
<%'循环输出页码
Response.Write "请选择数据页:"
For i=1 To rs.PageCount
If i=NowPage then
Response.Write i&" "
Else
Response.Write "<A href='pageshow.asp?page=" & i &"'>" & i & "</A> "
End If
Next
%>
请输入页号:<INPUT type="text" name="page" size=4>
<INPUT type="submit" value="转到">
<INPUT type="reset" value="取消">
</P>
</Form>
</DIV>
</BODY>
</HTML>
例8-4-8:
<%'输出当前页面记录
For j=0 To rs.PageSize-1
Response.Write "<TR align=center valign=middle>"
For i=0 To rs.fields.count-1
Response.Write "<TD>" &rs.fields(i).value & "</TD>"
Next
Response.Write "<TD><A href='edit.asp?which=" &rs("s_number") &"'>修改</A></TD>"
Response.Write "</TR>"
rs.MoveNext
If rs.Eof Then Exit For
Next
……
%>
edit.asp,代码如下:
<!--#include file = adovbs.inc -->
<%'取得被编辑记录的标识
number=Request("which")
Set conn = Server.CreateObject("ADODB.Connection")
sTRProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open sTRProvider
Set rs=Server.createObject("ADODB.RecordSet")
rs.Open "select * from t_student where s_number =" &_
number,conn,adOpenkeySet,adlockOptimistic
%>
<HTML><BODY><DIV align ="center">
<H4 >学生(学号为:<%=rs("s_number")%>)的数据</H4><HR>
<Form method="POST" action="eupdate.asp">
<%'输出记录内容%>
<TABLE border=0 >
<TR><TD height="35"> 学 号:</TD>
<TD><input type="text" name="no" size="10" value=
"<% =Server.HTMLEncode(rs("s_number")) %>"></TD>
</TR>
<TR><TD height="35"> 姓 名:</TD>
<TD><input type="text" name="username" size="10" value=
"<% =Server.HTMLEncode(rs("s_name")) %>"></TD>
</TR>
<TR><TD height="35"> 性 别:</TD>
<TD><select size="1" name="sex">
<% If rs("sex")="男" then %>
<option value="男" selected>男</option>
<option valuve="女" >女</option>
<% Else %>
<option value="男" >男</option>
<option valuve="女" selected>女</option>
<%End If %>
</select></TD>
</TR>
<TR><TD height="35">出生日期:</TD>
<TD><input type="text" name="birthday" size="10" value=
"<% =Server.HTMLEncode(rs("birthday")) %>"></TD>
</TR>
<TR><TD height="35">政治面貌:</TD>
<TD><input type="text" name="polity" size="10" value=
"<% =Server.HTMLEncode(rs("polity")) %>"></TD>
</TR>
</TABLE>
<INPUT type=submit value="确定" name=B1 >
<INPUT type=reSet value="重填" name=B2 >
</div>
<input type="hidden" name="number" size="20" value=
"<% =Server.HTMLEncode(rs("s_number")) %>">
</Form>
<%Set conn=nothing%>
</BODY>
</HTML>
eupdate.asp,代码如下:
<!--#include file = adovbs.inc -->
<%
'取得记录标识
no=Trim(request.form("number"))
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from t_student where s_number='" & no & "'"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
'修改记录值
If not rs.eof then
rs("s_number")=Trim(request.form("no"))
rs("s_name")=Trim(request.form("username"))
rs("sex")=Trim(request.form("sex"))
rs("birthday")=Trim(request.form("birthday"))
rs("polity")=Trim(request.form("polity"))
'执行更新操作
rs.Update
End If
%>
例8-4-9:
<%'输出表头%>
<TR align=center valign=middle height=23>
<TD ><B>学号</B></TD>
<TD ><B>姓名</B></TD>
<TD ><B>性别</B></TD>
<TD ><B>出生日期</B></TD>
<TD ><B>政治面貌</B></TD>
<TD ><B>删除</B></TD>
</TR>
<Form action="delup.asp" name="delform" method="POST">
<%'输出当前页面记录
For j=0 To rs.PageSize-1
Response.Write "<TR align=center valign=middle>"
For i=0 To rs.fields.count-1
Response.Write "<TD>" &rs.fields(i).value & "</TD>"
Next
Response.Write "<TD><INPUT type='checkbox' name='delwhich' value='" & rs("s_number") &"'> </TD>"
Response.Write "</TR>"
rs.MoveNext
If rs.Eof Then Exit For
Next
Response.Write "</TABLE><BR>"
%>
<INPUT type="submit" name="del" value="删除">
<INPUT type="reset" value="取消">
</P></Form>
<%
'输出第一页、上一页、下一页和最后一页对应的超链
……
delup.asp,代码如下:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from t_student"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
'获取用户选择的记录数
decount=Request.Form("delwhich").count
If decount=0 then
Response.Write "对不起,没有找到删除的记录!"
Response.End
End If
While not rs.EOF
For i=1 to decount
'查找与用户选择相同的记录
If Request.Form("delwhich")(i)=Cstr(rs("s_number")) then
rs.delete
'执行更新操作
rs.Update
Exit For
End If
Next
rs.MoveNext
Wend
rs.close
Set conn = nothing
%>
第9章 设计实例——网上论坛
9.1 系统概述
9.1.3 系统的公共模块
1.层叠样式表文件(css.css )
BODY {
font-size,9pt;
background-color,#D1EDD8;
font-family,"Tahoma";
}
TABLE {
font-size,9pt;
font-family,"Tahoma";
border-collapse,collapse;
word-break:break-all;
}
A:visited {
font-size,9pt;
color,#000000;
font-family,"Tahoma";
text-decoration,none;
}
A:active {
font-size,9pt;
color,#FFFFFF;
font-family,"Tahoma";
text-decoration,none;
}
A:link {
font-size,9pt;
color,#000000;
font-family,"Tahoma";
text-decoration,none;
}
A:hover {
font-size,9pt;
color,#FFFFFF;
font-family,"Tahoma";
text-decoration,none;
}
.tr {
font-family,"Tahoma";
font-size,9pt;
background-color,#40A458;
}
.td {
font-family,"Tahoma";
font-size,9pt;
background-color,#A4DBB1;
}
input {
font-family,"Tahoma";
border-top-width,1px;
border-left-width,1px;
font-size,12px;
border-bottom-width,1px;
border-right-width,1px;
}
textarea {
font-family,"Tahoma";
border-top-width,1px;
border-left-width,1px;
font-size,12px;
border-bottom-width,1px;
border-right-width,1px;
color,#000000;
}
2.访问权限判断文件(access.asp)
<%
If Session("userName")="" then
Response.Redirect("default.htm")
Response.End()
End If
%>
3.公用函数文件(conn_fun.asp)
<%Dim rs,conn
'过程:连接数据库
sub getConn()
Dim strProvider
Set rs=Server.CreateObject("ADODB.Recordset")
Set conn=Server.CreateObject("ADODB.Connection")
strProvider="Provider=sqloledb;Data Source=.;Initial Catalog=bbs;User ID=sa;Password=sql;"
conn.open strProvider
If conn.Errors.Count>0 Then
Response.Write("数据库连接失败!")
Set rs=nothing
Set conn=nothing
Response.End
End If
End sub
'函数:用户级别文字转换
Function getUserLevel(userLevel)
If userLevel=0 Then
getUserLevel="论坛管理员"
ElseIf userLevel=1 Then
getUserLevel="普通用户"
End If
End Function
'函数:用户是否被锁定文字转换
Function getUserIsLocked(isLocked)
If isLocked=False Then
getUserIsLocked="未锁定"
ElseIf isLocked=True Then
getUserIsLocked="已锁定"
End If
End Function
'函数:文本中特殊字符入库转换
Function checkContent(content)
content=replace(content,"'","`")
content=replace(content,chr(34),"``")
content=replace(content,chr(13),"<br>")
content=replace(content,chr(32)," ")
checkContent=Content
End Function
'函数:文本中特殊字符出库转换
Function showContent(content)
content=replace(content,"`","'")
content=replace(content,"``",chr(34) )
content=replace(content,"<br>",chr(13))
content=replace(content," ",chr(32))
showContent=content
End Function
'函数:将心情图片路径转换为编号入库
Function moodPathToId(moodPath)
idLen=len(moodPath)-16-4
moodPathToId=cint(mid(moodPath,17,idLen))
End Function
'函数:将心情图片编号转换为路径出库
Function moodIdToPath(moodId)
moodIdToPath="images/mood/face"&moodId&".gif"
End Function
%>
4.页面的顶部显示文件(top.asp)
<!--#Include File="access.asp"-->
<!--#Include File="conn_fun.asp"-->
<HTMl><HEAD>
<LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="JavaScript">
function confirmQuit(){
flag=confirm("确定要退出论坛吗?");
return flag;
}
</SCRIPT></HEAD>
<BODY><BR>
<H2 align="center"><FONT color="#286637" face="华文彩云">论 坛</FONT></H2>
<%getConn()
userSql="select * from userInfo where userName='"&Session("userName")&"'"
Set userRs=conn.execute(userSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25" width="198" align="left" bordercolor="#40A458">
<FONT color="#FFFFFF">欢迎 <%=Session("userName")%> 光临!
<%If userRs("isLocked") Then%>您已被锁定!<%End If%></FONT></TD>
<TD height="25" align="center" bordercolor="#40A458">
<A href="mainPage.asp" color="#FFFFFF">论坛首页</A>┊┊
<A href="editUserInfo.asp">个人资料</A>┊┊
<A href="search.asp">论坛搜索</A>┊┊
<%If userRs("userLevel")=0 Then%>
<A href="admin.asp">论坛管理</A>┊┊
<%End If%>
<A href="logout.asp" onClick="return confirmQuit();">退出登录</A>
</TD>
<TD height="25" width="198" align="center" bordercolor="#3D65B8">
</TD></TR></TABLE>
<%Set userRs=nothing
Set conn=nothing%>
<BR></BODY></HTMl>
5.页面的底部显示文件(bottom.htm)
<HTMl><BODY><BR><BR><TABLE width="760" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#cccccc" bgcolor="#FFFFFF">
<TR><TD height="25" colspan="6" class="tr" align="center">简易论坛! 作者:LJ 版权所有!</TD>
</TR></TABLE></BODY></HTMl>
9.2 用户登录、注册、个人资料功能模块
9.2.1 用户登录模块
1、登录页面(default.htm)
<HTMl><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>登录论坛</TITLE><LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="VBScript">
Sub Submit1_OnClick
userName=Trim(form1.userName.value)
passWord=form1.passWord.value
If userName=Empty Then
MsgBox "帐号不能为空!"
Document.form1.userName.Focus()
Exit Sub
ElseIf InStr(userName,"'")<>0 Then
MsgBox "请正确填写帐号!"
Document.form1.userName.Focus()
Exit Sub
ElseIf passWord=Empty Then
MsgBox "请填写密码!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf InStr(passWord,"'")<>0 Then
MsgBox "请正确填写密码!"
Document.form1.passWord.Focus()
Exit Sub
End If
form1.Submit
End Sub
</SCRIPT></HEAD>
<BODY>
<H2 align="center"><FONT color="#286637" face="华文彩云">论 坛</FONT> </H2>
<DIV align="center">
<TABLE width="400" border="1" align="center" bordercolor="#000000">
<FORM name="form1" method="post" action="login.asp">
<TR><TD height="25" colspan="6" align="center" class="tr">用户登录 </TD></TR>
<TR><TD height="100" colspan="6" align="center" class="td">
<TABLE width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
<TR><TD width="41%" align="right">帐 号:</TD>
<TD width="59%"><input name="userName" type="text"></TD>
</TR>
<TR><TD align="right">密 码:</TD>
<TD><input name="passWord" type="password"></TD>
</TR></TABLE></TD></TR>
<TR><TD height="25" colspan="6" align="center" class="tr">
<input type="button" name="Submit1" value="提 交" onclick="return checkLogin(this.form)">
<input type="reset" name="Submit2" value="重 置"></TD></TR>
<TR><TD height="25" colspan="6" align="center" class="tr"><A href="register.htm">新用户注册</A></TD>
</TR></FORM></TABLE></DIV></BODY></HTMl>
2、登录提交页面(login.asp)
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
userName_form=Trim(Request.Form("userName"))
passWord_form=Trim(Request.Form("passWord"))
'------判断登陆的帐号与密码的合法性
getConn()
userSql="select * from userInfo where userName='"&userName_form&"'"
rs.open userSql,conn,1,1
If rs.eof Then
errmsg="不存在此用户,请注册!"
returnURL="register.htm"
ElseIf rs("passWord")<>passWord_form Then
errmsg="密码不正确,请重新登陆!"
returnURL="default.htm"
ElseIf Instr(Application("online_name"),userName_form) Then
errmsg="该帐号正在被使用!登录失败!"
returnURL="default.htm"
Else
Session("userName")=userName_form
conn.Execute("update userInfo set visitCount="&rs("visitCount")+1&" where userName='"&userName_form&"'")
errmsg="登录成功!进入论坛!"
returnURL="mainPage.asp"
End If
Set rs=Nothing
Set conn=Nothing
%>
<HTMl><HEAD><META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
</HEAD><BODY><%=errmsg%></BODY></HTMl>
9.2.2 用户注册模块
1、注册页面(register.htm)
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>用户注册</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="VBScript">
Sub Submit1_OnClick
atInEMail=InStr(form1.EMail.value,"@")
pointInEMail=InStr(form1.EMail.value,".")
If form1.userName.value=Empty Then
MsgBox "请填写用户名!"
Document.form1.userName.Focus()
Exit Sub
ElseIf InStr(Trim(form1.userName.value),"'")<>0 Then
MsgBox "请正确填写用户名!"
Document.form1.userName.Focus()
Exit Sub
ElseIf form1.passWord.value=Empty Then
MsgBox "请填写密码!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf InStr(form1.passWord.value,"'")<>0 Then
MsgBox "请正确填写密码!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf Len(form1.passWord.value)<6 Then
MsgBox "密码至少6位!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf form1.conPassWord.value=Empty Then
MsgBox "请填写确认密码!"
Document.form1.conPassWord.Focus()
Exit Sub
ElseIf form1.passWord.value<>form1.conPassWord.value Then
MsgBox "两次填写密码不一致!"
form1.passWord.value=Empty
form1.conPassWord.value=Empty
Document.form1.passWord.Focus()
Exit Sub
ElseIf form1.EMail.value=Empty Then
MsgBox "请填写E-Mail!"
Document.form1.EMail.Focus()
Exit Sub
ElseIf atInEMail<2 Or pointInEMail<4 Or atInEMail>pointInEMail Then
MsgBox "请填写正确的E-Mail!"
Document.form1.EMail.Focus()
Exit Sub
ElseIf Len(form1.userSign.value)>200 Then
MsgBox "签名最多不能超过200字!"
Document.form1.userSign.Focus()
Exit Sub
End If
form1.Submit
End Sub
</SCRIPT>
</HEAD>
<BODY>
<H2 align="center"><FONT color="#286637" face="华文彩云">论 坛</FONT> </H2>
<TABLE width="600" border="1" align="center" bordercolor="#000000">
<FORM name="form1" method="post" action="register.asp">
<TR>
<TD height="25" colspan="3" align="center" class="tr">新用户注册</TD>
</TR>
<TR>
<TD width="150" height="25" align="right" class="td">用户名:</TD>
<TD width="468" class="td"><input name="userName" type="text" id="userName"></TD>
<TD width="87" align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">密 码:</TD>
<TD class="td"><input name="passWord" type="password" id="passWord">*密码至少为6位*</TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">确认密码:</TD>
<TD class="td"><input name="conPassWord" type="password" id="conPassWord"></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">性 别:</TD>
<TD class="td"><input name="userSex" type="radio" value="男" checked>男
<input name="userSex" type="radio" value="女">女
</TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">E-Mail:</TD>
<TD class="td"><input name="EMail" type="text" id="EMail"></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD align="right" class="td">签 名:</TD>
<TD class="td"><textarea name="userSign" cols="50" rows="6" id="userSign"></textarea></TD>
<TD align="center" class="td">最多200字</TD>
</TR>
<TR>
<TD height="25" colspan="3" align="center" class="tr">
<input type="button" name="Submit1" value="提 交">
<input type="reset" name="Submit2" value="重 置"></TD>
</TR>
</FORM>
</TABLE>
</BODY>
</HTMl>
2、注册提交页面(register.asp)
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
userName_form=Trim(Request.Form("userName"))
passWord_form=Trim(Request.Form("passWord"))
userSex_form=Request.Form("userSex")
EMail_form=Trim(Request.Form("Email"))
userSign_form=Trim(Request.Form("userSign"))
regDate=Date()&" "&Time()
getConn()
'------判断填写的用户名是否已经被注册
Set checkUserNameRs=conn.Execute("select * from userInfo where userName=' "&userName_form&"'")
'若用户名已存在,则返回前一页,保持表单中填写的内容不变
If not checkUserNameRs.Eof Then
errmsg="用户名已存在,请重新填写!"
returnURL="javascript:history.back();"
'若用户名未被使用,则插入用户表中一条记录,注册成功
Else
registerSql="insert into userInfo(userName,passWord,userSex,userEMail,userSign,userLevel,visitCount,topicCount,regDate,islocked) values ('"&userName_form&"','"&passWord_form&"','"&userSex_form&"','"&EMail_form&"','"&userSign_form&"',1,1,0,'"®Date&"',0)"
conn.execute(registerSql)
Session("userName")=userName_form
errmsg="注册成功!自动登录论坛!"
returnURL="mainPage.asp"
End If
Set checkUserNameRs=Nothing
Set conn=Nothing
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<%=errmsg%>
</BODY>
</HTMl>
9.2.3 个人资料模块
1、个人资料修改页面(editUserInfo.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>个人资料</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkRegister(form){
var atInEMail=form.EMail.value.indexOf("@");
var pointInEMail=form.EMail.value.indexOf(".");
if(form.passWord.value==""){
alert("请填写密码!");
form.passWord.focus();
return false;
}
else if(form.passWord.value.length<6){
alert("密码至少6位!");
form.passWord.focus();
return false;
}
else if(form.conPassWord.value==""){
alert("请填写确认密码!");
form.conPassWord.focus();
return false;
}
else if(form.passWord.value!=form.conPassWord.value){
alert("两次填写密码不一致!");
form.passWord.value="";
form.conPassWord.value="";
form.passWord.focus();
return false;
}
else if(form.EMail.value==""){
alert("请填写E-Mail!");
form.EMail.focus();
return false;
}
else if(atInEMail<1||pointInEMail<3||atInEMail>pointInEMail){
alert("请填写正确的E-Mail!");
form.EMail.focus();
return false;
}
else if(form.userSign.value.length>200){
alert("签名最多不能超过200字!");
form.userSige.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD> 当前位置:<A href="editUserInfo.asp">个人资料</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE><BR>
<%
getConn()
userSql="select * from userInfo where userName='"&Session("userName")&"'"
rs.open userSql,conn,1,1
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form1" method="post" action="postEditUserInfo.asp">
<TR class="tr">
<TD height="25" colspan="3" align="center">修改个人资料</TD>
</TR>
<TR class="td">
<TD width="150" height="25" align="right">用户名:</TD>
<TD width="468"> <%=Session("userName")%></TD>
<TD align="center">必填</TD>
</TR>
<TR class="td">
<TD align="right">密 码:</TD>
<TD> <input name="passWord" type="password" value=<%=rs("passWord")%>>*密码至少为6位*</TD>
<TD align="center">必填</TD>
</TR>
<TR class="td">
<TD align="right">确认密码:</TD>
<TD> <input name="conPassWord" type="password" value=<%=rs("passWord")%>></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR class="td">
<TD align="right">性 别:</TD>
<%If rs("userSex")="男" Then%>
<TD> <input name="userSex" type="radio" value="男" checked>男<input name="userSex" type="radio" value="女">女</TD>
<%Else%>
<TD> <input name="userSex" type="radio" value="男">男<input name="userSex" type="radio" value="女" checked>女</TD>
<%End If%>
<TD height="20" align="center" class="td">必填</TD>
</TR>
<TR class="td">
<TD align="right">E-Mail:</TD>
<TD> <input name="EMail" type="text" value=<%=rs("userEMail")%>></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR class="td">
<TD align="right">签 名:</TD>
<TD> <textarea name="userSign" cols="50" rows="6"><%=showContent(rs("userSign"))%></textarea></TD>
<TD align="center">最多200字</TD>
</TR>
<TR class="tr">
<TD height="20" colspan="3" align="center">
<input type="submit" name="Submit1" value="提 交" onClick="return checkRegister(this.form)">
<input type="reset" name="Submit2" value="重 置"></TD>
</TR>
</FORM>
</TABLE>
<%Set rs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
2、个人资料修改提交页面(postEditUserInfo.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
passWord_form=Trim(Request.Form("passWord"))
userSex_form=Trim(Request.Form("userSex"))
EMail_form=Trim(Request.Form("Email"))
userSign_form=checkContent(Trim(Request.Form("userSign")))
getConn()
modifySql="update userInfo set passWord='"&passWord_form&"',userSex='"&userSex_form&"',userEMail='"&EMail_form&"',userSign='"&userSign_form&"' where userName='"&Session("userName")&"'"
conn.Execute(ModifySql)
Set conn=Nothing
errmsg="个人资料修改成功!"
returnURL="editUserInfo.asp"
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<%=errmsg%>
</BODY>
</HTMl>
9.3 栏目信息功能模块
9.3.1 论坛栏目浏览
1、论坛栏目浏览页面(mainPage.asp)
<!--#Include file="inc/top.asp"-->
<HTMl><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE><LINK rel="stylesheet" href="inc/css.css">
</HEAD>
<BODY><TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR><TD height="25" class="tr">
<TABLE width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
<TR><TD>当前位置:<A href=mainPage.asp>首页</A></TD></TR>
</TABLE></TD>
</TR></TABLE><BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25" colspan="2">|栏目介绍|</TD>
<TD align="center">主题数目</TD>
<TD align="center">回复数目</TD></TR>
<%'------查询栏目表中的所有栏目信息并显示
getConn()
boardSql="select * from board"
Set boardRs=conn.Execute(boardSql)
While Not boardRs.Eof
%>
<TR><TD width="60" height="53" align="center" class="td">
<img src="images/board.gif"></TD>
<TD width="475" class="td"><TABLE width="100%" border="0">
<TR><TD colspan="2">
<A href="topicList.asp?boardId=<%=boardRs("boardId")%>">
[<%=boardRs("boardName")%>]</A></TD></TR>
<TR><TD width="6%" align="right" valign="top">
<FONT face=Wingdings>v</FONT></TD>
<TD><%=boardRs("boardDescription")%></TD></TR></TABLE></TD>
<%'查询属于本栏目的帖子数目
Set rs1=Server.CreateObject("ADODB.Recordset")
topicSql="select * from topic where boardId='"&boardRs("boardId")&"'"
rs1.open topicSql,conn,1,1
%>
<TD width="65" align="center" class="td"><%=rs1.recordcount%></TD>
<%'查询属于本栏目的所有回帖数目
Set rs2=Server.CreateObject("ADODB.Recordset")
replySql="select * from reply,topic where topic.topicId=reply.topicId and topic.boardId='"&boardRs("boardId")&"'"
rs2.open replySql,conn,1,1
%>
<TD width="65" align="center" class="td"><%=rs2.recordcount%></TD>
</TR>
<%boardRs.Movenext
Wend
Set boardRs=nothing
Set rs1=nothing
Set rs2=nothing
%>
</TABLE>
<%'------判断登录用户是否为管理员,若是,才可显示下面的论坛管理板块
userSql="select * from userInfo where userName='"&Session("userName")&"'"
Set userRs=conn.execute(userSql)
If userRs("userLevel")=0 Then
%><BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr"><TD height="25" colspan="2">|论坛管理|</TD></TR>
<TR class="td"><TD width="67" height="57" align="center"><img src="images/board.gif"></TD>
<TD><TABLE width="100%" border="0">
<TR><TD colspan="2"><A href="admin.asp"> [论坛管理]</A></TD>
</TR>
<TR><TD width="4%" align="right" valign="top">
<FONT face=Wingdings>v</FONT></TD>
<TD>管理员进行操作</TD></TR></TABLE></TD></TR></TABLE>
<%End If
Set userRs=nothing
Set conn=nothing %>
</BODY></HTMl>
<!--#Include File="inc/bottom.htm"-->
9.3.2 栏目帖子浏览
1、栏目帖子浏览页面(topicList.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
getConn()
boardSql="select * from board where boardId='"&boardId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0" align="center">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A></TD>
<%userSql="select * from userInfo where userName='"&Session("userName")&"'"
Set userRs=conn.Execute(userSql)
If userRs("isLocked")=0 Then%>
<TD align="right"><img src="Images/topic/newTopic.gif"><A href="newTopic.asp?boardId=<%=boardId_string%>">发表新帖</A></TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr" align="center">
<TD height="25">主题(点击心情图片打开新窗口)</TD>
<TD>作者</TD>
<TD>回复/人气</TD>
<TD>发表时间</TD>
</TR>
<%Set userRs=Nothing
Set boardRs=Nothing
'------查询所有属于本栏目的帖子并按时间倒序显示
topicSql="select * from topic where boardId='"&boardId_string&"' order by addTime desc"
rs.open topicSql,conn,1,1
'------分页显示
'判断记录集是否为空
If rs.EOF then
Response.Write "记录集为空!"
Response.End
End If
'设置RecordSet对象的每一页数据记录的条数
rs.PageSize=4
'设置当前页
NowPage=Request.QueryString("page")
If NowPage="" then NowPage=1
NowPage=CLng(NowPage)
If NowPage<1 then NowPage=1
If NowPage>rs.PageCount then NowPage=rs.PageCount
rs.AbsolutePage=NowPage
'输出当前页面记录
For j=0 To rs.PageSize-1
%>
<TR class="td">
<TD height="25">
<A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=rs("topicId")%>" target="_blank"> <img src="<%=moodIdToPath(rs("topicMood"))%>" border=0></A>
<A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=rs("topicId")%>"><%=rs("topicTitle")%></A>
</TD>
<TD align="center"><A href="userInfo.asp?userName=<%=rs("userName")%>" target="_blank"><%=rs("userName")%></A></TD>
<TD align="center"><%=rs("replyCount")%>/<%=rs("readCount")%></TD>
<TD align="center"><%=rs("addTime")%></TD>
</TR>
<%
rs.MoveNext
If rs.Eof Then Exit For
Next
%>
<TR class="tr" align="center">
<Form action="" method="GET">
<TD height="25" colspan="4">
当前页:<%=NowPage%> (共 <%=rs.PageCount%> 页)
<%
'输出第一页、上一页、下一页和最后一页对应的超链
If NowPage<>1 then %>
|<A href="?page=1&boardId=<%=boardId_string%>">首页</A>|
|<A href="?page=<%=NowPage-1%>&boardId=<%=boardId_string%>">上一页</A>|
<%End If
If NowPage<>rs.PageCount then %>
|<A href="?page=<%=NowPage+1%>&boardId=<%=boardId_string%>">下一页</A>|
|<A href="?page=<%=rs.PageCount%>&boardId=<%=boardId_string%>">尾页</A>|
<%End If%>
请输入页号:<INPUT type="text" name="page" size=4>
<INPUT type="submit" value="转到">
<INPUT type="reset" value="取消">
<INPUT type="hidden" name="boardId" value=<%=boardId_string%>>
</TD>
</FORM>
</TR>
</TABLE>
<%Set rs=nothing
Set conn=nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
9.3.3 帖子与回复内容浏览
1、帖子与回复内容浏览页面(topicShow.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function confirmDelete(what){
if (what==1)
flag=confirm("确定要删除本条留言以及所有回复吗?");
else flag=confirm("确定要删除本条回复吗?");
return flag;
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
getConn()
topicSql="select * from board,topic,userInfo where topic.topicId="&topicId_string&" and topic.userName=userInfo.userName and topic.boardId=board.boardId"
Set topicRs=conn.Execute(topicSql)
conn.Execute("update topic set readCount="&topicRs("readCount")+1&" where topicId='"&topicId_string&"'")
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp> 栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=topicRs("boardName")%></A>>><A href="?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>"><%=topicRs("topicTitle")%></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25" colspan="2">
<TABLE width="98%" border="0">
<TR>
<TD> 标题:<img src="<%=moodIdToPath(topicRs("topicMood"))%>"><%=topicRs("topicTitle")%></TD>
<%Set userRs=conn.Execute("select * from userInfo where userName='"&Session("userName")&"'")
If userRs("isLocked")=false Then%>
<TD align="right"><img src="Images/topic/newReply.gIf"><A href="newReply.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>">我要回复</A></TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
<TR class="td">
<TD width="207">
<TABLE width="100%" border="0">
<TR>
<TD> <%=topicRs("userName")%><BR>
等级:<%=getUserLevel(topicRs("userLevel"))%><BR>
访问:<%=topicRs("visitCount")%><BR>
文章:<%=topicRs("topicCount")%><BR>
注册:<%=topicRs("regDate")%><BR>
E-Mail:<%=topicRs("userEMail")%>
</TD>
</TR>
</TABLE>
</TD>
<TD width="547">
<TABLE width="100%" border="0">
<TR>
<TD height="50">
<TABLE width="96%" border="0">
<TR>
<TD><img src="<%=moodIdToPath(topicRs("topicMood"))%>"><b><%=topicRs("topicContent")%></b></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<TABLE width="96%" border="0">
<TR>
<TD> ------------------------------------------------------------------------<BR>
签名:<%=topicRs("userSign")%>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD height="25"> 发表于:<%=topicRs("addTime")%></TD>
<TD>
<TABLE width="96%" border="0" align="center">
<TR>
<TD align="right">
<%'------判断权限,当前用户可以编辑自己的帖子,管理员可以删除所有帖子
If topicRs("userName")=Session("userName") _
And topicRs("isLocked")=False Then%>
|<A href="editTopic.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>">编辑</A>|
<%End If%>
<%If userRs("userLevel")=0 And userRs("isLocked")=False Then%>
|<A href="deleteTopic.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>" onClick="return confirmDelete(1);">删除</A>|
</TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
<%Set topicRs=Nothing
replySql="select * from reply,userInfo where reply.userName=userInfo.userName and reply.topicId="&topicId_string&" order by reply.replyTime desc"
Set replyRs=conn.execute(replySql)
While Not replyRs.Eof
%>
<TR class="td">
<TD width="207" height="25">
<TABLE width="100%" border="0">
<TR>
<TD> <%=replyRs("userName")%><BR>
等级:<%=getUserLevel(replyRs("userLevel"))%><BR>
访问:<%=replyRs("visitCount")%><BR>
文章:<%=replyRs("topicCount")%><BR>
注册:<%=replyRs("regDate")%><BR>
E-Mail:<%=replyRs("userEMail")%>
</TD>
</TR>
</TABLE>
</TD>
<TD width="547" height="25">
<TABLE width="100%" border="0">
<TR><TD > </TD></TR>
<TR>
<TD>
<TABLE width="96%" border="0">
<TR>
<TD><img src="<%=moodIdToPath(replyRs("replyMood"))%>"><%=replyRs("replyContent")%></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<TABLE width="96%" border="0">
<TR>
<TD> ------------------------------------------------------------------------<BR>
签名:<%=replyRs("userSign")%>
</TD>
</TR>
<TR><TD> </TD></TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD height="25"> 回复于:<%=replyRs("replyTime")%></TD>
<TD><TABLE width="98%" border="0">
<TR>
<TD align="right">
<%'------判断权限,当前用户可以编辑自己的回复,管理员可以删除所有回复
If replyRs("userName")=Session("userName") And replyRs("isLocked")=False Then%>
|<A href="editReply.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>&replyId=<%=replyRs("replyId")%>">编辑</A>|
<%End If%>
<%If userRs("userLevel")=0 And userRs("isLocked")=False Then%>
|<A href="deleteReply.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>&replyId=<%=replyRs("replyId")%>" onClick="return confirmDelete(0);">删除</A>|
</TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
<%
replyRs.Movenext
Wend
Set replyRs=Nothing
Set userRs=Nothing
Set conn=Nothing
%>
</TABLE>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
9.4 帖子管理功能模块
9.4.1 发表新帖
1、发表新帖页面(newTopic.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var title=form.title.value;
var content=form.content.value;
if(title==""){
alert("请填写帖子主题!");
form.title.focus();
return false;
}
else if(content==""){
alert("请填写帖子内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
getConn()
dim boardSql
boardSql="select * from board where boardId='"&boardId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>>发表新帖</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<%Set boardRs=Nothing%>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postTopic.asp" target="">
<TR class="td">
<TD height="25" colspan="2" align="center" class="tr">发表新帖</TD>
</TR>
<TR class="td">
<TD height="25" align="right">标题:</TD>
<TD><input name="title" type="text" size="70" value=""></TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD>
<%'------使用文件组件,找到“images\mood\”下的所有表情图片,循环显示
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%>
<input type="radio" name="mood" value=<%=moodPathToId(filePath)%>>
<img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR>
<%End If
Next%>
</TD>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD align="right">帖子内容:</TD>
<TD><textarea name="content" cols="70" rows="12"></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2">
<input type="submit" name="Submit1" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit2" value="重 置">
<input name="boardId" type="hidden" value=<%=boardId_string%>>
<input name="act" type="hidden" value="new">
</TD>
</TR>
</FORM>
</TABLE>
<%Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
2、新帖提交页面(postTopic.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
title_form=trim(Request.form("title"))
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
addTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新帖,则插入topic表一条新记录
If act_form="new" Then
topicSql="insert into topic values("&boardId_form&",'"&title_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&addTime&"',0,0)"
Set topicCountRs=conn.Execute("select * from userInfo where userName= '"&Session("userName")&"'")
conn.Execute("update userInfo set topicCount= "&topicCountRs("topicCount") +1&" where userName='"&Session("userName")&"'")
'------若是编辑帖子,则更新topic表的相关记录
Else
topicSql="update topic set topicTitle= '"&title_form&"',topicMood= '"&mood_form&"',topicContent='"&content_form&"',addTime='"&addTime&"' where topicId="&topicId_form&""
End If
conn.Execute(topicSql)
Set topicCountRs=Nothing
Set conn=Nothing
returnURL="topicList.asp?boardId="& boardId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.4.2 编辑帖子编辑帖子页面(editTopic.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var title=form.title.value;
var content=form.content.value;
if(title==""){
alert("请填写帖子主题!");
form.title.focus();
return false;
}
else if(content==""){
alert("请填写帖子内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
getConn()
boardSql="select * from board,topic where board.boardId='"&boardId_string&"' and board.boardId=topic.boardId and topic.topicId='"&topicId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="td">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>><A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>"><%=left(boardRs("topicTitle"),5)%>......</A>>>编辑帖子</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<%
Set boardRs=Nothing
topicSql="select * from topic where boardId='"&boardId_string&"' and topicId='"&topicId_string&"'"
Set topicRs=conn.Execute(topicSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postTopic.asp" target="">
<TR class="tr">
<TD height="25" colspan="2" align="center">编辑帖子</TD>
</TR>
<TR class="td">
<TD height="25" align="right">标题:</TD>
<TD><input name="title" type="text" size="70" value="<%=topicRs("topicTitle")%>"></TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD>
<%'------使用文件组件,找到“images\mood\”下的所有表情图片,循环显示
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%>
<%If topicRs("topicMood")=moodPathToId(filePath) then%>
<input type="radio" name="mood" value=<%=moodPathToId(filePath)%> checked>
<%else%>
<input type="radio" name="mood" value=<%=moodPathToId(filePath)%>>
<%End If%>
<img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR><%End If%>
<%next%>
</TD>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD align="right">帖子内容:</TD>
<TD><textarea name="content" cols="70" rows="12" id="content"><%=showContent(topicRs("topicContent"))%></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2">
<input type="submit" name="Submit6" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit62" value="重 置">
<input name="boardId" type="hidden" value="<%=boardId_string%>">
<input name="topicId" type="hidden" value="<%=topicId_string%>">
<input name="act" type="hidden" value="edit">
</TD>
</TR>
</FORM>
</TABLE>
<%Set topicRs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
编辑帖子提交页面(postTopic.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
title_form=trim(Request.form("title"))
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
addTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新帖,则插入topic表一条新记录
If act_form="new" Then
topicSql="insert into topic values("&boardId_form&",'"&title_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&addTime&"',0,0)"
Set topicCountRs=conn.Execute("select * from userInfo where userName='"&Session("userName")&"'")
conn.Execute("update userInfo set topicCount="&topicCountRs("topicCount")+1&" where userName='"&Session("userName")&"'")
'------若是编辑帖子,则更新topic表的相关记录
Else
topicSql="update topic set topicTitle='"&title_form&"',topicMood='"&mood_form&"',topicContent='"&content_form&"',addTime='"&addTime&"' where topicId="&topicId_form&""
End If
conn.Execute(topicSql)
Set topicCountRs=Nothing
Set conn=Nothing
returnURL="topicList.asp?boardId="& boardId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.4.3 删除帖子删除帖子页面(deleteTopic.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
'------删除主题及其所有回复
getConn()
deleteReplySql="delete from reply where topicId='"&topicId_string&"'"
conn.Execute(deleteReplySql)
deleteTopicSql="delete from topic where boardId='"&boardId_string&"' and topicId='"&topicId_string&"'"
conn.Execute(deleteTopicSql)
Set conn=Nothing
returnURL="topicList.asp?boardId="& boardId_string
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.5 回复管理功能模块
9.5.1 发表回复发表回复页面(newReply.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var content=form.content.value;
if(content==""){
alert("请填写回复内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
getConn()
dim boardSql
boardSql="select * from board where boardId='"&boardId_string&"'"
Set boardRs=conn.execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>>发表回复</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<%Set boardRs=Nothing%>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postReply.asp" target="">
<TR class="tr">
<TD height="25" colspan="2" align="center">发表回复</TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD width="2%"></TD>
<TD> <%Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%> <input type="radio" name="mood" value="<%=moodPathToId(filePath)%>">
<img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR>
<%End If
Next%>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD height="25" align="right">回复内容:</TD>
<TD><textarea name="content" cols="70" rows="12" ></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2"> <input type="submit" name="Submit6" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit62" value="重 置">
<input name="boardId" type="hidden" value="<%=boardId_string%>">
<input name="topicId" type="hidden" value="<%=topicId_string%>">
<input name="act" type="hidden" value="new"> </TD>
</TR>
</FORM>
</TABLE>
<%Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
发表回复提交页面(postReply.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
replyId_form=Request.Form("replyId")
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
replyTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新回复,则插入reply表一条新记录
If act_form="new" then
replySql="insert into reply(topicId,replyMood,replyContent,userName,replyTime) values('"&topicId_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&replyTime&"')"
Set replyCountRs=conn.Execute("select * from topic where topicId='"&topicId_form&"'")
conn.execute("update topic set replyCount="&replyCountRs("replyCount")+1&" where topicId='"&topicId_form&"'")
'------若是编辑回复,则更新reply表的相关记录
Else
replySql="update reply set replyMood='"&mood_form&"',replyContent='"&content_form&"',replyTime='"&replyTime&"' where replyId='"&replyId_form&"'"
End If
conn.Execute(replySql)
Set replyCountRs=Nothing
Set conn=Nothing
returnURL="topicshow.asp?boardId="& boardId_form & "&topicId="& topicId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.5.2 编辑回复编辑回复页面(editReply.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var content=form.content.value;
if(content==""){
alert("请填写回复内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
replyId_string=Request.QueryString("replyId")
getConn()
boardSql="select * from board,topic where board.boardId='"&boardId_string&"' and board.boardId=topic.boardId and topic.topicId='"&topicId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="td">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>><A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>"><%=left(boardRs("topicTitle"),5)%>......</A>>>编辑回复</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<%
Set boardRs=Nothing
replySql="select * from reply where topicId='"&topicId_string&"' and replyId='"&replyId_string&"'"
Set replyRs=conn.Execute(replySql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postreply.asp" target="">
<TR class="tr">
<TD height="25" colspan="2" align="center">编辑回复</TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD>
<%'------使用文件组件,找到“images\mood\”下的所有表情图片,循环显示
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%>
<%If replyRs("replyMood")=moodPathToId(filePath) then%>
<input type="radio" name="mood" value="<%=moodPathToId(filePath)%>" checked>
<%else%>
<input type="radio" name="mood" value="<%=moodPathToId(filePath)%>">
<%End If%> <img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR><%End If%>
<%next%>
</TD>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD align="right">回复内容:</TD>
<TD><textarea name="content" cols="70" rows="12"><%=showContent(replyRs("replyContent"))%></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2"> <input type="submit" name="Submit6" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit62" value="重 置">
<input name="boardId" type="hidden" value="<%=boardId_string%>">
<input name="topicId" type="hidden" value="<%=topicId_string%>">
<input name="replyId" type="hidden" value="<%=replyId_string%>">
<input name="act" type="hidden" value="edit"> </TD>
</TR>
</FORM>
</TABLE>
<%Set replyRs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
编辑回复提交页面(postReply.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
replyId_form=Request.Form("replyId")
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
replyTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新回复,则插入reply表一条新记录
If act_form="new" then
replySql="insert into reply(topicId,replyMood,replyContent,userName,replyTime) values('"&topicId_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&replyTime&"')"
Set replyCountRs=conn.Execute("select * from topic where topicId='"&topicId_form&"'")
conn.execute("update topic set replyCount="&replyCountRs("replyCount")+1&" where topicId='"&topicId_form&"'")
'------若是编辑回复,则更新reply表的相关记录
Else
replySql="update reply set replyMood='"&mood_form&"',replyContent='"&content_form&"',replyTime='"&replyTime&"' where replyId='"&replyId_form&"'"
End If
conn.Execute(replySql)
Set replyCountRs=Nothing
Set conn=Nothing
returnURL="topicshow.asp?boardId="& boardId_form & "&topicId="& topicId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.5.3 删除回复删除回复页面(deleteReply.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
replyId_string=Request.QueryString("replyId")
'------删除回复
getConn()
deleteReplySql="delete from reply where replyId='"&replyId_string&"'"
conn.Execute(deleteReplySql)
Set conn=Nothing
returnURL="topicShow.asp?boardId="& boardId_string&"&topicId="&topicId_string
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
</HEAD>
</HTMl>
9.6 论坛搜索功能模块论坛搜索页面(search.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
</HEAD>
<BODY>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href="search.asp">论坛搜索</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form1" action="" method="GET">
<TR class="tr">
<%
getConn()
%>
<TD height="25" width="150" align="right">查找帖子:</TD>
<TD>
<select name="board">
<option value="" selected>-所有栏目-</option>
<%
Set conditionRs=conn.Execute("select * from board")
While Not conditionRs.Eof
%>
<option value=<%=conditionRs("boardId")%>><%=conditionRs("boardName")%></option>
<%
conditionRs.Movenext()
Wend
Set conditionRs=Nothing
%>
</select>
<select name="topicType">
<option value="title" selected>标题</option>
<option value="content">内容</option>
<option value="userName">作者</option>
</select>
<input name="keyWord" type="text"size="40">
<input type="submit" name="submit1" value="搜索"></TD>
</TR>
</FORM>
</TABLE>
<%'------获取本页提交的表单数据
ifSubmit_string=Request.QueryString("submit1")
boardId_string=Request.QueryString("board")
topicType_string=Request.QueryString("topicType")
keyWord_string=trim(Request.QueryString("keyWord"))
'------若点击了搜索,则获取查询条件,查询记录并显示;否则不显示任何记录
'------初始化SQL语句
topicSql="select * from board,topic where topic.boardId=board.boardId"
If ifSubmit_string<>"" Then
If boardId_string<>"" Then
topicSql=topicSql & " and topic.boardId=" & boardId_string
End If
If topicType_string="title" Then
topicSql=topicSql & " and topic.topicTitle like'%"&keyWord_string&"%'"
ElseIf topicType_string="content" Then
topicSql=topicSql & " and topic.topicContent like'%"&keyWord_string&"%'"
Else
topicSql=topicSql & " and topic.userName like'%"&keyWord_string&"%'"
End If
topicSql=topicSql & " order by topic.boardId,topic.addTime desc"
%>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr" align="center">
<TD height="25">栏目</TD>
<TD>主题(点击心情打开新窗口)</TD>
<TD>作者</TD>
<TD>回复/人气</TD>
<TD>发表时间</TD>
<%
rs.open topicSql,conn,1,1
'------分页显示
'判断记录集是否为空
If rs.EOF then
Response.Write "记录集为空!"
Response.End
End If
'设置RecordSet对象的每一页数据记录的条数
rs.PageSize=4
'设置当前页
NowPage=Request.QueryString("page")
If NowPage="" then NowPage=1
NowPage=CLng(NowPage)
If NowPage<1 then NowPage=1
If NowPage>rs.PageCount then NowPage=rs.PageCount
rs.AbsolutePage=NowPage
'输出当前页面记录
For j=0 To rs.PageSize-1
%>
<TR class="td">
<TD height="25" align="center"><%=rs("boardName")%></TD>
<TD align="left">
<A href="topicShow.asp?boardId=<%=rs("boardId")%>&topicId=<%=rs("topicId")%>" target="_blank"> <img src="<%=moodIdToPath(rs("topicMood"))%>" border=0></A>
<A href="topicShow.asp?boardId=<%=rs("boardId")%>&topicId=<%=rs("topicId")%>"><%=rs("topicTitle")%></A>
</TD>
<TD align="center"><A href="userInfo.asp?userName=<%=rs("userName")%>" target="_blank"><%=rs("userName")%></A></TD>
<TD align="center"><%=rs("replyCount")%>/<%=rs("readCount")%></TD>
<TD align="center"><%=rs("addTime")%></TD>
</TR>
<%
rs.MoveNext
If rs.Eof Then Exit For
Next
%>
<TR class="tr" align="center">
<Form name="form1" action="" method="GET">
<TD height="25" colspan="5">
当前页:<%=NowPage%> (共 <%=rs.PageCount%> 页)
<%
'输出第一页、上一页、下一页和最后一页对应的超链
If NowPage<>1 then %>
|<A href="?page=1&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">首页</A>|
|<A href="?page=<%=NowPage-1%>&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">上一页</A>|
<%End If
If NowPage<>rs.PageCount then %>
|<A href="?page=<%=NowPage+1%>&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">下一页</A>|
|<A href="?page=<%=rs.PageCount%>&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">尾页</A>|
<%End If%>
请输入页号:<INPUT type="text" name="page" size=4>
<INPUT type="submit" value="转到">
<INPUT type="reset" value="取消">
<INPUT type="hidden" name="submit1" value=<%=ifSubmit_string%>>
<INPUT type="hidden" name="board" value=<%=boardId_string%>>
<INPUT type="hidden" name="topicType" value=<%=topicType_string%>>
<INPUT type="hidden" name="keyWord" value=<%=keyWord_string%>>
</TD>
</FORM>
</TR>
</TABLE>
<%End If
Set rs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
9.7 论坛管理功能模块
1、论坛管理页面(admin.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="JavaScript">
function checkForm(form){
var name=form.boardName.value;
var description=form.boardDescription.value;
if(name==""){
alert("请填写栏目名称!");
form.boardName.focus();
return false;
}
else if(description==""){
alert("请填写栏目说明!");
form.boardDescription.focus();
return false;
}
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href="admin.asp">论坛管理</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR align="center" class="tr">
<TD height="25" colspan="5">栏目管理</TD>
</TR>
<%
getConn()
boardSql="select * from board"
Set boardRs=conn.Execute(boardSql)
%>
<TR align="center" class="td">
<TD height="25" width="5%">编辑<BR>栏目</TD>
<TD><TABLE width="100%" border="1" bordercolor="#000000">
<TR align="center" class="td">
<TD height="25">栏目名称</TD>
<TD>栏目说明</TD>
<TD>操作</TD>
</TR>
<%While Not boardRs.Eof%>
<FORM method="post" action="postAdmin.asp">
<TR align="center" class="td">
<TD height="25" width="160"><input name="boardName" type="text" value="<%=boardRs("boardName")%>" size="20"> </TD>
<TD width="450"><input name="boardDescription" type="text" value="<%=boardRs("boardDescription")%>" size="60"></TD>
<TD><input type="hidden" name="act" value="modifyBoard">
<input type="hidden" name="boardId" value=<%=boardRs("boardId")%>>
<input type="submit" name="Submit1" value="修改" onclick="return checkForm(this.form);">
<A href="postAdmin.asp?boardId=<%=boardRs("boardId")%>&act=deleteBoard" onClick="return confirm('一旦删除数据不能再恢复,确定要删除吗?');">删除</A></TD>
</TR>
</FORM>
<%boardRs.Movenext()
Wend
Set boardRs=Nothing
%>
</TABLE>
</TD>
</TR>
</TABLE>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR align="center" class="td">
<TD height="25" width="5%">添加<BR>栏目</TD>
<FORM method="post" action="postAdmin.asp">
<TD height="25" width="162"><input name="boardName" type="text" size="20"></TD>
<TD width="450"><input name="boardDescription" type="text" size="60"></TD>
<TD><input type="hidden" name="act" value="newBoard">
<input type="submit" name="Submit1" value="添加" onclick="return checkForm(this.form);">
</TD>
</FORM>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR align="center" class="tr">
<TD height="25" colspan="5">用户管理
</TR>
<TR align="center" class="td">
<TD height="25">用户名</TD>
<TD>注册时间</TD>
<TD>等级</TD>
<TD>状态</TD>
<TD>操作</TD>
</TR>
<%
userSql="select * from userInfo where userName<>'"&Session("userName")&"'order by regDate"
Set userRs=conn.Execute(userSql)
While Not userRs.Eof
%>
<FORM method="post" action="postAdmin.asp">
<TR align="center" class="td">
<TD height="25"><A href="userInfo.asp?userName=<%=userRs("userName")%>" target="_blank"><%=userRs("userName")%></A></TD>
<TD><%=userRs("regDate")%></TD>
<TD><%=getUserLevel(userRs("userLevel"))%></TD>
<TD><%=getUserIsLocked(userRs("isLocked"))%></TD>
<TD>
<%If userRs("isLocked")=0 Then%>
<A href="postAdmin.asp?userId=<%=userRs("userId")%>&act=lockUser" onclick="return confirm('你确认要锁定<%=userRs("userName")%>吗?');">锁定</A>
<%Else%>
<A href="postAdmin.asp?userId=<%=userRs("userId")%>&act=unlockUser" onclick="return confirm('你确认要解除对<%=userRs("userName")%>的锁定吗?');">解锁</A>
<%End If%>
<A href="postAdmin.asp?userName=<%=userRs("userName")%>&act=deleteUser" onclick="return confirm('你确认要删除<%=userRs("userName")%>及其所有留言和回复吗?');">删除</A>
</TD>
</TR>
</FORM>
<%
userRs.Movenext
Wend
Set userRs=Nothing
%>
</TABLE>
<%Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
2、论坛管理提交页面(postAdmin.asp)
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取操作项目标识
act=Request("act")
getConn()
'------修改栏目信息
If act="modifyBoard" Then
boardId_form=Request.Form("boardId")
boardName_form=trim(Request.Form("boardName"))
boardDescription_form=trim(Request.Form("boardDescription"))
Set boardNameRs=conn.execute("select * from board where boardName= '"&boardName_form&"' and boardId<>'"&boardId_form&"'")
If Not boardNameRs.Eof Then
errmsg="栏目名称已存在,请更换名称!"
returnURL="JavaScript:History.Back()"
Else
Set modifyBoardRs=conn.execute("update board set boardName= '"&boardName_form&"',boardDescription= '"&boardDescription_form&"' where boardId='"&boardId_form&"'")
Set modifyBoardRs=Nothing
errmsg="栏目信息修改成功!"
returnURL="admin.asp"
End If
Set boardNameRs=Nothing
'------删除栏目
ElseIf act="deleteBoard" Then
board_string=Request.QueryString("boardId")
'删除栏目帖子的所有回复
Set topicIdRs=conn.execute _
("select * from topic where boardId='"&board_string&"'")
While Not topicIdRs.Eof
Set deleteReplyRs=conn.execute ("delete from reply where topicId='" &topicIdRs("topicId")&"'")
topicIdRs.Movenext
Wend
Set deleteReplyRs=Nothing
Set topicIdRs=Nothing
'删除栏目的所有帖子
Set deletetopicRs=conn.execute ("delete from topic where boardId='" &board_string&"'")
Set deleteTopicRs=Nothing
'删除栏目
Set deleteBoardRs=conn.execute ("delete from board where boardId='" &board_string&"'")
Set deleteBoardRs=Nothing
errmsg="栏目删除成功!"
returnURL="admin.asp"
'------添加新栏目
ElseIf act="newBoard" Then
boardName_form=trim(Request.Form("boardName"))
boardDescription_form=trim(Request.Form("boardDescription"))
Set boardNameRs=conn.execute ("select * from board where boardName='" &boardName_form&"'")
If Not boardNameRs.Eof Then
errmsg="栏目名称已存在,请更换名称!"
returnURL="javascript:history.back();"
Else
Set newBoardRs=conn.execute("insert board(boardName,boardDescription) values('"&boardName_form&"','"&boardDescription_form&"')")
Set newBoardRs=Nothing
errmsg="栏目添加成功!"
returnURL="admin.asp"
End If
Set boardNameRs=Nothing
'------锁定用户,使用户不能发帖和回复,只能浏览
ElseIf act="lockUser" Then
userId_string=Request.QueryString("userId")
Set userRs=conn.execute("update userInfo set isLocked=1 where userId= '"&userId_string&"'")
Set userRs=Nothing
errmsg="用户锁定成功!"
returnURL="admin.asp"
'------为已锁定用户解锁
ElseIf act="unlockUser" Then
userId_string=Request.QueryString("userId")
Set userRs=conn.execute ("update userInfo set isLocked=0 where userId='" &userId_string&"'")
Set userRs=Nothing
errmsg="用户解锁成功!"
returnURL="admin.asp"
'------删除用户
ElseIf act="deleteUser" Then
userName_string=Request.QueryString("userName")
'删除用户的所有回复
Set replyRs=conn.execute ("delete from reply where userName='" &userName_string&"'")
Set replyRs=Nothing
'删除用户的所有帖子的回复
Set topicIdRs=conn.execute ("select * from topic where userName='" &userName_string&"'")
While Not topicIdRs.Eof
Set deleteReplyRs=conn.execute ("delete from reply where topicId='" &topicIdRs("topicId")&"'")
topicIdRs.Movenext
Wend
Set deleteReplyRs=Nothing
Set topicIdRs=Nothing
'删除用户的所有帖子
Set deleteTopicRs=conn.execute ("delete from topic where userName='" &userName_string&"'")
Set deleteTopicRs=Nothing
Set deleteUserRs=conn.execute ("delete from userInfo where userName='" &userName_string&"'")
Set deleteUserRs=Nothing
errmsg="用户删除成功!"
returnURL="admin.asp"
End IF
Set conn=Nothing
%>
<HTMl><HEAD>
<META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
</HEAD><BODY><%=errmsg%></BODY></HTMl>
9.8 退出系统功能模块退出系统页面(logout.asp)
<%
Session.Abandon
Response.Redirect("default.htm")
%>
<HTML>
<HEAD>
<TITLE>HTML示例</TITLE>
</HEAD>
<BODY>
<FONT SIZE=7>欢迎访问!</FONT>
</BODY>
</HTML>
例1-2-1:
<%@ LANGUAGE = "VBScript" %>
<HTML>
<BODY>
<% For i = 3 To 7 %>
<FONT size=<% Response.Write i %>>
大家好!<BR>
</FONT>
<% Next %>
</BODY>
</HTML>
第2章 HTML基础例2-1-1:
<HTML>
<HEAD>
<TITLE>这里显示标题信息</TITLE>
</HEAD>
<BODY>
<P>这里是正文部分。</P>
</BODY>
</HTML>
例2-2-1:
<HTML>
<HEAD>
<TITLE>BODY属性示例</TITLE>
</HEAD>
<BODY background="img/0003.JPG" bgcolor="#FF0000" text="#FF3300" leftmargin="50" topmargin="50">
WWW(World Wide Web或万维网)是世界上最大的电子信息仓库,由众多的Web站点组成。每个Web站点都包含一些特定的资源,这些资源存放于一台或多台被称为Web服务器的计算机上。正是由于大量的Web站点提供了丰富多彩的资源,才使得用户能够通过网络快速、高效地获得他们需要的信息。
</BODY>
</HTML>
例2-3-1:
<HTML>
<HEAD><!-- 文档头部 -->
<TITLE>古诗鉴赏</TITLE>
</HEAD>
<BODY><!-- 文档主体部分 -->
<CENTER>
<H2>望庐山瀑布</H2><H5>李 白</H5>
<P>日照香炉生紫烟,<BR>遥看瀑布挂前川。<BR>飞流直下三千尺,<BR>
疑是银河落九天。</P>
</CENTER>
<HR align="center" width="90%" noshade>
<PRE>
[注释]
1.庐山:在江西省九江市南,是我国著名的风景区。
2.香炉:即香炉峰,在庐山西北,因形似香炉且山上经常笼罩着云烟而得名。
3.挂前川:挂在前面的水面上。
4.九天:古代传说天有九重,九天是天的最高层。
</PRE>
<DIV align="left">
这是诗人李白五十岁左右隐居庐山时写的一首风景诗。这首诗形象地描绘了庐山瀑布雄奇壮丽的景色,反映了诗人对祖国大好河山的无限热爱。
</DIV>
</BODY>
</HTML>
例2-3-2:
<HTML>
<HEAD><TITLE>物理字体和逻辑字体</TITLE></HEAD>
<BODY>
<DIV align="center">
<P><FONT color="#FF0000" size="5" face="华文新魏,隶书">物理字体和逻辑字体</FONT></P>
<TABLE width="75%" border="1">
<TR align="center"><TD>物理字体</TD><TD>逻辑字体</TD></TR>
<TR align="center">
<TD><B>粗体字</B><BR><I>斜体字</I><BR><U>增加下划线</U><BR><TT>打字机字体</TT><BR>普通文字<SUP>上标文字</SUP><BR>普通文字<SUB>下标文字
</SUB><BR><S>删除线文字</S></TD>
<TD><EM>强调文字</EM><BR><STRONG>字体加重</STRONG><BR><CODE>显示编程代码</CODE><BR><SAMP>输出示例文字</SAMP><BR><KBD>键盘按键文字</KBD>
<BR><VAR>定义变量或值</VAR><BR><DFN>定义文字</DFN><BR><CITE>引用文字
</CITE><BR><SMALL>缩小文字</SMALL><BR><BIG>放大文字</BIG></TD>
</TR>
</TABLE>
</DIV>
</BODY>
</HTML>
例2-4-1:
<HTML>
<HEAD><TITLE>列表的应用</TITLE></HEAD>
<BODY>
<TABLE width="100%" border="1">
<TR align="center"><TD>定义列表</TD><TD>有序列表</TD><TD>无序列表</TD></TR>
<TR><TD>
<DL>
<DT>HTML
<DD>超文本标记语言
<DD>是一种网络通用语言
<DT>互联网
<DD>网络的网络
</DL></TD>
<TD>
<OL>
<LI TYPE=A>星期一
<LI TYPE=a>星期二
<LI TYPE=I>星期三
<LI TYPE=i>星期四
<LI TYPE=1>星期五
</OL></TD>
<TD>
<UL>
<LI TYPE=circle>星期一
<LI TYPE=square>星期二
<LI TYPE=disc>星期三
<LI>星期四
<LI>星期五
</UL></TD></TR>
</TABLE>
</BODY>
</HTML>
例2-5-1:
<HTML>
<HEAD><TITLE>多媒体和超级链接的应用</TITLE></HEAD>
<BODY>
<TABLE width="100%" border="0">
<TR>
<TD><EMBED src="01.WMV" width=300 height=270 autostart=true></TD>
<TD>这是微软公司基于DirectShow基础之上开发的媒体播...以便更好的播放。</TD>
</TR>
</TABLE>
<P align="center"><A href="www.microsoft.com" target="_blank">官方网站</A> | <A href="wmplayer.exe">软件下载</A> | <A href="mailto:wpj@nciae.edu.cn">联系我们</A>
</BODY>
</HTML>
例2-6-1:
<HTML>
<HEAD><TITLE>表格的应用</TITLE></HEAD>
<BODY>
<TABLE width="500" cellspacing="1" cellpadding="5" border="1">
<TR>
<TD colspan="7" align="center" height="60"><H1>2005年9月</H1>
</TD></TR>
<TR bgcolor="#999999" align="center">
<TD>星期日</TD><TD>星期一</TD><TD>星期二</TD><TD>星期三</TD>
<TD>星期四</TD><TD>星期五</TD><TD>星期六</TD>
</TR>
<TR align="center">
<TD> </TD><TD> </TD><TD> </TD><TD> </TD>
<TD>1</TD><TD>2</TD><TD>3</TD>
</TR>
…
</TABLE>
</BODY>
</HTML>
例2-7-1:
<HTML>
<HEAD><TITLE>表单的应用</TITLE></HEAD>
<BODY>
<CENTER>注册个人信息</CENTER>
<FORM action="register.asp" method="post" name="register" target="_self">
姓名:<INPUT name="name" type="text" id="myname" maxlength="10" size="20">
<BR><INPUT name="myid" type="hidden" id="myid" value="personal">
性别:<SELECT name="sex">
<OPTION value="male" selected>男</OPTION>
<OPTION value="female">女</OPTION>
</SELECT><BR>
密码:<INPUT name="pwd" type="password" size="20" maxlength="20"><BR>
特长:<INPUT name="speciality" type="checkbox" value="computer" checked>计算机
<INPUT name="speciality" type="checkbox" value="sport">体育运动
<INPUT name="speciality" type="checkbox" value="music">音乐
<INPUT name="speciality" type="checkbox" value="art">艺术<BR>
年龄:<INPUT type="radio" name="age" value="1120">11-20
<INPUT name="age" type="radio" value="2130" checked>21-30
<INPUT type="radio" name="age" value="3140">31-40
<INPUT type="radio" name="age" value="4150">41-50<BR>
照片:<INPUT name="photo" type="file" id="photo" size="27" maxlength="50">
<BR>简介,<TEXTAREA name="resume" cols="36" rows="3">请如实填写!</TEXTAREA>
<BR><CENTER><INPUT type="submit" name="Submit" value="提交">
<INPUT type="reset" name="reset" value="重置"></CENTER>
</FORM>
</BODY>
</HTML>
例2-8-1:
<FRAMESET rows="…,…">
<FRAME>
<FRAMESET cols="…,…">
<FRAME>
<FRAME>
</FRAMESET>
</FRAMESET>
例2-9-1:
<HTML>
<HEAD>
<STYLE type="text/css">
<!--
BODY{font-family:"华文行楷";font-size:18pt;color:#ff0000}
-->
</STYLE>
<TITLE>CSS样式应用</TITLE>
</HEAD>
<BODY>
<P align="center">CSS样式基础</P>
<P align="center">CSS样式可以定义任何一个HTML标记。</P>
</BODY>
</HTML>
例2-9-2:
<HTML>
<HEAD>
<TITLE>用id属性定义样式表</TITLE>
<STYLE type="text/css">
<!--
#idstyle_1{font-family:"隶书";font-size:15pt;color:green}
#idstyle_2{font-family:"楷体";font-size:20pt;color:#00ff33}
-->
</STYLE>
</HEAD>
<BODY>
<CENTER>
<P><FONT id="idstyle_1">FONT的id属性用于应用CSS样式表</FONT></P>
<P><FONT id="idstyle_2">id属性的取值为事先定义的样式名称!</FONT></P>
</CENTER>
</BODY>
</HTML>
例2-9-3:
<HTML>
<HEAD>
<TITLE>使用class定义样式表</TITLE>
<STYLE type="text/css">
<!--
.classstyle_1{font-family:"黑体";font-size:24pt;color:blue}
.classstyle_2{font-family:"华文新魏";font-size:20pt;color:#ff6600}
-->
</STYLE>
</HEAD>
<BODY>
<CENTER>
<P class="classstyle_1">通过class属性应用样式表</P>
<P class="classstyle_2">可以同时定义多个class!</P>
</CENTER>
</BODY>
</HTML>
第3章 VBScript脚本语言例3-1-1:
<HTML>
<HEAD>
<SCRIPT language="VBScript">
Sub button1_OnClick
'判断用户名是否为空
If Trim(myform.username.value) = Empty Then
document.myform.elements(0).focus()
MsgBox "用户名不能为空!"
Exit Sub
End if
'判断用户密码是否正确
If Trim(myform.Pass1.value) = Empty Then
document.myform.elements(1).focus()
MsgBox "密码不能为空!"
Exit Sub
Else If len(myform.Pass1.value)>12 Then
document.myform.elements(1).focus()
MsgBox "密码不能超过12个字符!"
Exit Sub
Else If len(myform.Pass1.value)<6 Then
document.myform.elements(1).focus()
MsgBox "密码不能小于6个字符!"
Exit Sub
End If
End if
End if
myform.Submit
End Sub
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform" method="post" action="checklogin.asp">
<p>名称:<INPUT name="username" type="text" id="username"> </p>
<p>密码:<INPUT name="pass1" type="password" id="pass1"> </p>
<p><INPUT type="button" name="button1" id=button1 value="提交">
<INPUT type="reset" name="Submit2" value="重置"> </p>
</FORM>
</BODY>
</HTML>
checklogin.asp页面的具体代码如下:
<HTML>
<HEAD>
<TITLE>检查登录信息</TITLE>
</HEAD>
<BODY>
<%
'取得用户名和密码,并删除用户名和密码中的首尾空格
username=Trim(Request.Form("username"))
userpass=Trim(Request.Form("Pass1"))
'检查用户名和密码是否正确
If username="admin" then
If userpass="123456" then
Response.Write "欢迎你,管理员"
Else
Response.Write "<P>对不起,密码错误!</P>"
End If
Else
Response.Write "<P>你好!欢迎访问!</P>"
End If
Response.Write "<A href='login.htm'>返回</A>"
%>
</BODY>
</HTML>
例3-2-1:
<% @ LANGUAGE=VBScript %>
<% Option Explicit %>
<%
Dim intDegree
Dim intDegre
%>
例3-2-2:
<%
Option Explicit
Dim intX '声明脚本级变量
intX=1 '给脚本级变量赋值
SetLocalVariab1e '调用过程修改过程级变量的值
Response.Write intX '将脚本级变量的值发送到浏览器,值仍为1
Sub SetLocalVariab1e
Dim intX '声明过程级变量
intX=2 '给过程级变量赋值
End Sub
%>
例3-2-3:
<%
Option Explicit
Dim intX '声明脚本级变量
intX=1 '给脚本级变量赋值
SetLocalVariable '调用过程修改变量的值
Response.Write intX '将脚本级变量的值发送到浏览器,值为2
Sub SetLocalVariable
intX=2 '给脚本级变量赋值
End Sub
%>
例3-2-4:
<HTML>
<HEAD>
<TITLE>MsgBox使用方法</TITLE>
<Script Language="VBScript">
<!—
’定义子过程
Sub button1_OnClick
Dim intResult
intResult=MsgBox("你真的确定要提交吗?"&chr(13)&_
chr(10)&"提交后数据不可再更改。",4+32,"请你选择:")
’当点击不同按钮时给出不同响应
if intResult=6 then
MsgBox("提交成功!"&chr(13)&chr(10)&"请点击确定关闭窗口!")
else
MsgBox("数据未提交成功!")
end if
End Sub
-->
</Script>
</HEAD>
<BODY>
<INPUT type="button" name="button1" value="提交">
</BODY>
</HTML>
例3-2-5:
<HTML>
<HEAD>
<TITLE>InputBox函数的使用</TITLE>
</HEAD>
<BODY>
<Script Language="VBScript">
<!--
Dim strUserName,strUserAddress
strUserName=InputBox("请输入您的名字:","用户信息记录")
strUserAddress=InputBox("请输入您的住址:","用户信息记录")
MsgBox("您的基本信息为:"&chr(13)&chr(10)&"姓名:"&strUserName_
&chr(13)&chr(10)&"住址:"&chr(13)&chr(10)&strUserAddress)
-->
</Script>
</BODY>
</HTML>
例3-4-1:
<HTML>
<HEAD><TITLE>IF...ELSE嵌套语句</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub button1_OnClick()
Dim score
grade=trim(InputBox("请输入等级:(优、良、中、可、差)","学生等级","中"))
If grade="差" Then
score="成绩低于60分"
Else If grade="可" Then
score="成绩在60到70分之间。"
Else If grade="中" Then
score="成绩在70到80分之间。"
Else If grade="良" Then
score="成绩在80到90分之间。"
Else If grade="优" Then
score="成绩在90到100分之间。"
Else
score="输入的等级应该为(优、良、中、可、差)!"
End If
End If
End If
End If
End If
MsgBox score
End Sub
</SCRIPT></HEAD>
<BODY>
<INPUT type="button" name="button1" value="查看">
</BODY>
</HTML>
例3-4-2:
<HTML>
<HEAD><TITLE>Select Case语句</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub button1_OnClick()
Dim score
grade=trim(InputBox("请输入等级:(优、良、中、可、差)","学生等级","中"))
Select Case grade
Case "差" score="成绩低于60分,不及格!"
Case "可" score="成绩在60到70分之间,及格!"
Case "中" score="成绩在70到80分之间,中!"
Case "良" score="成绩在80到90分之间,良好!"
Case "优" score="成绩在90到100分之间,优秀!"
Case Else score="输入的等级应该为(优、良、中、可、差)!"
End Select
MsgBox score
End Sub
</SCRIPT></HEAD>
<BODY>
<INPUT type="button" name="button1" value="查看">
</BODY>
</HTML>
程序的执行结果与例3-4-1相同。
例3-4-3:
<HTML>
<HEAD>
<TITLE>流程控制语句的使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
Sub button1_OnClick
Document.Write("<Pre>")
Document.Write("*")
i=1
While i<10
Document.Write(Space(5)&i)
i=i+1
Wend
Document.WriteLn
For j=1 To 9
Document.Write(j&" ")
For k=1 To j
temp=j*k
If Len(temp)=1 Then
Document.Write(Space(4)&temp&" ")
Else
Document.Write(Space(3)&temp&" ")
End If
Next
Document.WriteLn
Next
Document.Write("</Pre>")
i=1
Do While i<60
Document.Write("*")
i=i+1
loop
End Sub
-->
</Script>
<BODY>
<INPUT type="button" name="button1" value="开始">
</BODY>
</HTML>
例3-5-1:
<HTML>
<HEAD>
<TITLE>函数的定义和使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
'产生固定范围并且没有重复的随机数
Function rndnum(a,k,w)
randomize
num=cint(fix(rnd*k))
m=0
while m<=w-1
if cint(a(m))=cint(num) then
randomize
num=cint(fix(rnd*k))
m=0
else
m=m+1
end if
wend
rndnum=num
End Function
Sub button1_OnClick
Dim arr(10)
For i=0 to 9
n=rndnum(arr,100,i)
arr(i)=n
Document.Write(n&" ")
Next
End Sub
-->
</Script>
<BODY>
<INPUT type="button" name="button1" value="开始">
</BODY>
</HTML>
例3-5-2:
<HTML>
<HEAD>
<TITLE>过程的定义和使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
Sub button1_OnClick
MsgBox("请等待2秒钟,马上开始!")
Call DelayTime(2)
MsgBox("窗前明月光,疑是地上霜。")
DelayTime(3)
MsgBox("举头望明月,低头思故乡。")
End Sub
Sub DelayTime(Delay)
Secondsinday=24*60*60
EndTime=Timer+Delay
If EndTime>Secondsinday Then
EndTime=EndTime-Secondsinday
Do While Timer>EndTime
Loop
End If
Do While Timer<EndTime
Loop
End Sub
-->
</Script>
<BODY>
<INPUT type="button" name="button1" value="开始">
</BODY>
</HTML>
例3-7-1:
<HTML>
<HEAD>
<TITLE>Window对象的使用</TITLE>
</HEAD>
<Script Language="VBScript">
<!--
Sub button1_OnClick
Window.Alert now()
End Sub
Sub button2_OnClick
Window.close
End Sub
Sub openwin()
Window.open "login.htm","新窗口","toolbar=no","rsizeable=no",
width=200,height=200
Window.status=”窗口已经打开!”
End Sub
-->
</Script>
<BODY OnLoad='openwin()'>
<INPUT type="button" name="button1" value="显示时间"><p>
<INPUT type="button" name="button2" value="关闭"><p>
</BODY>
</HTML>
例3-7-2:
<HTML>
<HEAD>
<TITLE>按顺序输入注册信息</TITLE>
<Script id=clientEventHandlersVBS Language="VBScript">
<!--
Sub Windows_onload
Form1.xm.Focus
End Sub
Sub xm_onkeypress
If Window.Event.KeyCode=13 then
Form1.mm.Focus
End If
End Sub
Sub mm_onkeypress
If Window.Event.KeyCode=13 then
Form1.qrmm.Focus
End If
End Sub
Sub qrmm_onkeypress
If Window.Event.KeyCode=13 then
Form1.nl.Focus
End If
End Sub
Sub nl_onkeypress
If Window.Event.KeyCode=13 then
Form1.dh.Focus
End If
End Sub
Sub dh_onkeypress
If Window.Event.KeyCode=13 then
Form1.zz.Focus
End If
End Sub
Sub zz_onkeypress
If Window.Event.KeyCode=13 then
Form1.email.Focus
End If
End Sub
Sub email_onkeypress
If Window.Event.KeyCode=13 then
Form1.Button1.Focus
End If
End Sub
Sub button1_OnClick
Dim txt1,txt2,txt3,txt4,txt5,txt6,txt7,alltxt
txt1=Form1.xm.value
txt2=Form1.nl.value
txt3=Form1.dh.value
txt4=Form1.zz.value
txt5=Form1.mm.value
txt6=Form1.qrmm.value
txt7=Form1.email.value
If txt1="" then
MsgBox "姓名不能为空!"
Form1.xm.Focus
Exit Sub
End If
If txt5="" then
MsgBox "密码不能为空!"
Form1.mm.Focus
Exit Sub
End If
If Len(txt5)<6 or Len(txt5)>12 Then
MsgBox "密码应该在6到12位之间。"
Form1.mm.Focus
Exit Sub
End If
If txt6="" then
MsgBox "确认密码不能为空!"
Form1.qrmm.Focus
Exit Sub
End If
If txt5<>txt6 Then
MsgBox "密码和确认密码不相同!"
Form1.qrmm.Focus
Exit Sub
End If
If txt3="" then
MsgBox "电话不能为空!"
Form1.dh.Focus
Exit Sub
End If
If not isNumeric(txt2) Then
MsgBox "请输入数字!"
Form1.nl.Focus
Exit Sub
End If
If (InStr("@",txt7)=0 and InStr(".",txt7)=0) then
MsgBox "请输入有效的email地址!"
Form1.email.Focus
Exit Sub
End If
alltxt="姓名为:"+txt1+vbcrlf
alltxt=alltxt+"年龄为:"+txt5+vbcrlf
alltxt=alltxt+"年龄为:"+txt2+vbcrlf
alltxt=alltxt+"电话为:"+txt3+vbcrlf
alltxt=alltxt+"住址为:"+txt4
intb=MsgBox(alltxt,4+32,"确认提交")
If intb=6 Then
Form1.submit
End If
End Sub
Sub button1_onkeypress
If Windows.Event.KeyCode=13 then
button1_onclick
End If
End Sub
-->
</Script>
</HEAD>
<BODY bgcolor=White>
<FORM id=Form1 method="post" action="zhuce.asp">
<LEFT>
<p>姓名:<INPUT id=xm name=xm></p><br>
<p>密码:<INPUT type="password" id=mm name=mm>
确认密码:<INPUT type="password" id=qrmm name=qrmm></p><br>
<p>年龄:<INPUT id=nl name=nl>
电话:<INPUT id=dh name=dh></p><br>
<p>住址:<INPUT id=zz name=zz>
email:<INPUT id=email name=email></p><br>
<INPUT id=button1 name=button1 type=button value="提交">
<INPUT id=button2 name=button2 type=reset value="重填">
</LEFT>
</FORM>
</BODY>
</HTML>
例3-8-1:
<HTML>
<HEAD>
<TITLE>文档加载错误</TITLE>
<Script Language="VBScript">
<!--
Dim intVar,intResult,strVar
intVar=120
strVar="Tom"
’在进行计算时类型不匹配
intResult=intVar*strVar
MsgBox("结果为:"&intResult)
-->
</Script>
</HEAD>
<BODY>
...
</BODY>
</HTML>
例3-8-2:
<HTML>
<HEAD>
<TITLE>网页运行错误</TITLE>
<Script Language="VBScript">
<!--
Sub config()
MsgBox("你好!")
End Sub
Sub btn1_OnFocus
Window.status="欢迎光临!"
End Sub
Sub btn1_OnBlur
Window.status="欢迎下次再来!"
’将对象的名字拼写错误
Windows.close
End Sub
-->
</Script>
</HEAD>
<BODY>
<INPUT type="button" name="btn1" value="点击" onClick="config()">
</BODY>
</HTML>
第4章 Response与Request对象例4-2-1:
<%@ LANGUAGE="VBScript" %>
<% Response.Buffer=True %>
<HTML>
<HEAD>
<TITLE>使用缓冲示例</TITLE>
</HEAD>
<BODY>
<%
firsttime=Timer
For i = 1 To 5000
Response.Write "i的值为:"&i&space(4)
if i mod 4=0 then Response.Write "<br>"
Next
secondtime=Timer
jg=secondtime-firsttime
Response.write "用时:"&jg&" 秒"
%>
</BODY>
</HTML>
<%@ LANGUAGE="VBScript" %>
<% Response.Buffer=False %>
<HTML>
<HEAD>
<TITLE>不使用缓冲示例</TITLE>
</HEAD>
<BODY>
<%
firsttime=Timer
For i = 1 To 5000
Response.Write "i的值为:"&i&space(4)
if i mod 4=0 then Response.Write "<br>"
Next
secondtime=Timer
jg=secondtime-firsttime
Response.write "用时:"&jg&" 秒"
%>
</BODY>
</HTML>
例4-2-2:
<%
'输出字符串
Response.Write "这是一个字符串,"
'输出HTML标记
Response.Write "<BR>"
'输出字符和HTML标记
Response.Write "<p><font color='red'>这是一个红色的字符串</font></p>"
Response.Write "<p>欢迎访问<a href='http://www.myhome.com.cn'>我的网页</a></p>"
'输出数字、字符和HTML标记
Response.Write "The sum is," & 1+2 & "<BR>"
'输出变量的值
a="这是一个字符串,"
b="<font color='red'>这是一个红色的字符串</font>"
Response.Write a & b & "<BR>结束!"
%>
例4-2-3:
<%
'显示字符串
Response.Write " 同学们好! "&"<BR>"
'显示带引号的字符串
Response.Write """"&" 同学们好! "&""""&",他说到,"
'显示"%>"
Response.Write """"&"服务器脚本以'%\>'结束 "&""""&"<BR>"
%>
例4-2-4:
<%
Response.Buffer=True
%>
<HTML>
<BODY>
<%
'获取系统当前时间
dtmHour = Hour(Now())
'判断是否为上午
If dtmHour >= 8 and dtmHour <= 18 Then
Response.Redirect "work.htm"
Else
Response.Redirect "rest.htm"
End If
%>
</BODY>
</HTML>
例4-2-5:
<% Response.Buffer=True %>
<HTML>
<BODY>
<%
today=weekday(Date())
If today=1 or today=7 Then
Response.write "<H1>今天休息!</H1>"
Response.End
End If
%>
<H1>欢迎光临!</H1>
</BODY>
</HTML>
例4-3-1:
Input1.htm,代码如下:
<HTML>
<BODY>
<FORM method="POST" action="Output1.asp">
<h4 align="center">欢迎访问,请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10"></P>
<P align="center"><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
Output1.asp,代码如下,
<%
name=Request.Form("Username")
pass=Request.Form(2)
If name<>"" then
Response.Write "<P>谢谢!</P>"
Response.Write "<P>您输入的用户名是:" & name & "</P>"
Response.Write "<P>您输入的密码是:" & pass & "</P>"
Else
Response.Write "<P>对不起,请重新输入!"
Response.Write "<a href='input1.htm'>上一页</a>"
End If
%>
io.asp,代码如下:
<%
If Request.Form("ok")<>"确定" then
%>
<HTML>
<BODY>
<FORM method="POST" action="io.asp">
<h4 align="center">欢迎访问,请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10"></P>
<P align="center"><INPUT name="ok" type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
<%
Else
name=Request.Form("Username")
pass=Request.Form("Userpass")
If name<>"" then
Response.Write "<P>谢谢!</P>"
Response.Write "<P>您输入的用户名是:" & name & "</P>"
Response.Write "<P>您输入的密码是:" & pass & "</P>"
Else
Response.Write "<P>对不起,请重新输入!"
Response.Write "<a href='io.asp'>上一页</a>"
End If
End If
%>
例4-3-2:
Input2.htm,代码如下:
<HTML>
<BODY>
<FORM method="POST" action="Output2.asp">
<TABLE border="1" width="100%" bordercolor="#C0C0C0">
<TR>
<TD width="18%">姓名:<INPUT type="text"name="Username" size="10"></TD>
<TD width="82%">性别:<INPUT type="radio" name="sex" value="男" checked>男
<INPUT type="radio" name="sex" value="女" >女</TD>
</TR>
<TR>
<TD width="100%" valign="baseline" colspan="2">近期的购买计划,
<INPUT name="buy" type=checkbox value="计算机">计算机
<INPUT name="buy" type=checkbox value="电视 ">电视
<INPUT name="buy" type=checkbox value="汽车">汽车
<INPUT name="buy" type=checkbox value="房子">房子
</TD>
</TR>
<TR>
<TD width="18%" valign="top">您上网的主要目的:
<p>(按CTRL键可多选)</p>
</TD>
<TD width="82%"><SELECT name="purpose" multiple size="4">
<option value="查资料">查资料</option>
<option value="聊天">聊天</option>
<option value="学习">学习</option>
<option value="娱乐">娱乐</option>
</SELECT>
</TD>
</TR>
</TABLE>
<BR>
<INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
Output2.asp,代码如下:
<HTML>
<BODY>
<%
user=request.form("Username")
sex=request.form("sex")
If sex="女" then
user=user&"女士"
Else
user=user&"先生"
End If
response.write user&":您好!<BR>"
count=Request.Form("buy").count
Response.Write "<HR><BR>根据我们的调查,您的购买计划有" & count &"种,其中包括:<BR>"
For i=1 to count
Response.Write Request.Form("buy")(i)&"<BR>"
Next
count=Request.Form("purpose").count
Response.Write "<HR><BR>您上网主要目的" & count & "种,主要有:<BR>"
For i=1 to count
Response.Write Request.Form("purpose")(i)&"<BR>"
Next%>
</BODY>
</HTML>
例4-3-3:
Input3.asp,代码如下:
<HTML>
<BODY>
<H4>欢迎访问,请完成您的选择</H4>
<HR>
<A HREF="Output3.asp?work=查询数据&user=普通用户">查询数据</A>
<A HREF="Output3.asp?work=添加数据&user=权限用户">添加数据</A>
<%
Response.Write "<A HREF='Output3.asp?work=修改数据&user=权限用户'>修改数据</A> "
Response.Write "<A HREF='Output3.asp?work=删除数据&user=权限用户'>删除数据</A>"
%>
</BODY>
</HTML>
Output3.asp,代码如下:
<%
username=Request.QueryString("user")
userwork=Request.QueryString("work")
Response.Write username & " 您好!您选择了:" & userwork
%>
例4-3-4:
Input4.htm,代码如下:
<HTML>
<BODY>
<FORM method="GET" action="Output4.asp">
<P>姓名:<INPUT type="text"name="Username" size="10"></P>
<P>性别:<SELECT name="Sex" size="1">
<option value="男">男</option>
<option value="女">女</option>
</SELECT>
</P>
<P><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</P>
</FORM>
</BODY>
</HTML>
Output4.asp,代码如下:
<HTML>
<BODY>
<P>您的姓名是:<%Response.Write Request.QueryString("Username")%>。</P>
<P>您的性别是:<%Response.Write Request.QueryString("Sex")%>!</P>
</BODY>
</HTML>
例4-3-5:
<%
name=Request.Cookies("username")
pass=Request.Cookies("username")
%>
<HTML>
<BODY>
<FORM method="POST" action="cookiewrite.asp">
<h4 align="center">欢迎访问,请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10" value=<%=name%>></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10" value=<%=pass%>></P>
<P align="center"><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY>
</HTML>
cookiewrite.asp,代码如下:
<%
username=Request.form("username")
userpass=Request.form("userpass")
Response.Cookies("username")=username
Response.Cookies("username").Expires=Date()+7
Response.Cookies("userpass")=userpass
Response.Cookies("userpass").Expires=Date()+7
%>
例4-3-6:
<%
Dim strip
strip=Request.ServerVariables("REMOTE_ADDR")
If strip="127.0.0.1" then
Response.Write "谢谢您的访问!"
Else
Response.Write "对不起,拒绝访问!"
End If
%>
4.4 综合实例
userlogin.asp,代码如下:
<HTML>
<HEAD>
<TITLE>用户登录</TITLE>
</HEAD>
<BODY><CENTER>
<H4>请输入用户名并密码</H4>
<HR>
<TABLE border=0>
<FORM action="userdeal.asp" method="POST">
<TR>
<TD>用户名:</TD>
<TD><INPUT type="INPUT" NAME="Username" size="10" VALUE=<%=Request.QueryString("username")%> ></TD>
</TR>
<TR>
<TD>密 码:</TD>
<TD><INPUT type="PASSWORD" NAME="UserPass" size="10" ></TD>
</TR>
<TR>
<TD colspan=2 align="center"><INPUT type="SUBMIT" value="登录">
<INPUT type="RESET" value="取消"></TD>
</TR>
</FORM>
</TABLE>
<P><FONT color="red"><%=Request.QueryString("msg")%></FONT></P>
</CENTER>
</BODY>
</HTML>
userdeal.asp,代码如下
<%
' 设置提示信息
strNo = "用户名或密码为空,请输入正确的用户名和密码!"
strBadName = "对不起!输入的用户名不存在!"
strBadPass = "对不起!输入的密码错误!"
' 取得网页表单的值
strName = Request.Form("Username")
strPass = Request.Form("Userpass")
' 是否输入用户名和密码
If strName = "" or strPass = "" Then
' strName=""
Response.Redirect "userlogin.asp?msg=" & strNo & "&UserName=" & strName
End If
' 检查密码
If strname="guest" or strname="admin" Then
' 密码正确,找到用户
If strname="guest" and strPass="001" Then
'进入网站的网页
Response.Redirect "main.asp?user=guest"
Else If strname="admin" and strPass="002" Then
Response.Redirect "main.asp?user=admin"
Else
'密码错误
Response.Redirect "userlogin.asp?msg="&strBadPass &"&UserName=" & strName
End If
End If
Else
'用户错误
strName=""
Response.Redirect "userlogin.asp?msg=" & strBadName & "&UserName=" & strName
End If
%>
main.asp,代码如下:
<%
user=Request.QueryString("user")
Response.write "<H3>欢迎 " & user &" 访问本网站!</H3>"
%>
第5章 Session、Application和Server对象例5-1-1:
session.htm,代码如下:
<HTML><BODY>
<FORM method="POST" action="session.asp">
<h4 align="center">请输入您的用户名和年龄</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">年 龄:<INPUT type="text" name="Userage" size="10"></P>
<P align="center"><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消">
</FORM>
</BODY></HTML>
session.asp,代码如下,
<%
user=Request.Form("Username")
age=Request.Form("Userage")
Response.Write "回显用户输入的数据:<BR>"
Response.Write "用户名:" & user
Response.Write "年龄:" & age & "岁<BR>"
session("user")=user
session("age")=age
Response.Write "<a href='nextpage.asp'>下一页</a>"
%>
nextpage.asp,代码如下:
<%
Response.Write "session中的数据为:<BR>"
Response.Write "用户名:" & session("user")
Response.Write "年龄:" & session("age")
%>
例5-1-2:
<%
Response.Write "SessionID为:" & Session.SessionID & " "
Session("MyName") = "Mary"
Response.Write "Session变量的值为:" & Session("MyName")
Session.Abandon
Response.Write "<P><a href='abandon1.asp'>Abandon</a></P>"
Response.Write "SessionID为:" & Session.SessionID & " "
Response.Write "Session变量的值为:" & Session("MyName")
%>
当用户点击“Abandon”超链接时,将访问abandon1.asp文件,该文件代码如下:
<%
Response.Write "SessionID为:" & Session.SessionID & " "
Response.Write "Session变量的值为:" & Session("MyName")
%>
例5-2-1:
网站登录页面代码如下:
<%
If Request.Form("ok")="确定"then
user=Request.Form("Username")
pass=Request.Form("Userpass")
If user<>"" then
session("user")=user
session("pass")=pass
Response.Redirect "counter.asp"
End If
End If
%>
<HTML>
<BODY>
<FORM method="POST" action="app.asp">
<h4 align="center">请输入您的用户名和密码</h4>
<P align="center">用户名:<INPUT type="text" name="Username" size="10"></P>
<P align="center">密 码:<INPUT type="password" name="Userpass" size="10"></P>
<P align="center"><INPUT type="submit" name="ok" value="确定">
<INPUT type="reset" value="取消"> </P>
</FORM>
</BODY>
</HTML>
counter.asp,代码如下:
<%
If session("user")<>"" then
Response.Write "欢迎"&Session("user")&"访问本网站"&"<BR>"
session("user")=""
Application("counter")=Application("counter")+1
Response.Write "您是本网站的第"&Application("counter")&"个访问者"
Else
Response.Write "对不起,请首先访问网站的登录页面!<BR>"
Response.Write "<P><a href='login.asp'>登录页面</a></P>"
End If
%>
例5-3-1:
<%
Response.Write "表单的基本格式为:<BR>"
Response.Write Server.HTMLEncode("<FORM action= URL method=postmethod >")&"<BR>"
Response.Write "…<BR>"
Response.Write Server.HTMLEncode("</FORM>")&"<BR>"
%>
例5-3-2:
<%
'取得当前Web站点的物理路径
physicalpath=Server.Mappath("\")
Response.Write physicalpath&"<BR>"
'取得当前ASP文件所在目录下example目录的物理路径
physicalpath=Server.Mappath("example")
Response.Write physicalpath&"<BR>"
'取得当前Web站点下名为asp虚拟目录下login.asp文件的物理路径
physicalpath=Server.Mappath("/asp/login.asp")
Response.Write physicalpath&"<BR>"
'取得当前asp文件的物理路径
physicalpath=Server.Mappath(Request.ServerVariables("PATH_INFO"))
Response.Write physicalpath&"<BR>"
'取得当前asp文件的父目录
physicalpath=Server.Mappath("..\")
Response.Write physicalpath&"<BR>"
%>
5.4 网上聊天室
Global.asa,代码如下:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
Dim temptalk(5)
Application("talk")=temptalk
End Sub
</SCRIPT>
login.asp,代码如下:
<HTML>
<HEAD>
<TITLE>欢迎使用</TITLE>
</HEAD>
<BODY>
<H4 align="center">欢迎进入网上聊天室</H4>
<HR>
<P align="center">请输入您的呢称:</P>
<FROM method="POST" action="check.asp">
<P align="center"><input type="text" name="username" size="20"></P>
<P align="center"><input type="submit" value="进入">
<input type="reset" value="重写" ></P>
</FROM>
<P align="center"><FONT color="red"><%=Request.QueryString("msg")%>
</FONT></P>
</BODY>
</HTML>
check.asp,代码如下:
<%
user=trim(Request.Form("username"))
If user="" then
Response.Redirect "login.asp?msg=对不起,用户名不能为空!"
Else
items=split(application("people"),",")
'检查用户名是否重名
If instr(1,Application("people"),user&",")<>0 then
Response.Redirect "login.asp?msg=对不起,用户名重名!"
End If
'检查聊天室是否满员
If ubound(items)>3 then
Response.Redirect "login.asp?msg=对不起,聊天室满员!"
End If
Session("curruser")=user
Application.lock
Application("people")=application("people")&user&","
temptalk=Application("talk")
For i=5 to 1 step -1
temptalk(i)=temptalk(i-1)
Next
temptalk(0)="(" & time & ")" & user & "说:大家好!"
Application("talk")=temptalk
Application.unlock
Response.Redirect "main.asp"
End If
%>
main.asp,代码如下:
<HTML>
<HEAD>
<TITLE>网上聊天室</TITLE>
</HEAD>
<Frameset rows="70%,*" onunload=open("quit.asp")>
<Frameset cols="69%,*">
<Frame name="ltop" target="ltop" scrolling="auto" noresize src="talk.asp">
<Frame name="rtop" target="rtop" scrolling="auto" noresize src="userlist.asp">
</Frameset>
<Frameset cols="100%">
<Frame name="bottom" scrolling="auto" noresize src="talking.asp">
</Frameset>
<Noframes>
<BODY>
<P>此网页使用了框架,但您的浏览器不支持框架。</P>
</BODY>
</Noframes>
</Frameset>
</HTML>
talking.asp,代码如下:
<%
If Request.Form("Quit")="退出" Then
Response.Redirect "quit.asp"
End If
If request("content")<>"" then
name=Session("curruser")
temptalk=Application("talk")
Str="("&time&")"&name&"说:" & Server.HtmlEncode(Request.Form("content"))
Application.lock
For i=5 to 1 step -1
temptalk(i)=temptalk(i-1)
Next
temptalk(0)=str
Application("talk")=temptalk
Application.unlock
End If
%>
<HTML>
<BODY bgcolor="rgb(230,300,100)">
<Form method="post" action="talking.asp">
<INPUT type="text" name="content" size="50">
<INPUT type="submit" name="ok" value="提交">
<INPUT type="submit" name="quit" value="退出">
</FORM>
</BODY>
</HTML>
talk.asp,代码如下:
<HTML>
<HEAD>
<META http-equiv="refresh" content="5; url=talk.asp">
<TITLE>聊天内容</TITLE>
<BASE target="ltop">
</HEAD>
<BODY bgcolor="rgb(200,200,200)">
<%
temptalk=Application("talk")
For i=0 to 5
Response.Write temptalk(i) & "<BR>"
Next
%>
</BODY>
</HTML>
userlist.asp,代码如下:
<HTML>
<HEAD>
<META http-equiv="refresh" content="5; url=userlist.asp">
<TITLE>聊天成员</TITLE>
<BASE target="rtop">
</HEAD>
<BODY bgcolor="rgb(224,241,227)">
<FONT color="#ff00ff">在线人员名单:</FONT><BR>
<%
'显示在线人员
users=split(application("people"),",")
For i=0 To ubound(users)
Response.write "<FONT color=green>"&"-"&users(i)&"-"&"</FONT><BR>"
Next
%>
</BODY>
</HTML>
本页面要将application("people")变量中的内容显示出来,并定时“刷新”页面。
quit.asp,代码如下:
<%
name=Session("curruser")
If name<>"" then
Application.unlock
Application("people")=Replace(Application("people"),name&",","")
temptalk=Application("talk")
For i=5 to 1 step -1
temptalk(i)=temptalk(i-1)
Next
temptalk(0)="(" & time & ")" & name & "说:我走了!"
Application("talk")=temptalk
Application.unlock
Session.abandon
End If
%>
第6章 ASP组件例6-2-1:
adrot.txt,代码如下:
REDIRECT AdRedirect.asp
WIDTH 200
HEIGHT 100
BORDER 1
*
baidu.gif
http://www.baidu.com
百度搜索
30
google.gif
http://www.google.com
goole搜索
40
sogua.gif
http://www.sogua.com
搜刮
30
AdRedirect.asp,代码如下:
<%
'重定向到对应广告图像指定的URL
Response.Redirect (Request.QueryString("url"))
%>
AdRotatorEx.asp,代码如下:
<HTML>
<HEAD><TITLE>梦想家园!</TITLE></HEAD>
<BODY>
<BR>
<H1 align="center">欢迎光临“梦想家园”</H1>
<HR>
<%
Set Ad=Server.CreateObject("MSWC.AdRotator")
%>
<CENTER><%=Ad.GetAdverTisement("adrot.txt")%></CERTER>
</BODY>
</HTML>
例6-3-1:
link.txt,代码如下:
chapter1.asp 第一章:ASP基础
chapter2.asp 第二章:HTML基础
chapter3.asp 第三章:VBSCRIPT脚本语言
chapter4.asp 第四章:RESPONSE和REQUEST对象
chapter5.asp 第五章:SESSION、APPLICATION和SERVER对象
chapter6.asp 第六章:ASP组件
chapter7.asp 第七章:关系数据库基础
chapter8.asp 第八章:ADO对象
chapter9.asp 第九章:设计实例——网上论坛
ContentLinkingEx.asp,代码如下:
<HTML>
<HEAD>
<TITLE>电子图书之《ASP程序设计》</TITLE>
</HEAD>
<BODY>
<H2>《ASP程序设计》</H2>
<%
Set bookLink=Server.CreateObject("MSWC.NextLink")
count=bookLink.GetListCount("link.txt")
%>
<P>本书共有<%=count%>章,点击查看详细内容。</P>
<UL>
<%
For i=1 To count
url=bookLink.GetNthURL("link.txt",i)
descript=bookLink.GetNthDescription("link.txt",i)
%>
<LI><A HREF="<%=url%>"><%=descript%></LI>
<%next%>
</UL>
</BODY>
</HTML>
chapter6.asp,代码如下:
<HTML>
<HEAD>
<TITLE>第六章:ASP组件</TITLE>
</HEAD>
<BODY>
<H2>第六章:ASP组件</H2>
<!--此处是本章内容-->
<B>本章将详细介绍多种ASP组件的功能与实际应用.....,</B>
<%Set bookLink=Server.CreateObject("MSWC.NextLink")
count=bookLink.GetListCount("link.txt")
current=bookLink.GetListIndex("link.txt")
If current>1 Then
%>
<P>
<A HREF="<%=bookLink.GetPreviousURL("link.txt")%>">上一页</A>
<%End If%>
<%
If current<count Then
%>
<A HREF="<%=bookLink.GetNextURL("link.txt")%>">下一页</A>
<%End if%>
<A HREF="ContentLinkingEx.asp">返回目录</A>
</P>
</BODY>
</HTML>
例6-4-1,
<HTML>
<BODY>
本页面已经被浏览了<%=WebCounter.Increment("PageCount") %>次
</BODY>
</HTML>
例6-4-2,
CouterEx.asp,代码如下:
<HTML>
<HEAD><TITLE>民意调查!</TITLE></HEAD>
<BODY><FORM method="post" action="result.asp">
您对本网站的满意程度是:
<P><INPUT TYPE="radio" VALUE="V1" NAME="R1">非常满意</P>
<P><INPUT TYPE="radio" VALUE="V2" NAME="R1">比较满意</P>
<P><INPUT TYPE="radio" VALUE="V3" NAME="R1">不满意</P>
<P><INPUT TYPE="submit" VALUE="提交" NAME="B1"></P>
</FORM></BODY></HTML>
result.asp,代码如下:
<HTML><BODY>
<%result=Request.Form("R1")
If result="V1" Then WebCounter.Increment("Perfect_Count")
If result="V2" Then WebCounter.Increment("Common_Count")
If result="V3" Then WebCounter.Increment("Bad_Count")
%>
<P>选择“非常满意”的有:<%=WebCounter.Get("Perfect_Count")%>人
<P>选择“比较满意”的有:<%=WebCounter.Get("Common_Count")%>人
<P>选择“不满意”的有:<%=WebCounter.Get("Bad_Count")%>人
</BODY></HTML>
例6-5-1:
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
var MyTextFile=MyFileObject.CreateTextFile("f:\test1.txt",true) '创建文件
MyFileObject.CopyFile "f:\test1.txt","f:\test2.txt" '复制文件
MyFileObject.MoveFile "f:\test1.txt","f:\test3.txt" '移动文件
MyFileObject.DeleteFile "f:\test2.txt" '删除文件
MyFileObject.DeleteFile "d:\test3.txt" '删除文件
%>
例6-5-2:使用File对象中的方法进行文件操作。
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set NewFile=MyFileObject.GetFile("f:\test1.txt") '创建File对象
NewFile.Copy "f:\test1.txt" '复制文件
NewFile.Move "f:\test2.txt" '移动文件
NewFile.Delete '删除文件
%>
例6-5-3:使用FileSystemObject对象中的方法进行文件夹操作。
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
MyFileObject.CreateFolder("f:\test1.txt") '创建文件夹
MyFileObject.MoveFolder "f:\test1","d:\test2" '移动文件夹
MyFileObject.DeleteFolder "d:\test2" '删除文件夹
%>
例6-5-4:使用Folder对象中的方法进行文件夹操作。
<HTML><BODY>
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyFolder=MyFileObject.GetFolder("c:\programfiles") '创建Folder对象
For Each thing in MyFolder.Files '循环显示其中文件
Response.Write("<p>"&thing)
Next
%>
</BODY></HTML>
例6-5-5:
<HTML><BODY>
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
For Each thing in MyFileObject.Drives '对Drives集合进行循环
%>
<BR>驱动器号:<%=thing.DriveLetter%>
<BR>总容量:<%=thing.TotalSize%>
<BR>可用容量:<%=thing.AvailableSpace%>
<HR>
<%NEXT%>
</BODY>
</HTML>
例6-5-6:
<HTML>
<BODY>
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyDrive=MyFileObject.GetDrive("D:") '对Drives集合进行循环
%>
<BR>D盘的总容量:<%= MyDrive.TotalSize%>
<BR>D盘的可用容量:<%= MyDrive.AvailableSpace%>
<HR>
</BODY></HTML>
例6-5-7:读取文本文件的内容。
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyTextFile=MyFileObject.OpenTextFile("f:\test1.txt")
While not MyTextFile.AtEndOfStream '判断是否到了该文件的结尾
Response.Write(MyTextFile.ReadLine+"<br>")
Wend
MyTextFile.Close
%>
例6-5-8:向文本文件中连续写入多行信息
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
Set MyTextFile=MyFileObject.CreateTextFile("f:\test.txt") '创建文件
MyTextFile.WriteLine("向文本文件中写入多行信息!") '在文件中添加字符串
For i=1 To 10
Response.Write(MyTextFile.WriteLine("欢迎光临"))
Next
MyTextFile.Close
%>
例6-5-9:
<%
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'以追加方式打开一个已存在的文件
Set MyTextFile=MyFileObject.OpenTextFile("f:\asp\weblog.log",8,true)
'追加本次用户浏览网页的时间
MyTextFile.WriteLine("欢迎来到本网站!时间:"&now())
'追加客户端的IP地址
MyTextFile.WriteLine(Request.ServerVariables("REMOTE_ADDR"))
MyTextFile.Close
%>
例6-5-10:
<HTML><HEAD><TITLE>梦想家园!</TITLE></HEAD>
<BODY><BR>
<H2 align="center">欢迎光临“梦想家园”</H2>
<HR>
<%Dim Visitors
FileAdress=Server.mappath("count.txt") '获取计数器文件的绝对地址
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'以下是读取文件计数器的数据并加1
Set CounterFile=MyFileObject.OpenTextFile(FileAdress) '得到TextStream对象
Visitors=CounterFile.readline '读取计数器文件中的访问次数
CounterFile.close
Visitors=Visitors+1 '将访问次数加1
'以下是将新数据重新写入文件计数器
Set CouterFile=MyFileObject.CreateTextFile(FileAdress)
CouterFile.WriteLine(Visitors)
CouterFile.close
Set MyFileObject=nothing
%>
<CENTER>您是本站第<%=Visitors%>位访问者!</CENTER>
</BODY></HTML>
例6-5-11,
<HTML>
<HEAD>
<TITLE>梦想家园!</TITLE>
</HEAD>
<BODY>
<BR>
<H2 align="center">欢迎光临“梦想家园”</H2>
<HR>
<%
Dim Visitors
FileAdress=Server.mappath("count.txt")'获取计数器文件的绝对地址
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
'以下是读取文件计数器的数据并加1
Set CounterFile=MyFileObject.OpenTextFile(FileAdress) '得到TextStream对象
Visitors=CounterFile.readline '读取计数器文件中的访问次数
CounterFile.close
If IsEmpty(Session("Complete")) Then '加1前先判断Session变量
Visitors=Visitors+1 '将访问次数加1
End If
Session("Complete")=True '若已经将计数器加1,则创建Session变量
'以下是将新数据重新写入文件计数器
Set CouterFile=MyFileObject.CreateTextFile(FileAdress)
CouterFile.WriteLine(Visitors)
CouterFile.close
Set MyFileObject=nothing
%>
<CENTER>
<%
CountLen=len(Visitors) '取得计数器数据的位数
For i=1 To Countlen
'用相应的数字图像显示计数器数据
Response.Write"<IMG SRC=" & Mid(Visitors,i,1) & ".gif></IMG>"
Next
%>
</CENTER>
</BODY>
</HTML>
例6-6-1,
JmailEx.asp,代码如下:
<HTML>
<HEAD><TITLE>使用JMail组件发送邮件</TITLE></HEAD>
<BODY>
<H2 align="center">请填写邮件信息</H2><HR>
<FORM method="POST" action="sendEmail.asp">
<CENTER>
<TABLE border=1 width="80%">
<TR><TD>收件人地址:</TD>
<TD><input type="text" name="To" size="30%"></TD>
</TR>
<TR><TD><P>发件人地址:</TD>
<TD><input type="text" name="From" size="30%"></TD>
</TR>
<TR><TD>主题:</TD>
<TD><input type="text" name="Subject" size="30%"></TD>
</TR>
<TR><TD>内容:</TD>
<TD><textarea rows="8" name="Body" cols="35"></textarea></TD>
</TR>
<TR><TD colspan="2" align="center">
<INPUT type="submit" value="发送" name="Send">
<INPUT type="reset" value="重写" name="Cancel"></TD>
</TR>
</TABLE>
</CENTER>
</FORM>
</BODY>
</HTML>
sendEmail.asp,代码如下:
<HTML>
<BODY>
<CENTER>
<%
If Request.Form("From")<>"" And Request.Form("To")<>"" And Request,_
Form("Subject")<>"" Then
Set Message=Server.CreateObject( "JMail.Message")
Message.Logging=true '启用日志记录
Message.From=Request.Form("From")
Message.Subject=Request.Form("Subject")
Message.Body=Request.Form("Body")
Message.AddRecipient Request.Form("To")
If Not Message.Send("lijie:123456@smtp.163.com") Then
Response.Write("<PRE>" & Message.log & "</PRE>") '若发送不成功则显示日志
Else
Response.Write("邮件发送成功!")
End If
Else
Response.Write("必须填写收件人、发件人和邮件主题!")
End If
%>
</CENTER>
</BODY>
</HTML>
例6-6-2:
ASPUploadEx.asp,代码如下:
<HTML>
<HEAD>
<TITLE>使用ASPUpload组件上传文件</TITLE>
</HEAD>
<BODY>
<H2 align="center">文件上传</H2>
<HR>
<FORM method="POST" action="upload.asp" enctype="multipart/form-data">
<CENTER>
<TABLE border=1 width="80%">
<TR>
<TD>选择文件:</TD>
<TD><input type="file" name="upFile" size="30%"></TD>
</TR>
<TR>
<TD><P>文件名称:</TD>
<TD><input type="text" name="fileName" size="30%"></TD>
</TR>
<TR>
<TD>文件说明:</TD>
<TD><input type="text" name="fileIntro" size="30%"></TD>
</TR>
<TR>
<TD colspan="2" align="center">
<INPUT type="submit" value="上传">
</TR>
</TABLE>
</CENTER>
</FORM>
</BODY>
</HTML>
upload.asp,代码如下:
<HTML>
<BODY>
<%Dim Upload
Set Upload=Server.CreateObject("Persits.UPload.1")
Upload.SetMaxSize 1024*1024,False '限制文件大小不超过1M
Upload.OverwriteFiles=True '允许覆盖
Upload.Save "F:\web\upload" '上传到指定的位置
Response.Write "文件上传成功!<BR>"
Response.Write "上传文件为:" & Upload.Files("upFile").Path & "<BR>"
Response.Write "文件大小为:" & Upload.Files("upFile").Size & "<BR>"
Response.Write "文件名称为:" & Upload.Form("fileName").Value & "<BR>"
Response.Write "文件说明为:" & Upload.Form("fileIntro").Value & "<BR>"
%>
</BODY>
</HTML>
第8章 ADO对象例8-2-1:
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=stusql;UID=sa;PWD="
%>
例8-2-2:
<%
Set conn = Server.CreateObject("ADODB.Connection")
'设置ODBC连接字符串。Driver表示数据库的ODBC驱动程序,DBQ表示数据库的位置。
'UID表示用户帐号,PWD表示密码
strodbc = "Driver={Microsoft Access Driver (*.mdb)}; DBQ="&_
Server.MapPath("\") & "\student.mdb;UID=;PWD=; "
conn.Open strodbc
%>
例8-2-3:
<!--#include virtual ="adovbs.inc" -->
<% '打开连接数据库
Set conn = Server.CreateObject("ADODB.Connection")
strProvider="Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'建立查询,下面表示选择t_student表的所有项。
Command = "select * from t_student"
'执行查询。rs是RecordSet对象。
Set rs = conn.Execute(command,,adcmdtext)
Set conn = nothing
%>
例8-2-4:
register.htm,代码如下:
<HTML><BODY>
欢迎注册,请输入用户名和密码
<FORM method="POST" action="register.asp">
<P>姓名:<INPUT type="text" name="Name" size="10"></P>
<P>密码:<INPUT type="Password" name="Password" size="10"></P>
<P><INPUT type="submit" value="确定">
<INPUT type="reset" value="取消"></P>
</FORM>
</BODY></HTML>
例8-2-5:
register.asp,代码如下:
<!--#include file = "adovbs.inc" -->
<%'取得用户名和密码,并删除用户名和密码中的首尾空格
username=Trim(Request.Form("Name"))
userpass=Trim(Request.Form("Password"))
'如果用户名或密码为空,重定向到注册页面
If username="" or userpass="" then
Response.Redirect "register.htm"
End If
'打开数据库,建立连接
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'检查用户是否已经存在
Commandtext = "Select * From t_user where d_user='" & username & "'"
conn.Execute commandtext,recordsAffected,adcmdtext
If recordsAffected=0 then
'向表t_user添加数据
Commandtext= "Insert into t_user(d_user,d_pass) VALUES('" & username & "','" & userpass & "')"
conn.Execute commandtext,recordsAffected,adcmdtext
Response.Write "注册成功!<BR>"
'修改注册信息
Commandtext="Update t_user Set d_pass='001' Where d_user='"&username&"'"
conn.Execute commandtext,recordsAffected,adcmdtext
Response.Write "已成功修改了" & recordsAffected & "条记录!<BR>"
'删除注册信息
Commandtext = "Delete t_user Where d_user='" & username & "'"
conn.Execute commandtext,recordsAffected,adcmdtext
Response.Write "已成功删除了" & recordsAffected & "条记录!"
Else
Response.Write "用户已存在,请选择新的用户名重新注册!"
Response.Write "<A href='register.htm'>注册页面</A><BR>"
End If
Set conn = nothing %>
例8-2-6:
<!--#include file = "adovbs.inc" -->
<% username=Trim(Request.Form("Name"))
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'开始一个新的事物
conn.BeginTrans
Commandtext = "Delete t_user Where d_user='" & username & "'"
conn.Execute commandtext,recordsAffected,adcmdtext
'判断是否有错误发生
if conn.Errors.count=0 then
conn.CommitTrans
Response.Write "已成功执行!"
Else
conn.RollbackTrans
Response.Write "发生错误,取消操作!"
End If
Set conn = nothing
%>
例8-2-7:
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=stusql;UID=sa;PWD="
Response.Write "<TABLE border=3>"
Response.Write "<TR><TD>相关属性</TD>"
Response.Write "<TD>属性值</TD>"
Response.Write "</TR>"
For each item In conn.properties
Response.Write "<TR><TD>"&item.name&"</TD>"
Response.Write "<TD>"&item.value&"</TD></TR>"
Next
Response.Write "</TABLE>"
Set conn = nothing
%>
例8-3-1:
<!--#include virtual ="adovbs.inc" -->
<%
'建立数据库连接
Set conn=Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
'创建Command对象
Set comm=Server.CreateObject("ADODB.Command")
'设置Command对象属性
comm.ActiveConnection=conn
comm.commandtext="select * from t_student"
comm.commandtype=adCmdText
comm.prepared=True
'执行指定查询
set rs=comm.Execute()
'显示查询结果代码(略)
……
Set conn = nothing
%>
例8-3-2:
commselect.htm,代码如下:
<HTML>
<BODY>
<DIV Align="center">
<H4>请输入查询条件</H4>
<HR>
<FORM method="POST" action="commselect.asp">
<P>学号:<INPUT type="text" name="number" size="10">
性别:<SELECT name="Sex" size="1">
<option value="男">男</option>
<option value="女">女</option>
</SELECT>
</P>
<P><INPUT type="submit" value="查询">
<INPUT type="reset" value="取消">
</P>
</FORM>
</DIV>
</BODY>
</HTML>
commselect.asp,代码如下:
<% '接收用户输入的查询条件
sex=Request.Form("sex")
number=Request.Form("number")
If sex="" or number="" then
Response.Redirect "commselect.htm"
End If
'创建数据库连接
Set conn=Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
'创建Command对象并设置其属性
Set comm=Server.CreateObject("ADODB.Command")
comm.ActiveConnection=conn
'SQL语句中用?表示将接收parameter对象传递的参数
comm.commandtext="select * from t_student where sex=? and s_number=?"
comm.commandtype=adCmdText
comm.prepared=True
'建立Parameters集合
Set params=comm.parameters
'建立第一个Parameter对象,并用变量sex为其赋值
Set sex = comm.CreateParameter("sex",adVarChar,adParamInput,2,sex)
'将新建的Parameter对象追加到Parameters集合中
Params.Append sex
'建立第二个Parameter对象
Set num=comm.CreateParameter("s_num",adVarChar,adParamInput,10,number)
Params.Append num
set rs=comm.Execute()
'显示查询结果代码
If not rs.EOF then
While not rs.EOF
Response.Write "<TABLE border=3>"
Response.Write "查询结果为:<BR><TR>"
For i=0 To rs.fields.count-1
Response.Write "<TD>"&rs(i).value&"</TD>"
Next
rs.MoveNext
Response.Write "<TR>"
Wend
Else
Response.Write "对不起,没有找到匹配的结果!<BR>"
End If
Set conn=nothing
%>
例8-4-1:
<!--#include file ="adovbs.inc" -->
<%
'利用Server对象的CreateObject方法建立RecordSet对象。
Set rs=Server.CreateObject("ADODB.RecordSet")
'设置参数
rs.ActiveConnection = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
rs.Source="t_student"
rs.CursorType = adOpenStatic
rs.LockType = adLockreadOnly
'连接数据库
rs.Open
'断开连接
Set rs = nothing
%>
例8-4-2:
<%
strUserName = Request.Form("Username")
Set objDBConn = Server.Createobject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
objdbconn.Open strProvider
' 取得记录集
strSQL = "SELECT * FROM t_user WHERE UserName='" & strUserName & "'"
Set RS = objDBConn.Execute(strSQL)
' 检查用户
If RS.EOF and RS.BOF Then
' 无此用户
Response.Write "可以使用该用户名"
Else
Response.Write "该用户名已存在,请选择新的用户名"
End If
Set objRS = nothing
Set objDBConn = nothing
%>
例8-4-3:
<!--#include file ="adovbs.inc" -->
<%
'利用Server对象的CreateObject方法建立RecordSet对象。
Set rs=Server.CreateObject("ADODB.RecordSet")
'设置参数
Source="t_student"
Activeconn = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
'连接数据库
rs.Open Source,Activeconn,adOpenStatic,adLockreadOnly,adCmdTable
'断开连接
Set rs = nothing
%>
例8-4-4:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
Command = "select * from t_student order by s_number"
Set rs = conn.Execute(command,recordsAffected,adcmd)
'建立一个表,把查询得到的结果保存到表中。
Response.Write "<TABLE border=3>"
'表头
Response.Write "<TR>"
For i=0 To rs.fields.count-1
Response.Write "<TD>"&Ucase(rs(i).Name)&"</TD>"
Next
Response.Write "</TR>"
'把查询结果填入表中。
While not rs.EOF
Response.Write "<TR>"
For i=0 To rs.fields.count-1
Response.Write "<TD>"&rs(i).value&"</TD>"
Next
rs.MoveNext
Response.Write "<TR>"
Wend
Response.Write "</TABLE>"
Set conn = nothing
%>
例8-4-5:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from t_student where s_name='青山'"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
'修改记录值
If not rs.eof then
rs("POLITY").value = "党员"
'执行更新操作
rs.Update
End If
rs.close
Set conn = nothing
%>
例8-4-6:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=; "
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
'创建一个空记录集,以节约系统资源
sql="select * from t_student where 1=2"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
rs.addnew
rs("s_number")="9812111"
rs("s_name")="test"
'执行更新操作
rs.Update
rs.close
Set conn = nothing
%>
例8-4-7:
<!--#include file = adovbs.inc -->
<%
Set rs=Server.CreateObject("Adodb.recordSet")
Con="Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu;User ID=sa; Password=; "
sql="select * from t_student order by s_number"
rs.Open sql,Con,AdOpenStatic,adlockreadonly,adcmdtext
'判断记录集是否为空
If rs.EOF then
Response.Write "记录集为空!"
Response.End
End If
'设置RecordSet对象的每一页数据记录条数的大小
rs.PageSize=4
'设置当前页
NowPage=Request.QueryString("page")
If NowPage="" then NowPage=1
NowPage=CLng(NowPage)
If NowPage<1 then NowPage=1
If NowPage>rs.PageCount then NowPage=rs.PageCount
rs.AbsolutePage=NowPage
%>
<HTML>
<HEAD><TITLE>分页显示示例</TITLE></HEAD>
<BODY><DIV Align="Center">
<H3>学生基本情况一览表</H3>
当前页:<%=NowPage%> (共 <%=rs.PageCount%> 页)
<HR>
<TABLE border="1">
<%'输出表头%>
<TR align=center valign=middle height=23>
<TD ><B>学号</B></TD>
<TD ><B>姓名</B></TD>
<TD ><B>性别</B></TD>
<TD ><B>出生日期</B></TD>
<TD ><B>政治面貌</B></TD>
</TR>
<%'输出当前页面记录
For j=0 To rs.PageSize-1
Response.Write "<TR align=center valign=middle>"
For i=0 To rs.fields.count-1
Response.Write "<TD>" &rs.fields(i).value & "</TD>"
Next
Response.Write "</TR>"
rs.MoveNext
If rs.Eof Then Exit For
Next
Response.Write "</TABLE><BR>"
'输出第一页、上一页、下一页和最后一页对应的超链
If NowPage<>1 then
Response.Write "<A href='pageshow.asp?page=1'>第一页 </A>"
Response.Write " "
Response.Write "<A href='pageshow.asp?page=" & NowPage-1 & "'>上一页</A>"
End If
If NowPage<>rs.PageCount then
Response.Write " "
Response.Write "<A href='pageshow.asp?page=" & NowPage+1 & "'>下一页</A>"
Response.Write " "
Response.Write "<A href='pageshow.asp?page=" & rs.PageCount &"'>最后一页</A>"
End If
%>
<Form action="pageshow.asp" method="GET">
<%'循环输出页码
Response.Write "请选择数据页:"
For i=1 To rs.PageCount
If i=NowPage then
Response.Write i&" "
Else
Response.Write "<A href='pageshow.asp?page=" & i &"'>" & i & "</A> "
End If
Next
%>
请输入页号:<INPUT type="text" name="page" size=4>
<INPUT type="submit" value="转到">
<INPUT type="reset" value="取消">
</P>
</Form>
</DIV>
</BODY>
</HTML>
例8-4-8:
<%'输出当前页面记录
For j=0 To rs.PageSize-1
Response.Write "<TR align=center valign=middle>"
For i=0 To rs.fields.count-1
Response.Write "<TD>" &rs.fields(i).value & "</TD>"
Next
Response.Write "<TD><A href='edit.asp?which=" &rs("s_number") &"'>修改</A></TD>"
Response.Write "</TR>"
rs.MoveNext
If rs.Eof Then Exit For
Next
……
%>
edit.asp,代码如下:
<!--#include file = adovbs.inc -->
<%'取得被编辑记录的标识
number=Request("which")
Set conn = Server.CreateObject("ADODB.Connection")
sTRProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open sTRProvider
Set rs=Server.createObject("ADODB.RecordSet")
rs.Open "select * from t_student where s_number =" &_
number,conn,adOpenkeySet,adlockOptimistic
%>
<HTML><BODY><DIV align ="center">
<H4 >学生(学号为:<%=rs("s_number")%>)的数据</H4><HR>
<Form method="POST" action="eupdate.asp">
<%'输出记录内容%>
<TABLE border=0 >
<TR><TD height="35"> 学 号:</TD>
<TD><input type="text" name="no" size="10" value=
"<% =Server.HTMLEncode(rs("s_number")) %>"></TD>
</TR>
<TR><TD height="35"> 姓 名:</TD>
<TD><input type="text" name="username" size="10" value=
"<% =Server.HTMLEncode(rs("s_name")) %>"></TD>
</TR>
<TR><TD height="35"> 性 别:</TD>
<TD><select size="1" name="sex">
<% If rs("sex")="男" then %>
<option value="男" selected>男</option>
<option valuve="女" >女</option>
<% Else %>
<option value="男" >男</option>
<option valuve="女" selected>女</option>
<%End If %>
</select></TD>
</TR>
<TR><TD height="35">出生日期:</TD>
<TD><input type="text" name="birthday" size="10" value=
"<% =Server.HTMLEncode(rs("birthday")) %>"></TD>
</TR>
<TR><TD height="35">政治面貌:</TD>
<TD><input type="text" name="polity" size="10" value=
"<% =Server.HTMLEncode(rs("polity")) %>"></TD>
</TR>
</TABLE>
<INPUT type=submit value="确定" name=B1 >
<INPUT type=reSet value="重填" name=B2 >
</div>
<input type="hidden" name="number" size="20" value=
"<% =Server.HTMLEncode(rs("s_number")) %>">
</Form>
<%Set conn=nothing%>
</BODY>
</HTML>
eupdate.asp,代码如下:
<!--#include file = adovbs.inc -->
<%
'取得记录标识
no=Trim(request.form("number"))
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from t_student where s_number='" & no & "'"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
'修改记录值
If not rs.eof then
rs("s_number")=Trim(request.form("no"))
rs("s_name")=Trim(request.form("username"))
rs("sex")=Trim(request.form("sex"))
rs("birthday")=Trim(request.form("birthday"))
rs("polity")=Trim(request.form("polity"))
'执行更新操作
rs.Update
End If
%>
例8-4-9:
<%'输出表头%>
<TR align=center valign=middle height=23>
<TD ><B>学号</B></TD>
<TD ><B>姓名</B></TD>
<TD ><B>性别</B></TD>
<TD ><B>出生日期</B></TD>
<TD ><B>政治面貌</B></TD>
<TD ><B>删除</B></TD>
</TR>
<Form action="delup.asp" name="delform" method="POST">
<%'输出当前页面记录
For j=0 To rs.PageSize-1
Response.Write "<TR align=center valign=middle>"
For i=0 To rs.fields.count-1
Response.Write "<TD>" &rs.fields(i).value & "</TD>"
Next
Response.Write "<TD><INPUT type='checkbox' name='delwhich' value='" & rs("s_number") &"'> </TD>"
Response.Write "</TR>"
rs.MoveNext
If rs.Eof Then Exit For
Next
Response.Write "</TABLE><BR>"
%>
<INPUT type="submit" name="del" value="删除">
<INPUT type="reset" value="取消">
</P></Form>
<%
'输出第一页、上一页、下一页和最后一页对应的超链
……
delup.asp,代码如下:
<!--#include virtual ="adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
strProvider = "Provider=SQLOLEDB.1;Data Source=zjf;Initial Catalog=stu; User ID=sa; Password=;"
conn.Open strProvider
Set rs = Server.CreateObject("ADODB.RecordSet")
sql="select * from t_student"
rs.Open sql,conn,adOpenKeySet,adLockOptimistic,adcmdtext
'获取用户选择的记录数
decount=Request.Form("delwhich").count
If decount=0 then
Response.Write "对不起,没有找到删除的记录!"
Response.End
End If
While not rs.EOF
For i=1 to decount
'查找与用户选择相同的记录
If Request.Form("delwhich")(i)=Cstr(rs("s_number")) then
rs.delete
'执行更新操作
rs.Update
Exit For
End If
Next
rs.MoveNext
Wend
rs.close
Set conn = nothing
%>
第9章 设计实例——网上论坛
9.1 系统概述
9.1.3 系统的公共模块
1.层叠样式表文件(css.css )
BODY {
font-size,9pt;
background-color,#D1EDD8;
font-family,"Tahoma";
}
TABLE {
font-size,9pt;
font-family,"Tahoma";
border-collapse,collapse;
word-break:break-all;
}
A:visited {
font-size,9pt;
color,#000000;
font-family,"Tahoma";
text-decoration,none;
}
A:active {
font-size,9pt;
color,#FFFFFF;
font-family,"Tahoma";
text-decoration,none;
}
A:link {
font-size,9pt;
color,#000000;
font-family,"Tahoma";
text-decoration,none;
}
A:hover {
font-size,9pt;
color,#FFFFFF;
font-family,"Tahoma";
text-decoration,none;
}
.tr {
font-family,"Tahoma";
font-size,9pt;
background-color,#40A458;
}
.td {
font-family,"Tahoma";
font-size,9pt;
background-color,#A4DBB1;
}
input {
font-family,"Tahoma";
border-top-width,1px;
border-left-width,1px;
font-size,12px;
border-bottom-width,1px;
border-right-width,1px;
}
textarea {
font-family,"Tahoma";
border-top-width,1px;
border-left-width,1px;
font-size,12px;
border-bottom-width,1px;
border-right-width,1px;
color,#000000;
}
2.访问权限判断文件(access.asp)
<%
If Session("userName")="" then
Response.Redirect("default.htm")
Response.End()
End If
%>
3.公用函数文件(conn_fun.asp)
<%Dim rs,conn
'过程:连接数据库
sub getConn()
Dim strProvider
Set rs=Server.CreateObject("ADODB.Recordset")
Set conn=Server.CreateObject("ADODB.Connection")
strProvider="Provider=sqloledb;Data Source=.;Initial Catalog=bbs;User ID=sa;Password=sql;"
conn.open strProvider
If conn.Errors.Count>0 Then
Response.Write("数据库连接失败!")
Set rs=nothing
Set conn=nothing
Response.End
End If
End sub
'函数:用户级别文字转换
Function getUserLevel(userLevel)
If userLevel=0 Then
getUserLevel="论坛管理员"
ElseIf userLevel=1 Then
getUserLevel="普通用户"
End If
End Function
'函数:用户是否被锁定文字转换
Function getUserIsLocked(isLocked)
If isLocked=False Then
getUserIsLocked="未锁定"
ElseIf isLocked=True Then
getUserIsLocked="已锁定"
End If
End Function
'函数:文本中特殊字符入库转换
Function checkContent(content)
content=replace(content,"'","`")
content=replace(content,chr(34),"``")
content=replace(content,chr(13),"<br>")
content=replace(content,chr(32)," ")
checkContent=Content
End Function
'函数:文本中特殊字符出库转换
Function showContent(content)
content=replace(content,"`","'")
content=replace(content,"``",chr(34) )
content=replace(content,"<br>",chr(13))
content=replace(content," ",chr(32))
showContent=content
End Function
'函数:将心情图片路径转换为编号入库
Function moodPathToId(moodPath)
idLen=len(moodPath)-16-4
moodPathToId=cint(mid(moodPath,17,idLen))
End Function
'函数:将心情图片编号转换为路径出库
Function moodIdToPath(moodId)
moodIdToPath="images/mood/face"&moodId&".gif"
End Function
%>
4.页面的顶部显示文件(top.asp)
<!--#Include File="access.asp"-->
<!--#Include File="conn_fun.asp"-->
<HTMl><HEAD>
<LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="JavaScript">
function confirmQuit(){
flag=confirm("确定要退出论坛吗?");
return flag;
}
</SCRIPT></HEAD>
<BODY><BR>
<H2 align="center"><FONT color="#286637" face="华文彩云">论 坛</FONT></H2>
<%getConn()
userSql="select * from userInfo where userName='"&Session("userName")&"'"
Set userRs=conn.execute(userSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25" width="198" align="left" bordercolor="#40A458">
<FONT color="#FFFFFF">欢迎 <%=Session("userName")%> 光临!
<%If userRs("isLocked") Then%>您已被锁定!<%End If%></FONT></TD>
<TD height="25" align="center" bordercolor="#40A458">
<A href="mainPage.asp" color="#FFFFFF">论坛首页</A>┊┊
<A href="editUserInfo.asp">个人资料</A>┊┊
<A href="search.asp">论坛搜索</A>┊┊
<%If userRs("userLevel")=0 Then%>
<A href="admin.asp">论坛管理</A>┊┊
<%End If%>
<A href="logout.asp" onClick="return confirmQuit();">退出登录</A>
</TD>
<TD height="25" width="198" align="center" bordercolor="#3D65B8">
</TD></TR></TABLE>
<%Set userRs=nothing
Set conn=nothing%>
<BR></BODY></HTMl>
5.页面的底部显示文件(bottom.htm)
<HTMl><BODY><BR><BR><TABLE width="760" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#cccccc" bgcolor="#FFFFFF">
<TR><TD height="25" colspan="6" class="tr" align="center">简易论坛! 作者:LJ 版权所有!</TD>
</TR></TABLE></BODY></HTMl>
9.2 用户登录、注册、个人资料功能模块
9.2.1 用户登录模块
1、登录页面(default.htm)
<HTMl><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>登录论坛</TITLE><LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="VBScript">
Sub Submit1_OnClick
userName=Trim(form1.userName.value)
passWord=form1.passWord.value
If userName=Empty Then
MsgBox "帐号不能为空!"
Document.form1.userName.Focus()
Exit Sub
ElseIf InStr(userName,"'")<>0 Then
MsgBox "请正确填写帐号!"
Document.form1.userName.Focus()
Exit Sub
ElseIf passWord=Empty Then
MsgBox "请填写密码!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf InStr(passWord,"'")<>0 Then
MsgBox "请正确填写密码!"
Document.form1.passWord.Focus()
Exit Sub
End If
form1.Submit
End Sub
</SCRIPT></HEAD>
<BODY>
<H2 align="center"><FONT color="#286637" face="华文彩云">论 坛</FONT> </H2>
<DIV align="center">
<TABLE width="400" border="1" align="center" bordercolor="#000000">
<FORM name="form1" method="post" action="login.asp">
<TR><TD height="25" colspan="6" align="center" class="tr">用户登录 </TD></TR>
<TR><TD height="100" colspan="6" align="center" class="td">
<TABLE width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
<TR><TD width="41%" align="right">帐 号:</TD>
<TD width="59%"><input name="userName" type="text"></TD>
</TR>
<TR><TD align="right">密 码:</TD>
<TD><input name="passWord" type="password"></TD>
</TR></TABLE></TD></TR>
<TR><TD height="25" colspan="6" align="center" class="tr">
<input type="button" name="Submit1" value="提 交" onclick="return checkLogin(this.form)">
<input type="reset" name="Submit2" value="重 置"></TD></TR>
<TR><TD height="25" colspan="6" align="center" class="tr"><A href="register.htm">新用户注册</A></TD>
</TR></FORM></TABLE></DIV></BODY></HTMl>
2、登录提交页面(login.asp)
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
userName_form=Trim(Request.Form("userName"))
passWord_form=Trim(Request.Form("passWord"))
'------判断登陆的帐号与密码的合法性
getConn()
userSql="select * from userInfo where userName='"&userName_form&"'"
rs.open userSql,conn,1,1
If rs.eof Then
errmsg="不存在此用户,请注册!"
returnURL="register.htm"
ElseIf rs("passWord")<>passWord_form Then
errmsg="密码不正确,请重新登陆!"
returnURL="default.htm"
ElseIf Instr(Application("online_name"),userName_form) Then
errmsg="该帐号正在被使用!登录失败!"
returnURL="default.htm"
Else
Session("userName")=userName_form
conn.Execute("update userInfo set visitCount="&rs("visitCount")+1&" where userName='"&userName_form&"'")
errmsg="登录成功!进入论坛!"
returnURL="mainPage.asp"
End If
Set rs=Nothing
Set conn=Nothing
%>
<HTMl><HEAD><META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
</HEAD><BODY><%=errmsg%></BODY></HTMl>
9.2.2 用户注册模块
1、注册页面(register.htm)
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>用户注册</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="VBScript">
Sub Submit1_OnClick
atInEMail=InStr(form1.EMail.value,"@")
pointInEMail=InStr(form1.EMail.value,".")
If form1.userName.value=Empty Then
MsgBox "请填写用户名!"
Document.form1.userName.Focus()
Exit Sub
ElseIf InStr(Trim(form1.userName.value),"'")<>0 Then
MsgBox "请正确填写用户名!"
Document.form1.userName.Focus()
Exit Sub
ElseIf form1.passWord.value=Empty Then
MsgBox "请填写密码!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf InStr(form1.passWord.value,"'")<>0 Then
MsgBox "请正确填写密码!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf Len(form1.passWord.value)<6 Then
MsgBox "密码至少6位!"
Document.form1.passWord.Focus()
Exit Sub
ElseIf form1.conPassWord.value=Empty Then
MsgBox "请填写确认密码!"
Document.form1.conPassWord.Focus()
Exit Sub
ElseIf form1.passWord.value<>form1.conPassWord.value Then
MsgBox "两次填写密码不一致!"
form1.passWord.value=Empty
form1.conPassWord.value=Empty
Document.form1.passWord.Focus()
Exit Sub
ElseIf form1.EMail.value=Empty Then
MsgBox "请填写E-Mail!"
Document.form1.EMail.Focus()
Exit Sub
ElseIf atInEMail<2 Or pointInEMail<4 Or atInEMail>pointInEMail Then
MsgBox "请填写正确的E-Mail!"
Document.form1.EMail.Focus()
Exit Sub
ElseIf Len(form1.userSign.value)>200 Then
MsgBox "签名最多不能超过200字!"
Document.form1.userSign.Focus()
Exit Sub
End If
form1.Submit
End Sub
</SCRIPT>
</HEAD>
<BODY>
<H2 align="center"><FONT color="#286637" face="华文彩云">论 坛</FONT> </H2>
<TABLE width="600" border="1" align="center" bordercolor="#000000">
<FORM name="form1" method="post" action="register.asp">
<TR>
<TD height="25" colspan="3" align="center" class="tr">新用户注册</TD>
</TR>
<TR>
<TD width="150" height="25" align="right" class="td">用户名:</TD>
<TD width="468" class="td"><input name="userName" type="text" id="userName"></TD>
<TD width="87" align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">密 码:</TD>
<TD class="td"><input name="passWord" type="password" id="passWord">*密码至少为6位*</TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">确认密码:</TD>
<TD class="td"><input name="conPassWord" type="password" id="conPassWord"></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">性 别:</TD>
<TD class="td"><input name="userSex" type="radio" value="男" checked>男
<input name="userSex" type="radio" value="女">女
</TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD height="25" align="right" class="td">E-Mail:</TD>
<TD class="td"><input name="EMail" type="text" id="EMail"></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR>
<TD align="right" class="td">签 名:</TD>
<TD class="td"><textarea name="userSign" cols="50" rows="6" id="userSign"></textarea></TD>
<TD align="center" class="td">最多200字</TD>
</TR>
<TR>
<TD height="25" colspan="3" align="center" class="tr">
<input type="button" name="Submit1" value="提 交">
<input type="reset" name="Submit2" value="重 置"></TD>
</TR>
</FORM>
</TABLE>
</BODY>
</HTMl>
2、注册提交页面(register.asp)
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
userName_form=Trim(Request.Form("userName"))
passWord_form=Trim(Request.Form("passWord"))
userSex_form=Request.Form("userSex")
EMail_form=Trim(Request.Form("Email"))
userSign_form=Trim(Request.Form("userSign"))
regDate=Date()&" "&Time()
getConn()
'------判断填写的用户名是否已经被注册
Set checkUserNameRs=conn.Execute("select * from userInfo where userName=' "&userName_form&"'")
'若用户名已存在,则返回前一页,保持表单中填写的内容不变
If not checkUserNameRs.Eof Then
errmsg="用户名已存在,请重新填写!"
returnURL="javascript:history.back();"
'若用户名未被使用,则插入用户表中一条记录,注册成功
Else
registerSql="insert into userInfo(userName,passWord,userSex,userEMail,userSign,userLevel,visitCount,topicCount,regDate,islocked) values ('"&userName_form&"','"&passWord_form&"','"&userSex_form&"','"&EMail_form&"','"&userSign_form&"',1,1,0,'"®Date&"',0)"
conn.execute(registerSql)
Session("userName")=userName_form
errmsg="注册成功!自动登录论坛!"
returnURL="mainPage.asp"
End If
Set checkUserNameRs=Nothing
Set conn=Nothing
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<%=errmsg%>
</BODY>
</HTMl>
9.2.3 个人资料模块
1、个人资料修改页面(editUserInfo.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>个人资料</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkRegister(form){
var atInEMail=form.EMail.value.indexOf("@");
var pointInEMail=form.EMail.value.indexOf(".");
if(form.passWord.value==""){
alert("请填写密码!");
form.passWord.focus();
return false;
}
else if(form.passWord.value.length<6){
alert("密码至少6位!");
form.passWord.focus();
return false;
}
else if(form.conPassWord.value==""){
alert("请填写确认密码!");
form.conPassWord.focus();
return false;
}
else if(form.passWord.value!=form.conPassWord.value){
alert("两次填写密码不一致!");
form.passWord.value="";
form.conPassWord.value="";
form.passWord.focus();
return false;
}
else if(form.EMail.value==""){
alert("请填写E-Mail!");
form.EMail.focus();
return false;
}
else if(atInEMail<1||pointInEMail<3||atInEMail>pointInEMail){
alert("请填写正确的E-Mail!");
form.EMail.focus();
return false;
}
else if(form.userSign.value.length>200){
alert("签名最多不能超过200字!");
form.userSige.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD> 当前位置:<A href="editUserInfo.asp">个人资料</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE><BR>
<%
getConn()
userSql="select * from userInfo where userName='"&Session("userName")&"'"
rs.open userSql,conn,1,1
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form1" method="post" action="postEditUserInfo.asp">
<TR class="tr">
<TD height="25" colspan="3" align="center">修改个人资料</TD>
</TR>
<TR class="td">
<TD width="150" height="25" align="right">用户名:</TD>
<TD width="468"> <%=Session("userName")%></TD>
<TD align="center">必填</TD>
</TR>
<TR class="td">
<TD align="right">密 码:</TD>
<TD> <input name="passWord" type="password" value=<%=rs("passWord")%>>*密码至少为6位*</TD>
<TD align="center">必填</TD>
</TR>
<TR class="td">
<TD align="right">确认密码:</TD>
<TD> <input name="conPassWord" type="password" value=<%=rs("passWord")%>></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR class="td">
<TD align="right">性 别:</TD>
<%If rs("userSex")="男" Then%>
<TD> <input name="userSex" type="radio" value="男" checked>男<input name="userSex" type="radio" value="女">女</TD>
<%Else%>
<TD> <input name="userSex" type="radio" value="男">男<input name="userSex" type="radio" value="女" checked>女</TD>
<%End If%>
<TD height="20" align="center" class="td">必填</TD>
</TR>
<TR class="td">
<TD align="right">E-Mail:</TD>
<TD> <input name="EMail" type="text" value=<%=rs("userEMail")%>></TD>
<TD align="center" class="td">必填</TD>
</TR>
<TR class="td">
<TD align="right">签 名:</TD>
<TD> <textarea name="userSign" cols="50" rows="6"><%=showContent(rs("userSign"))%></textarea></TD>
<TD align="center">最多200字</TD>
</TR>
<TR class="tr">
<TD height="20" colspan="3" align="center">
<input type="submit" name="Submit1" value="提 交" onClick="return checkRegister(this.form)">
<input type="reset" name="Submit2" value="重 置"></TD>
</TR>
</FORM>
</TABLE>
<%Set rs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
2、个人资料修改提交页面(postEditUserInfo.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
passWord_form=Trim(Request.Form("passWord"))
userSex_form=Trim(Request.Form("userSex"))
EMail_form=Trim(Request.Form("Email"))
userSign_form=checkContent(Trim(Request.Form("userSign")))
getConn()
modifySql="update userInfo set passWord='"&passWord_form&"',userSex='"&userSex_form&"',userEMail='"&EMail_form&"',userSign='"&userSign_form&"' where userName='"&Session("userName")&"'"
conn.Execute(ModifySql)
Set conn=Nothing
errmsg="个人资料修改成功!"
returnURL="editUserInfo.asp"
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<%=errmsg%>
</BODY>
</HTMl>
9.3 栏目信息功能模块
9.3.1 论坛栏目浏览
1、论坛栏目浏览页面(mainPage.asp)
<!--#Include file="inc/top.asp"-->
<HTMl><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE><LINK rel="stylesheet" href="inc/css.css">
</HEAD>
<BODY><TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR><TD height="25" class="tr">
<TABLE width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
<TR><TD>当前位置:<A href=mainPage.asp>首页</A></TD></TR>
</TABLE></TD>
</TR></TABLE><BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25" colspan="2">|栏目介绍|</TD>
<TD align="center">主题数目</TD>
<TD align="center">回复数目</TD></TR>
<%'------查询栏目表中的所有栏目信息并显示
getConn()
boardSql="select * from board"
Set boardRs=conn.Execute(boardSql)
While Not boardRs.Eof
%>
<TR><TD width="60" height="53" align="center" class="td">
<img src="images/board.gif"></TD>
<TD width="475" class="td"><TABLE width="100%" border="0">
<TR><TD colspan="2">
<A href="topicList.asp?boardId=<%=boardRs("boardId")%>">
[<%=boardRs("boardName")%>]</A></TD></TR>
<TR><TD width="6%" align="right" valign="top">
<FONT face=Wingdings>v</FONT></TD>
<TD><%=boardRs("boardDescription")%></TD></TR></TABLE></TD>
<%'查询属于本栏目的帖子数目
Set rs1=Server.CreateObject("ADODB.Recordset")
topicSql="select * from topic where boardId='"&boardRs("boardId")&"'"
rs1.open topicSql,conn,1,1
%>
<TD width="65" align="center" class="td"><%=rs1.recordcount%></TD>
<%'查询属于本栏目的所有回帖数目
Set rs2=Server.CreateObject("ADODB.Recordset")
replySql="select * from reply,topic where topic.topicId=reply.topicId and topic.boardId='"&boardRs("boardId")&"'"
rs2.open replySql,conn,1,1
%>
<TD width="65" align="center" class="td"><%=rs2.recordcount%></TD>
</TR>
<%boardRs.Movenext
Wend
Set boardRs=nothing
Set rs1=nothing
Set rs2=nothing
%>
</TABLE>
<%'------判断登录用户是否为管理员,若是,才可显示下面的论坛管理板块
userSql="select * from userInfo where userName='"&Session("userName")&"'"
Set userRs=conn.execute(userSql)
If userRs("userLevel")=0 Then
%><BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr"><TD height="25" colspan="2">|论坛管理|</TD></TR>
<TR class="td"><TD width="67" height="57" align="center"><img src="images/board.gif"></TD>
<TD><TABLE width="100%" border="0">
<TR><TD colspan="2"><A href="admin.asp"> [论坛管理]</A></TD>
</TR>
<TR><TD width="4%" align="right" valign="top">
<FONT face=Wingdings>v</FONT></TD>
<TD>管理员进行操作</TD></TR></TABLE></TD></TR></TABLE>
<%End If
Set userRs=nothing
Set conn=nothing %>
</BODY></HTMl>
<!--#Include File="inc/bottom.htm"-->
9.3.2 栏目帖子浏览
1、栏目帖子浏览页面(topicList.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
getConn()
boardSql="select * from board where boardId='"&boardId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0" align="center">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A></TD>
<%userSql="select * from userInfo where userName='"&Session("userName")&"'"
Set userRs=conn.Execute(userSql)
If userRs("isLocked")=0 Then%>
<TD align="right"><img src="Images/topic/newTopic.gif"><A href="newTopic.asp?boardId=<%=boardId_string%>">发表新帖</A></TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr" align="center">
<TD height="25">主题(点击心情图片打开新窗口)</TD>
<TD>作者</TD>
<TD>回复/人气</TD>
<TD>发表时间</TD>
</TR>
<%Set userRs=Nothing
Set boardRs=Nothing
'------查询所有属于本栏目的帖子并按时间倒序显示
topicSql="select * from topic where boardId='"&boardId_string&"' order by addTime desc"
rs.open topicSql,conn,1,1
'------分页显示
'判断记录集是否为空
If rs.EOF then
Response.Write "记录集为空!"
Response.End
End If
'设置RecordSet对象的每一页数据记录的条数
rs.PageSize=4
'设置当前页
NowPage=Request.QueryString("page")
If NowPage="" then NowPage=1
NowPage=CLng(NowPage)
If NowPage<1 then NowPage=1
If NowPage>rs.PageCount then NowPage=rs.PageCount
rs.AbsolutePage=NowPage
'输出当前页面记录
For j=0 To rs.PageSize-1
%>
<TR class="td">
<TD height="25">
<A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=rs("topicId")%>" target="_blank"> <img src="<%=moodIdToPath(rs("topicMood"))%>" border=0></A>
<A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=rs("topicId")%>"><%=rs("topicTitle")%></A>
</TD>
<TD align="center"><A href="userInfo.asp?userName=<%=rs("userName")%>" target="_blank"><%=rs("userName")%></A></TD>
<TD align="center"><%=rs("replyCount")%>/<%=rs("readCount")%></TD>
<TD align="center"><%=rs("addTime")%></TD>
</TR>
<%
rs.MoveNext
If rs.Eof Then Exit For
Next
%>
<TR class="tr" align="center">
<Form action="" method="GET">
<TD height="25" colspan="4">
当前页:<%=NowPage%> (共 <%=rs.PageCount%> 页)
<%
'输出第一页、上一页、下一页和最后一页对应的超链
If NowPage<>1 then %>
|<A href="?page=1&boardId=<%=boardId_string%>">首页</A>|
|<A href="?page=<%=NowPage-1%>&boardId=<%=boardId_string%>">上一页</A>|
<%End If
If NowPage<>rs.PageCount then %>
|<A href="?page=<%=NowPage+1%>&boardId=<%=boardId_string%>">下一页</A>|
|<A href="?page=<%=rs.PageCount%>&boardId=<%=boardId_string%>">尾页</A>|
<%End If%>
请输入页号:<INPUT type="text" name="page" size=4>
<INPUT type="submit" value="转到">
<INPUT type="reset" value="取消">
<INPUT type="hidden" name="boardId" value=<%=boardId_string%>>
</TD>
</FORM>
</TR>
</TABLE>
<%Set rs=nothing
Set conn=nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
9.3.3 帖子与回复内容浏览
1、帖子与回复内容浏览页面(topicShow.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function confirmDelete(what){
if (what==1)
flag=confirm("确定要删除本条留言以及所有回复吗?");
else flag=confirm("确定要删除本条回复吗?");
return flag;
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
getConn()
topicSql="select * from board,topic,userInfo where topic.topicId="&topicId_string&" and topic.userName=userInfo.userName and topic.boardId=board.boardId"
Set topicRs=conn.Execute(topicSql)
conn.Execute("update topic set readCount="&topicRs("readCount")+1&" where topicId='"&topicId_string&"'")
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp> 栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=topicRs("boardName")%></A>>><A href="?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>"><%=topicRs("topicTitle")%></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25" colspan="2">
<TABLE width="98%" border="0">
<TR>
<TD> 标题:<img src="<%=moodIdToPath(topicRs("topicMood"))%>"><%=topicRs("topicTitle")%></TD>
<%Set userRs=conn.Execute("select * from userInfo where userName='"&Session("userName")&"'")
If userRs("isLocked")=false Then%>
<TD align="right"><img src="Images/topic/newReply.gIf"><A href="newReply.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>">我要回复</A></TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
<TR class="td">
<TD width="207">
<TABLE width="100%" border="0">
<TR>
<TD> <%=topicRs("userName")%><BR>
等级:<%=getUserLevel(topicRs("userLevel"))%><BR>
访问:<%=topicRs("visitCount")%><BR>
文章:<%=topicRs("topicCount")%><BR>
注册:<%=topicRs("regDate")%><BR>
E-Mail:<%=topicRs("userEMail")%>
</TD>
</TR>
</TABLE>
</TD>
<TD width="547">
<TABLE width="100%" border="0">
<TR>
<TD height="50">
<TABLE width="96%" border="0">
<TR>
<TD><img src="<%=moodIdToPath(topicRs("topicMood"))%>"><b><%=topicRs("topicContent")%></b></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<TABLE width="96%" border="0">
<TR>
<TD> ------------------------------------------------------------------------<BR>
签名:<%=topicRs("userSign")%>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD height="25"> 发表于:<%=topicRs("addTime")%></TD>
<TD>
<TABLE width="96%" border="0" align="center">
<TR>
<TD align="right">
<%'------判断权限,当前用户可以编辑自己的帖子,管理员可以删除所有帖子
If topicRs("userName")=Session("userName") _
And topicRs("isLocked")=False Then%>
|<A href="editTopic.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>">编辑</A>|
<%End If%>
<%If userRs("userLevel")=0 And userRs("isLocked")=False Then%>
|<A href="deleteTopic.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>" onClick="return confirmDelete(1);">删除</A>|
</TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
<%Set topicRs=Nothing
replySql="select * from reply,userInfo where reply.userName=userInfo.userName and reply.topicId="&topicId_string&" order by reply.replyTime desc"
Set replyRs=conn.execute(replySql)
While Not replyRs.Eof
%>
<TR class="td">
<TD width="207" height="25">
<TABLE width="100%" border="0">
<TR>
<TD> <%=replyRs("userName")%><BR>
等级:<%=getUserLevel(replyRs("userLevel"))%><BR>
访问:<%=replyRs("visitCount")%><BR>
文章:<%=replyRs("topicCount")%><BR>
注册:<%=replyRs("regDate")%><BR>
E-Mail:<%=replyRs("userEMail")%>
</TD>
</TR>
</TABLE>
</TD>
<TD width="547" height="25">
<TABLE width="100%" border="0">
<TR><TD > </TD></TR>
<TR>
<TD>
<TABLE width="96%" border="0">
<TR>
<TD><img src="<%=moodIdToPath(replyRs("replyMood"))%>"><%=replyRs("replyContent")%></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<TABLE width="96%" border="0">
<TR>
<TD> ------------------------------------------------------------------------<BR>
签名:<%=replyRs("userSign")%>
</TD>
</TR>
<TR><TD> </TD></TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD height="25"> 回复于:<%=replyRs("replyTime")%></TD>
<TD><TABLE width="98%" border="0">
<TR>
<TD align="right">
<%'------判断权限,当前用户可以编辑自己的回复,管理员可以删除所有回复
If replyRs("userName")=Session("userName") And replyRs("isLocked")=False Then%>
|<A href="editReply.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>&replyId=<%=replyRs("replyId")%>">编辑</A>|
<%End If%>
<%If userRs("userLevel")=0 And userRs("isLocked")=False Then%>
|<A href="deleteReply.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>&replyId=<%=replyRs("replyId")%>" onClick="return confirmDelete(0);">删除</A>|
</TD>
<%End If%>
</TR>
</TABLE>
</TD>
</TR>
<%
replyRs.Movenext
Wend
Set replyRs=Nothing
Set userRs=Nothing
Set conn=Nothing
%>
</TABLE>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
9.4 帖子管理功能模块
9.4.1 发表新帖
1、发表新帖页面(newTopic.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var title=form.title.value;
var content=form.content.value;
if(title==""){
alert("请填写帖子主题!");
form.title.focus();
return false;
}
else if(content==""){
alert("请填写帖子内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
getConn()
dim boardSql
boardSql="select * from board where boardId='"&boardId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>>发表新帖</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<%Set boardRs=Nothing%>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postTopic.asp" target="">
<TR class="td">
<TD height="25" colspan="2" align="center" class="tr">发表新帖</TD>
</TR>
<TR class="td">
<TD height="25" align="right">标题:</TD>
<TD><input name="title" type="text" size="70" value=""></TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD>
<%'------使用文件组件,找到“images\mood\”下的所有表情图片,循环显示
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%>
<input type="radio" name="mood" value=<%=moodPathToId(filePath)%>>
<img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR>
<%End If
Next%>
</TD>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD align="right">帖子内容:</TD>
<TD><textarea name="content" cols="70" rows="12"></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2">
<input type="submit" name="Submit1" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit2" value="重 置">
<input name="boardId" type="hidden" value=<%=boardId_string%>>
<input name="act" type="hidden" value="new">
</TD>
</TR>
</FORM>
</TABLE>
<%Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
2、新帖提交页面(postTopic.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
title_form=trim(Request.form("title"))
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
addTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新帖,则插入topic表一条新记录
If act_form="new" Then
topicSql="insert into topic values("&boardId_form&",'"&title_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&addTime&"',0,0)"
Set topicCountRs=conn.Execute("select * from userInfo where userName= '"&Session("userName")&"'")
conn.Execute("update userInfo set topicCount= "&topicCountRs("topicCount") +1&" where userName='"&Session("userName")&"'")
'------若是编辑帖子,则更新topic表的相关记录
Else
topicSql="update topic set topicTitle= '"&title_form&"',topicMood= '"&mood_form&"',topicContent='"&content_form&"',addTime='"&addTime&"' where topicId="&topicId_form&""
End If
conn.Execute(topicSql)
Set topicCountRs=Nothing
Set conn=Nothing
returnURL="topicList.asp?boardId="& boardId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.4.2 编辑帖子编辑帖子页面(editTopic.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var title=form.title.value;
var content=form.content.value;
if(title==""){
alert("请填写帖子主题!");
form.title.focus();
return false;
}
else if(content==""){
alert("请填写帖子内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
getConn()
boardSql="select * from board,topic where board.boardId='"&boardId_string&"' and board.boardId=topic.boardId and topic.topicId='"&topicId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="td">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>><A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>"><%=left(boardRs("topicTitle"),5)%>......</A>>>编辑帖子</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<%
Set boardRs=Nothing
topicSql="select * from topic where boardId='"&boardId_string&"' and topicId='"&topicId_string&"'"
Set topicRs=conn.Execute(topicSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postTopic.asp" target="">
<TR class="tr">
<TD height="25" colspan="2" align="center">编辑帖子</TD>
</TR>
<TR class="td">
<TD height="25" align="right">标题:</TD>
<TD><input name="title" type="text" size="70" value="<%=topicRs("topicTitle")%>"></TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD>
<%'------使用文件组件,找到“images\mood\”下的所有表情图片,循环显示
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%>
<%If topicRs("topicMood")=moodPathToId(filePath) then%>
<input type="radio" name="mood" value=<%=moodPathToId(filePath)%> checked>
<%else%>
<input type="radio" name="mood" value=<%=moodPathToId(filePath)%>>
<%End If%>
<img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR><%End If%>
<%next%>
</TD>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD align="right">帖子内容:</TD>
<TD><textarea name="content" cols="70" rows="12" id="content"><%=showContent(topicRs("topicContent"))%></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2">
<input type="submit" name="Submit6" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit62" value="重 置">
<input name="boardId" type="hidden" value="<%=boardId_string%>">
<input name="topicId" type="hidden" value="<%=topicId_string%>">
<input name="act" type="hidden" value="edit">
</TD>
</TR>
</FORM>
</TABLE>
<%Set topicRs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
编辑帖子提交页面(postTopic.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
title_form=trim(Request.form("title"))
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
addTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新帖,则插入topic表一条新记录
If act_form="new" Then
topicSql="insert into topic values("&boardId_form&",'"&title_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&addTime&"',0,0)"
Set topicCountRs=conn.Execute("select * from userInfo where userName='"&Session("userName")&"'")
conn.Execute("update userInfo set topicCount="&topicCountRs("topicCount")+1&" where userName='"&Session("userName")&"'")
'------若是编辑帖子,则更新topic表的相关记录
Else
topicSql="update topic set topicTitle='"&title_form&"',topicMood='"&mood_form&"',topicContent='"&content_form&"',addTime='"&addTime&"' where topicId="&topicId_form&""
End If
conn.Execute(topicSql)
Set topicCountRs=Nothing
Set conn=Nothing
returnURL="topicList.asp?boardId="& boardId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.4.3 删除帖子删除帖子页面(deleteTopic.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
'------删除主题及其所有回复
getConn()
deleteReplySql="delete from reply where topicId='"&topicId_string&"'"
conn.Execute(deleteReplySql)
deleteTopicSql="delete from topic where boardId='"&boardId_string&"' and topicId='"&topicId_string&"'"
conn.Execute(deleteTopicSql)
Set conn=Nothing
returnURL="topicList.asp?boardId="& boardId_string
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.5 回复管理功能模块
9.5.1 发表回复发表回复页面(newReply.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var content=form.content.value;
if(content==""){
alert("请填写回复内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
getConn()
dim boardSql
boardSql="select * from board where boardId='"&boardId_string&"'"
Set boardRs=conn.execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>>发表回复</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<%Set boardRs=Nothing%>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postReply.asp" target="">
<TR class="tr">
<TD height="25" colspan="2" align="center">发表回复</TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD width="2%"></TD>
<TD> <%Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%> <input type="radio" name="mood" value="<%=moodPathToId(filePath)%>">
<img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR>
<%End If
Next%>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD height="25" align="right">回复内容:</TD>
<TD><textarea name="content" cols="70" rows="12" ></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2"> <input type="submit" name="Submit6" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit62" value="重 置">
<input name="boardId" type="hidden" value="<%=boardId_string%>">
<input name="topicId" type="hidden" value="<%=topicId_string%>">
<input name="act" type="hidden" value="new"> </TD>
</TR>
</FORM>
</TABLE>
<%Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
发表回复提交页面(postReply.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
replyId_form=Request.Form("replyId")
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
replyTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新回复,则插入reply表一条新记录
If act_form="new" then
replySql="insert into reply(topicId,replyMood,replyContent,userName,replyTime) values('"&topicId_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&replyTime&"')"
Set replyCountRs=conn.Execute("select * from topic where topicId='"&topicId_form&"'")
conn.execute("update topic set replyCount="&replyCountRs("replyCount")+1&" where topicId='"&topicId_form&"'")
'------若是编辑回复,则更新reply表的相关记录
Else
replySql="update reply set replyMood='"&mood_form&"',replyContent='"&content_form&"',replyTime='"&replyTime&"' where replyId='"&replyId_form&"'"
End If
conn.Execute(replySql)
Set replyCountRs=Nothing
Set conn=Nothing
returnURL="topicshow.asp?boardId="& boardId_form & "&topicId="& topicId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.5.2 编辑回复编辑回复页面(editReply.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<script language="JavaScript">
function checkForm(form){
var content=form.content.value;
if(content==""){
alert("请填写回复内容!");
form.content.focus();
return false;
}
}
</script>
</HEAD>
<BODY>
<%'------显示导航条
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
replyId_string=Request.QueryString("replyId")
getConn()
boardSql="select * from board,topic where board.boardId='"&boardId_string&"' and board.boardId=topic.boardId and topic.topicId='"&topicId_string&"'"
Set boardRs=conn.Execute(boardSql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="td">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href=mainPage.asp>栏目</A>>><A href="topicList.asp?boardId=<%=boardId_string%>"><%=boardRs("boardName")%></A>>><A href="topicShow.asp?boardId=<%=boardId_string%>&topicId=<%=topicId_string%>"><%=left(boardRs("topicTitle"),5)%>......</A>>>编辑回复</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<%
Set boardRs=Nothing
replySql="select * from reply where topicId='"&topicId_string&"' and replyId='"&replyId_string&"'"
Set replyRs=conn.Execute(replySql)
%>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form" method="post" action="postreply.asp" target="">
<TR class="tr">
<TD height="25" colspan="2" align="center">编辑回复</TD>
</TR>
<TR class="td">
<TD height="25" align="right">表情:</TD>
<TD><TABLE width="100%" border="0">
<TR>
<TD>
<%'------使用文件组件,找到“images\mood\”下的所有表情图片,循环显示
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set myFolder=fs.GetFolder(Server.MapPath("images\mood\"))
i=0
For Each thing In myFolder.Files
fileName=fs.GetFileName(thing)
filePath="images/mood/"&fileName
i=i+1
%>
<%If replyRs("replyMood")=moodPathToId(filePath) then%>
<input type="radio" name="mood" value="<%=moodPathToId(filePath)%>" checked>
<%else%>
<input type="radio" name="mood" value="<%=moodPathToId(filePath)%>">
<%End If%> <img src="<%=filePath%>" width="19" height="19">
<%If i Mod 9=0 then%><BR><%End If%>
<%next%>
</TD>
</TR>
</TABLE></TD>
</TR>
<TR class="td">
<TD align="right">回复内容:</TD>
<TD><textarea name="content" cols="70" rows="12"><%=showContent(replyRs("replyContent"))%></textarea></TD>
</TR>
<TR align="center" class="tr">
<TD height="25" colspan="2"> <input type="submit" name="Submit6" value="提 交" onclick="return checkForm(this.form);">
<input type="reset" name="Submit62" value="重 置">
<input name="boardId" type="hidden" value="<%=boardId_string%>">
<input name="topicId" type="hidden" value="<%=topicId_string%>">
<input name="replyId" type="hidden" value="<%=replyId_string%>">
<input name="act" type="hidden" value="edit"> </TD>
</TR>
</FORM>
</TABLE>
<%Set replyRs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
编辑回复提交页面(postReply.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_form=Request.Form("boardId")
topicId_form=Request.Form("topicId")
replyId_form=Request.Form("replyId")
mood_form=Request.Form("mood")
'若未选表情,则默认为第一个
If mood_form="" then
mood_form=1
End If
content_form=checkContent(trim(Request.form("content")))
replyTime=Date()&" "&Time()
act_form=Request.Form("act")
getConn()
'------若是发表新回复,则插入reply表一条新记录
If act_form="new" then
replySql="insert into reply(topicId,replyMood,replyContent,userName,replyTime) values('"&topicId_form&"','"&mood_form&"','"&content_form&"','"&session("userName")&"','"&replyTime&"')"
Set replyCountRs=conn.Execute("select * from topic where topicId='"&topicId_form&"'")
conn.execute("update topic set replyCount="&replyCountRs("replyCount")+1&" where topicId='"&topicId_form&"'")
'------若是编辑回复,则更新reply表的相关记录
Else
replySql="update reply set replyMood='"&mood_form&"',replyContent='"&content_form&"',replyTime='"&replyTime&"' where replyId='"&replyId_form&"'"
End If
conn.Execute(replySql)
Set replyCountRs=Nothing
Set conn=Nothing
returnURL="topicshow.asp?boardId="& boardId_form & "&topicId="& topicId_form
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
</HTMl>
9.5.3 删除回复删除回复页面(deleteReply.asp)
<!--#Include File="inc/access.asp"-->
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取传递过来的表单数据
boardId_string=Request.QueryString("boardId")
topicId_string=Request.QueryString("topicId")
replyId_string=Request.QueryString("replyId")
'------删除回复
getConn()
deleteReplySql="delete from reply where replyId='"&replyId_string&"'"
conn.Execute(deleteReplySql)
Set conn=Nothing
returnURL="topicShow.asp?boardId="& boardId_string&"&topicId="&topicId_string
%>
<HTMl>
<HEAD>
<META http-equiv="Refresh" content='0;URL=<%=returnURL%>'>
</HEAD>
</HTMl>
9.6 论坛搜索功能模块论坛搜索页面(search.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
</HEAD>
<BODY>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href="search.asp">论坛搜索</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<FORM name="form1" action="" method="GET">
<TR class="tr">
<%
getConn()
%>
<TD height="25" width="150" align="right">查找帖子:</TD>
<TD>
<select name="board">
<option value="" selected>-所有栏目-</option>
<%
Set conditionRs=conn.Execute("select * from board")
While Not conditionRs.Eof
%>
<option value=<%=conditionRs("boardId")%>><%=conditionRs("boardName")%></option>
<%
conditionRs.Movenext()
Wend
Set conditionRs=Nothing
%>
</select>
<select name="topicType">
<option value="title" selected>标题</option>
<option value="content">内容</option>
<option value="userName">作者</option>
</select>
<input name="keyWord" type="text"size="40">
<input type="submit" name="submit1" value="搜索"></TD>
</TR>
</FORM>
</TABLE>
<%'------获取本页提交的表单数据
ifSubmit_string=Request.QueryString("submit1")
boardId_string=Request.QueryString("board")
topicType_string=Request.QueryString("topicType")
keyWord_string=trim(Request.QueryString("keyWord"))
'------若点击了搜索,则获取查询条件,查询记录并显示;否则不显示任何记录
'------初始化SQL语句
topicSql="select * from board,topic where topic.boardId=board.boardId"
If ifSubmit_string<>"" Then
If boardId_string<>"" Then
topicSql=topicSql & " and topic.boardId=" & boardId_string
End If
If topicType_string="title" Then
topicSql=topicSql & " and topic.topicTitle like'%"&keyWord_string&"%'"
ElseIf topicType_string="content" Then
topicSql=topicSql & " and topic.topicContent like'%"&keyWord_string&"%'"
Else
topicSql=topicSql & " and topic.userName like'%"&keyWord_string&"%'"
End If
topicSql=topicSql & " order by topic.boardId,topic.addTime desc"
%>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr" align="center">
<TD height="25">栏目</TD>
<TD>主题(点击心情打开新窗口)</TD>
<TD>作者</TD>
<TD>回复/人气</TD>
<TD>发表时间</TD>
<%
rs.open topicSql,conn,1,1
'------分页显示
'判断记录集是否为空
If rs.EOF then
Response.Write "记录集为空!"
Response.End
End If
'设置RecordSet对象的每一页数据记录的条数
rs.PageSize=4
'设置当前页
NowPage=Request.QueryString("page")
If NowPage="" then NowPage=1
NowPage=CLng(NowPage)
If NowPage<1 then NowPage=1
If NowPage>rs.PageCount then NowPage=rs.PageCount
rs.AbsolutePage=NowPage
'输出当前页面记录
For j=0 To rs.PageSize-1
%>
<TR class="td">
<TD height="25" align="center"><%=rs("boardName")%></TD>
<TD align="left">
<A href="topicShow.asp?boardId=<%=rs("boardId")%>&topicId=<%=rs("topicId")%>" target="_blank"> <img src="<%=moodIdToPath(rs("topicMood"))%>" border=0></A>
<A href="topicShow.asp?boardId=<%=rs("boardId")%>&topicId=<%=rs("topicId")%>"><%=rs("topicTitle")%></A>
</TD>
<TD align="center"><A href="userInfo.asp?userName=<%=rs("userName")%>" target="_blank"><%=rs("userName")%></A></TD>
<TD align="center"><%=rs("replyCount")%>/<%=rs("readCount")%></TD>
<TD align="center"><%=rs("addTime")%></TD>
</TR>
<%
rs.MoveNext
If rs.Eof Then Exit For
Next
%>
<TR class="tr" align="center">
<Form name="form1" action="" method="GET">
<TD height="25" colspan="5">
当前页:<%=NowPage%> (共 <%=rs.PageCount%> 页)
<%
'输出第一页、上一页、下一页和最后一页对应的超链
If NowPage<>1 then %>
|<A href="?page=1&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">首页</A>|
|<A href="?page=<%=NowPage-1%>&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">上一页</A>|
<%End If
If NowPage<>rs.PageCount then %>
|<A href="?page=<%=NowPage+1%>&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">下一页</A>|
|<A href="?page=<%=rs.PageCount%>&submit1=<%=ifSubmit_string%>&board=<%=boardId_string%>&topicType=<%=topicType_string%>&keyWord=<%=keyWord_string%>">尾页</A>|
<%End If%>
请输入页号:<INPUT type="text" name="page" size=4>
<INPUT type="submit" value="转到">
<INPUT type="reset" value="取消">
<INPUT type="hidden" name="submit1" value=<%=ifSubmit_string%>>
<INPUT type="hidden" name="board" value=<%=boardId_string%>>
<INPUT type="hidden" name="topicType" value=<%=topicType_string%>>
<INPUT type="hidden" name="keyWord" value=<%=keyWord_string%>>
</TD>
</FORM>
</TR>
</TABLE>
<%End If
Set rs=Nothing
Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
9.7 论坛管理功能模块
1、论坛管理页面(admin.asp)
<!--#Include file="inc/top.asp"-->
<HTMl>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>欢迎加入论坛</TITLE>
<LINK rel="stylesheet" href="inc/css.css">
<SCRIPT language="JavaScript">
function checkForm(form){
var name=form.boardName.value;
var description=form.boardDescription.value;
if(name==""){
alert("请填写栏目名称!");
form.boardName.focus();
return false;
}
else if(description==""){
alert("请填写栏目说明!");
form.boardDescription.focus();
return false;
}
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR class="tr">
<TD height="25">
<TABLE width="98%" border="0">
<TR>
<TD>当前位置:<A href="admin.asp">论坛管理</A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR align="center" class="tr">
<TD height="25" colspan="5">栏目管理</TD>
</TR>
<%
getConn()
boardSql="select * from board"
Set boardRs=conn.Execute(boardSql)
%>
<TR align="center" class="td">
<TD height="25" width="5%">编辑<BR>栏目</TD>
<TD><TABLE width="100%" border="1" bordercolor="#000000">
<TR align="center" class="td">
<TD height="25">栏目名称</TD>
<TD>栏目说明</TD>
<TD>操作</TD>
</TR>
<%While Not boardRs.Eof%>
<FORM method="post" action="postAdmin.asp">
<TR align="center" class="td">
<TD height="25" width="160"><input name="boardName" type="text" value="<%=boardRs("boardName")%>" size="20"> </TD>
<TD width="450"><input name="boardDescription" type="text" value="<%=boardRs("boardDescription")%>" size="60"></TD>
<TD><input type="hidden" name="act" value="modifyBoard">
<input type="hidden" name="boardId" value=<%=boardRs("boardId")%>>
<input type="submit" name="Submit1" value="修改" onclick="return checkForm(this.form);">
<A href="postAdmin.asp?boardId=<%=boardRs("boardId")%>&act=deleteBoard" onClick="return confirm('一旦删除数据不能再恢复,确定要删除吗?');">删除</A></TD>
</TR>
</FORM>
<%boardRs.Movenext()
Wend
Set boardRs=Nothing
%>
</TABLE>
</TD>
</TR>
</TABLE>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR align="center" class="td">
<TD height="25" width="5%">添加<BR>栏目</TD>
<FORM method="post" action="postAdmin.asp">
<TD height="25" width="162"><input name="boardName" type="text" size="20"></TD>
<TD width="450"><input name="boardDescription" type="text" size="60"></TD>
<TD><input type="hidden" name="act" value="newBoard">
<input type="submit" name="Submit1" value="添加" onclick="return checkForm(this.form);">
</TD>
</FORM>
</TR>
</TABLE>
<BR>
<TABLE width="760" border="1" align="center" bordercolor="#000000">
<TR align="center" class="tr">
<TD height="25" colspan="5">用户管理
</TR>
<TR align="center" class="td">
<TD height="25">用户名</TD>
<TD>注册时间</TD>
<TD>等级</TD>
<TD>状态</TD>
<TD>操作</TD>
</TR>
<%
userSql="select * from userInfo where userName<>'"&Session("userName")&"'order by regDate"
Set userRs=conn.Execute(userSql)
While Not userRs.Eof
%>
<FORM method="post" action="postAdmin.asp">
<TR align="center" class="td">
<TD height="25"><A href="userInfo.asp?userName=<%=userRs("userName")%>" target="_blank"><%=userRs("userName")%></A></TD>
<TD><%=userRs("regDate")%></TD>
<TD><%=getUserLevel(userRs("userLevel"))%></TD>
<TD><%=getUserIsLocked(userRs("isLocked"))%></TD>
<TD>
<%If userRs("isLocked")=0 Then%>
<A href="postAdmin.asp?userId=<%=userRs("userId")%>&act=lockUser" onclick="return confirm('你确认要锁定<%=userRs("userName")%>吗?');">锁定</A>
<%Else%>
<A href="postAdmin.asp?userId=<%=userRs("userId")%>&act=unlockUser" onclick="return confirm('你确认要解除对<%=userRs("userName")%>的锁定吗?');">解锁</A>
<%End If%>
<A href="postAdmin.asp?userName=<%=userRs("userName")%>&act=deleteUser" onclick="return confirm('你确认要删除<%=userRs("userName")%>及其所有留言和回复吗?');">删除</A>
</TD>
</TR>
</FORM>
<%
userRs.Movenext
Wend
Set userRs=Nothing
%>
</TABLE>
<%Set conn=Nothing%>
</BODY>
</HTMl>
<!--#Include File="inc/bottom.htm"-->
2、论坛管理提交页面(postAdmin.asp)
<!--#Include file="inc/conn_fun.asp"-->
<%'------获取操作项目标识
act=Request("act")
getConn()
'------修改栏目信息
If act="modifyBoard" Then
boardId_form=Request.Form("boardId")
boardName_form=trim(Request.Form("boardName"))
boardDescription_form=trim(Request.Form("boardDescription"))
Set boardNameRs=conn.execute("select * from board where boardName= '"&boardName_form&"' and boardId<>'"&boardId_form&"'")
If Not boardNameRs.Eof Then
errmsg="栏目名称已存在,请更换名称!"
returnURL="JavaScript:History.Back()"
Else
Set modifyBoardRs=conn.execute("update board set boardName= '"&boardName_form&"',boardDescription= '"&boardDescription_form&"' where boardId='"&boardId_form&"'")
Set modifyBoardRs=Nothing
errmsg="栏目信息修改成功!"
returnURL="admin.asp"
End If
Set boardNameRs=Nothing
'------删除栏目
ElseIf act="deleteBoard" Then
board_string=Request.QueryString("boardId")
'删除栏目帖子的所有回复
Set topicIdRs=conn.execute _
("select * from topic where boardId='"&board_string&"'")
While Not topicIdRs.Eof
Set deleteReplyRs=conn.execute ("delete from reply where topicId='" &topicIdRs("topicId")&"'")
topicIdRs.Movenext
Wend
Set deleteReplyRs=Nothing
Set topicIdRs=Nothing
'删除栏目的所有帖子
Set deletetopicRs=conn.execute ("delete from topic where boardId='" &board_string&"'")
Set deleteTopicRs=Nothing
'删除栏目
Set deleteBoardRs=conn.execute ("delete from board where boardId='" &board_string&"'")
Set deleteBoardRs=Nothing
errmsg="栏目删除成功!"
returnURL="admin.asp"
'------添加新栏目
ElseIf act="newBoard" Then
boardName_form=trim(Request.Form("boardName"))
boardDescription_form=trim(Request.Form("boardDescription"))
Set boardNameRs=conn.execute ("select * from board where boardName='" &boardName_form&"'")
If Not boardNameRs.Eof Then
errmsg="栏目名称已存在,请更换名称!"
returnURL="javascript:history.back();"
Else
Set newBoardRs=conn.execute("insert board(boardName,boardDescription) values('"&boardName_form&"','"&boardDescription_form&"')")
Set newBoardRs=Nothing
errmsg="栏目添加成功!"
returnURL="admin.asp"
End If
Set boardNameRs=Nothing
'------锁定用户,使用户不能发帖和回复,只能浏览
ElseIf act="lockUser" Then
userId_string=Request.QueryString("userId")
Set userRs=conn.execute("update userInfo set isLocked=1 where userId= '"&userId_string&"'")
Set userRs=Nothing
errmsg="用户锁定成功!"
returnURL="admin.asp"
'------为已锁定用户解锁
ElseIf act="unlockUser" Then
userId_string=Request.QueryString("userId")
Set userRs=conn.execute ("update userInfo set isLocked=0 where userId='" &userId_string&"'")
Set userRs=Nothing
errmsg="用户解锁成功!"
returnURL="admin.asp"
'------删除用户
ElseIf act="deleteUser" Then
userName_string=Request.QueryString("userName")
'删除用户的所有回复
Set replyRs=conn.execute ("delete from reply where userName='" &userName_string&"'")
Set replyRs=Nothing
'删除用户的所有帖子的回复
Set topicIdRs=conn.execute ("select * from topic where userName='" &userName_string&"'")
While Not topicIdRs.Eof
Set deleteReplyRs=conn.execute ("delete from reply where topicId='" &topicIdRs("topicId")&"'")
topicIdRs.Movenext
Wend
Set deleteReplyRs=Nothing
Set topicIdRs=Nothing
'删除用户的所有帖子
Set deleteTopicRs=conn.execute ("delete from topic where userName='" &userName_string&"'")
Set deleteTopicRs=Nothing
Set deleteUserRs=conn.execute ("delete from userInfo where userName='" &userName_string&"'")
Set deleteUserRs=Nothing
errmsg="用户删除成功!"
returnURL="admin.asp"
End IF
Set conn=Nothing
%>
<HTMl><HEAD>
<META http-equiv="Refresh" content='1;URL=<%=returnURL%>'>
</HEAD><BODY><%=errmsg%></BODY></HTMl>
9.8 退出系统功能模块退出系统页面(logout.asp)
<%
Session.Abandon
Response.Redirect("default.htm")
%>