< % strPW1 = Request.Form("txtPW") if strPW1 < > "" then Response.Cookies("PassWord") = strPW1 end if 'strPW1 < > "" strPW2 = Request.Cookies("PassWord") If strPW2 < > "123456" Then Response.Redirect("secure.htm") End if 'strPW2 < > "123456" %>
一旦管理员的身份验证通过,他们能够通过Admin.asp执行的操作包括:
查看Guests表中的所有记录 编辑或 删除指定的记录 向所有邮件列表中的用户发送邮件 管理页面admin.asp如图4所示。显示Guests表的记录时先从数据库提取这些记录,然后使用一个For Each ... Next结构遍历记录集的字段集合,提取字段名字并设置表格的表头。在这个页面中我们不再显示Guest_ID字段,但每个用户记录的前面都加上了一个“删除”和“编辑”功能的链接。用户名字字段Guest_Name与邮件字段Guest_Email被转换为mailto链接,单击名字可以单独向该用户发送邮件。其它要格式化的字段还包括是否发送邮件(Mail_List)以及用户留言(Guest_Comment)。生成表头的代码为:
' 从数据库选取记录 strSQL_Select = "SELECT Guests.Guest_ID, Guests.Guest_Email, " & _ " Guests.Guest_Name, Guests.Mail_List, " & _ " Guests.Guest_Comment, Guests.Sign_Date " & _ " FROM Guests ORDER BY Guests.Guest_Name; " Set oConn=Server.CreateObject("ADODB.Connection") oConn.Open strDSNPath Set rsGbook = oConn.Execute(strSQL_Select) if rsGbook.BOF = True and rsGbook.EOF = True then ...数据库空提示,略... else rsGbook.MoveFirst %> < table BORDER="0" cellpadding="5" cellspacing="2" align="center"> < tr> < % for each Head in rsGbook.Fields if Head.Name = "Guest_ID" then %> ..."删除"与"编辑"表头,略... < % else %> < td VALIGN="middle" align="center">< font face=Arial size=2> < % select case Head.Name case "Guest_Name" Response.Write "名 字" case "Mail_List" Response.Write "邮件列表" case "Guest_Comment" Response.Write "留 言" end select %> < /font>< HR>< /td> < % end if 'Head.Name = "Guest_ID" next %> < /tr>
为在表格的其余位置显示用户注册记录,我们用两个嵌套的循环遍历所有记录的所有字段,即在一个Do While ...循环里面嵌入一个For Each ... Next 循环。数据的格式化工作放在For Each ... Next循环内。其实现代码类如:
< % Do While Not rsGbook.EOF %> < tr> < % For Each Field in rsGbook.Fields if Field.Name = "Guest_ID" then %> < td VALIGN="middle" ALIGN="center"> ...删除功能的链接,略... < /td> < td VALIGN="middle" ALIGN="center"> ...编辑功能的链接,略... < /td> < % else %> < td VALIGN="middle" align="center"> < % if isNull(Field) then Response.Write " " else if Field.Name = "Guest_Name" then Response.Write ...用户名字的mailto链接,略... elseif Field.Name = "Mail_List" then ...输出"是"或"否",略... elseif Field.Name = "Guest_Comment" then ...输出用户留言,略... end if 'Field.Name end if 'isNull(Field)%> < /td> < % end if 'Field.Name = "Guest_ID" Next rsGbook.MoveNext %> < /tr> < % loop %> < /table>
< % iGuestID = Request.Querystring("ID") if iGuestID < > "" then '从数据库删除由ID标识的记录 strSQL_Delete = "DELETE FROM Guests " & _ " WHERE Guest_ID=" & iGuestID Set oConn = Server.CreateObject("ADODB.Connection") oConn.Open strDSNPath on error resume next oConn.Execute strSQL_Delete oConn.Close Set oConn = Nothing if err.number < > 0 then Response.Redirect("admin.asp?Error_Del=True") else Response.Redirect("admin.asp?Error_Del=False") end if else Response.Redirect("admin.asp") end if 'iGuestID < > "" %>