LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

[點(diǎn)晴永久免費(fèi)OA]5天學(xué)會(huì)ASP

admin
2022年7月4日 12:15 本文熱度 1207
首先,先把ASP說(shuō)一下,asp就是 Active Server Pages的簡(jiǎn)寫(xiě),我們?yōu)榱朔奖憷斫猓桶補(bǔ)sp想成是插入到Html中的一種腳本語(yǔ)言吧,這樣理解起來(lái)方便。

書(shū)上都是以hello world的代碼開(kāi)始的,我們的第一天也這樣開(kāi)始吧!這里我們就開(kāi)始以示例講解了:
<html>
<title>hello world</title>
<body>
<%response.write("hello world")%>
</body>
</html>
保存成test.asp,調(diào)試一下 就能看到hello world  了。

這里先讓大家看看他是怎么樣插入到HTML中的(asp程序是放在"<% %>"中間的),當(dāng)然我們也可以在response.write中輸出HTML標(biāo)記,比如:
<%response.write"<font size=20 color=red>你好</font>"%>
就能看到一個(gè)紅色的20號(hào)的"你好"。

接下來(lái)我們就說(shuō)asp中六大內(nèi)置對(duì)象,分別是:
      Request: 負(fù)責(zé)從用戶(hù)端接受信息
      Response:傳遞信息給用戶(hù)
      Server:  控制asp運(yùn)行環(huán)境
      session: 負(fù)責(zé)存儲(chǔ)個(gè)別用戶(hù)信息
      Application:負(fù)責(zé)存儲(chǔ)數(shù)據(jù)以提供多個(gè)用戶(hù)的使用
      objectcontext:可提供asp程序直接配合MTS進(jìn)行分散事物處理
這其中我們就常用的就是:Request,Response,Server,session,Application,在本文中會(huì)逐一講到,剛才我們用了六大內(nèi)置對(duì)象中的Response的write方法輸出了hello world。下面我們就用一個(gè)例子來(lái)講解request和response的用法:

首先,我們要用到html的form標(biāo)簽來(lái)傳遞值:

一,我們先做個(gè)表單頁(yè)面,用來(lái)輸入數(shù)據(jù),這個(gè)頁(yè)面就叫l(wèi)ogin.asp
<html>
<title>request和response的演示(值的傳遞和輸出)</title>
<body>
     <form name=form1 action=checklogin.asp method=post>
   <center>
   <table with=500>
      <tr>
        <td colspan="2" align="center"><font size=5 color=blue>用戶(hù)登錄入口</font>
        </td>
      </tr>
      <tr>
          <td><font size=2>用戶(hù)名:</font></td>
         <td>
         <input name=username type="text" maxlength="20">
        </td>
      </tr>
      <tr>
        <td><font size=2>密 碼:</font></td>
         <td>
         <input type="password" name=password maxlength="20">
          </td>
      </tr>
       <tr>
         <td colspan="2" align=center>
   <input type="submit" value=登錄> 
   <input type="reset" value=重置>
          </td>
      </tr>
</table>
</center>
</form>
</body>
</html>

二,我們?cè)谧鲆粋€(gè)頁(yè)面來(lái)接受這些數(shù)據(jù),這個(gè)頁(yè)面就叫checklogin.asp
<%
'-------------------為防止出錯(cuò)我們定義username和password----------------
dim username
dim password
'--------用request的form方法來(lái)接受login.asp頁(yè)面?zhèn)鱽?lái)的值,并付值給我們定義的username和password
username=request.form("username")
password=request.form("password")
'---------判斷傳來(lái)的值是不是空值
if username="" then
'用response的write的方法輸出一個(gè)腳本
    response.write"<script>alert('用戶(hù)名不能為空')</script>"
 response.end
end if
if password="" then
 response.write"<script>alert('密碼不能為空')</script>"
 response.end
end if
'----------判斷用戶(hù)名和密碼是不是一樣的,如果一樣的輸出"登陸成功"并把session的標(biāo)志給這個(gè)用戶(hù),否則就輸出"登陸失敗"
if username="admin" and password="admin" then
response.write"登陸成功"
session("loginok")=username
else
response.write"登陸失敗"
'重定向到login.asp
response.redirect"login.asp"
end if 
%>
這里我們用到了request.form的方法(接受數(shù)據(jù)),response的write(輸出數(shù)據(jù))和redirect(重定向),session的會(huì)話(huà),這里我先簡(jiǎn)單的說(shuō)說(shuō)session的作用吧,就拿同學(xué)錄來(lái)說(shuō)吧!這里會(huì)判斷用戶(hù)的級(jí)別和權(quán)限(如:沒(méi)有審核同學(xué),通過(guò)審核的同學(xué),班級(jí)管理員)如果用戶(hù)登陸成功了,session就和server產(chǎn)生了會(huì)話(huà),沒(méi)有審核同學(xué)就付值為session(userlevel)=1,通過(guò)審核的同學(xué)就付值為session(userlevel)=2,班級(jí)管理員就付值為session(userlevel)=3,這樣就有效的區(qū)別的用戶(hù)的權(quán)限和級(jí)別。
類(lèi)似的做用的還有cookies,它也有這樣的作用,不同的是cookise是產(chǎn)生到客戶(hù)端的。
我在這個(gè)例子里主要講解了request和response的用法,其實(shí)他們還有一些屬性和方法,這里我只是講解了常用的,希望大家有空看看這里我沒(méi)有提到的屬性方法,這個(gè)例子我沒(méi)有把用戶(hù)和密碼放到數(shù)據(jù)庫(kù)中是為了方便大家的理解,下一節(jié)我們將講解怎么樣連接數(shù)據(jù)庫(kù)和實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的。


-----------------------------------------------------------------------------------------------------------------------
第二天

今天我們主要講解ASP中常用的SQL語(yǔ)句,和怎樣在頁(yè)面中執(zhí)行SQl 語(yǔ)句。其實(shí)常用的語(yǔ)句無(wú)外乎數(shù)據(jù)的“增,刪,改”好象所有的數(shù)據(jù)處理程序中都有這幾種操作,下面我們先寫(xiě)sql語(yǔ)句(如果您需要了解更多的請(qǐng)看SQl語(yǔ)句經(jīng)典或看看SQL相關(guān)的書(shū)籍)。

-----(一)--查詢(xún)---------
1,首先我們要做第一個(gè)<查詢(xún)>,這是SQl中最基本的語(yǔ)句了:
select * from [表名] where [條件] order by [條件] desc 
其中,desc降序,默認(rèn)是asc升序,例子:select * from news where news_id='"&news_id&"' order by news_id desc
意思就是查詢(xún)news表中的"*"代表所有數(shù)據(jù),條件是news_id等于 news_id然后按照news_id 的降序排列,在查詢(xún)里還能用到group by分組排序等等,這里就不系說(shuō)了。
2,count()函數(shù)用來(lái)計(jì)算記錄的行數(shù)。
例:select count(*) from news
計(jì)算數(shù)據(jù)庫(kù)中有多少行
3,sum()函數(shù)用來(lái)計(jì)算表達(dá)試中的項(xiàng)目和。
例:select age,sum(number) from person where age>20 group by age
6,top關(guān)鍵字,限制返回記錄的行數(shù)

-----(二)--插入---------
insert 語(yǔ)句
格式如下:
insert into <表名>[<字段1>,<字段2>……] values[常量1,…………]
例:insert into news(id,news) values(1,'新聞')

---(三)--更新-------
update語(yǔ)句
格式:
update<表名> set <列名>=<表達(dá)式>,<列名1>=<表達(dá)式1>……
where <條件>
例:update news set content=最新消息 where news_id='"&news_id&"' 

-----(四)---刪除----------
delete語(yǔ)句
格式:delete from <表名> where 條件
例:delete from news where news_id>20
好了sql語(yǔ)句我們就說(shuō)這么多吧,下面我們開(kāi)始創(chuàng)建個(gè)數(shù)據(jù)庫(kù),這里我們就用SQL數(shù)據(jù)庫(kù)吧,打開(kāi)" 查詢(xún)分析器":
'創(chuàng)建個(gè)數(shù)據(jù)庫(kù)叫news
create database news
use news
create table admin
(
id int primany key,
admin_name varchar(20) not null,
admin_pwd  varchar(20) not null,
)
'然后我們插入一條數(shù)據(jù)
insert into admin values(1,'admin','admin')
'然后我們用select * from 查詢(xún)一下
select * from admin
就會(huì)看到我們的數(shù)據(jù)了。
好了,我們打開(kāi)昨天做的登陸嚴(yán)整的代碼,這里我們回顧一下吧,做天我們用了兩個(gè)頁(yè)來(lái)實(shí)現(xiàn)的會(huì)員登陸的驗(yàn)證,這兩個(gè)頁(yè)分別是(登陸頁(yè)login.asp)和(驗(yàn)證頁(yè)checklogin.asp),現(xiàn)在我們沒(méi)有做數(shù)據(jù)庫(kù)驗(yàn)證,(登陸頁(yè)login.asp)頁(yè)我們不用改動(dòng),下面我們只需改動(dòng)(驗(yàn)證頁(yè)checklogin.asp),就行了,這里我們要做個(gè)連接數(shù)據(jù)庫(kù)的頁(yè)面文件,名字就叫conn.asp吧,代碼如下:
<%
dim conn,dbuid,dbpwd,dBName,dbip
 dbuid="sa"   '數(shù)據(jù)庫(kù)登陸名
 dbpwd="123456"   '數(shù)據(jù)庫(kù)密碼
 dBName="news"   '數(shù)據(jù)庫(kù)名稱(chēng)
 dbip="(local)"   '可為IP '數(shù)據(jù)庫(kù)所在地址,如果是本地?cái)?shù)據(jù)庫(kù)則為:(local)
'---------------------------------------------------------------------------
set conn=Server.createObject("adodb.Connection")
Conn.Open  "PROVIDER=SQLOLEDB.1;Data Source="&dbip&";Initial Catalog="&dBName&";Persist Security Info=True;User 
ID="&dbuid&";Password="&dbpwd&";Connect Timeout=30"
If Err Then
 err.Clear
 set conn=nothing
 Response.Write "無(wú)法連接MSSQL"
 Response.End
End If
%>
打開(kāi)checklogin.asp代碼,首先在頁(yè)面上我們要引用數(shù)據(jù)庫(kù)連接文件:
<!--#include file=conn.asp-->
<%
'-------------------為防止出錯(cuò)我們定義username和password----------------
dim username
dim password
'--------用request的form方法來(lái)接受login.asp頁(yè)面?zhèn)鱽?lái)的值,并付值給我們定義的username和password
username=request.form("username")
password=request.form("password")
'---------判斷傳來(lái)的值是不是空值
if username="" then
'用response的write的方法輸出一個(gè)腳本
    response.write"<script>alert('用戶(hù)名不能為空')</script>"
 response.end
end if
if password="" then
 response.write"<script>alert('密碼不能為空')</script>"
 response.end
end if
'----------判斷用戶(hù)名和密碼是不是一樣的,如果一樣的輸出"登陸成功"并把session的標(biāo)志給這個(gè)用戶(hù),否則就輸出"登陸失敗"
'------------這里我們就要在數(shù)據(jù)庫(kù)里面比較兩個(gè)值了
set rs=server.cerateobject("adodb.recordset")
sql="select * from admin where admin_name='"&username&"' and admin_pwd='"&password&"' "
rs.open sql,conn,1,1
'如果用戶(hù)不存在
if rs.eof and rs.bof then
response.write"<script language=javascript>alert('用戶(hù)名或密碼不正確!');"
response.write"javascript:history.go(-1)</script>"
response.end
'負(fù)責(zé)將用session 產(chǎn)生會(huì)話(huà)
else
session("admin")=rs("admin_name")
session("password")=rs("admin_pwd")
session("aleave")=rs("aleave")
response.redirect "admin.asp"
end if
'---------最后不要忘記關(guān)閉數(shù)據(jù)連接,釋放資源
rs.close
set rs=nothing
%>
這里我們做好了一個(gè)簡(jiǎn)單的登陸驗(yàn)證,比如我們還可以用session來(lái)限制登陸的次數(shù),或者登陸人的權(quán)限等等,接下來(lái)我們做添加把,這個(gè)和上面的例子一樣的簡(jiǎn)單。為了便于理解我都做成了兩個(gè)頁(yè)面,在本節(jié)的最后,我會(huì)用"隱藏域的方法做個(gè)(增,刪,改)在一個(gè)頁(yè)的例子",我們繼續(xù)吧。和上一個(gè)例子一樣,這個(gè)頁(yè)面還是用來(lái)輸入值的,叫add.asp:
<html>
<title>添加示例</title>
<body>
     <form name=form1 action=adduser.asp method=post>
   <center>
   <table with=500>
      <tr>
        <td colspan="2" align="center"><font size=5 color=blue>添加</font>
        </td>
      </tr>
      <tr>
          <td><font size=2>用戶(hù)名:</font></td>

         <td>
         <input name=username type="text" maxlength="20">
        </td>
      </tr>
      <tr>
        <td><font size=2>密 碼:</font></td>
         <td>
         <input type="password" name=password maxlength="20">
          </td>
      </tr>
       <tr>
         <td colspan="2" align=center>
   <input type="submit" value=添加> 
   <input type="reset" value=重置>
          </td>
      </tr>
</table>
</center>
</form>
</body>
</html>
下面我們做adduser.asp,這個(gè)頁(yè)面就是向數(shù)據(jù)庫(kù)中添加值了
<!--#include file=conn.asp-->
'這里我們添加一個(gè)判斷,用來(lái)判斷是不是管理員,只有管理員才有權(quán)添加用戶(hù)
<%
if isempty(session("admin")) or session("admin")="" then 
response.redirect"login.asp"
end if
%>
<%
'-------------------為防止出錯(cuò)我們定義username和password----------------
dim username
dim password
'--------用request的form方法來(lái)接受add.asp頁(yè)面?zhèn)鱽?lái)的值,并付值給我們定義的username和password
username=request.form("username")
password=request.form("password")
'---------判斷傳來(lái)的值是不是空值
if username="" then
'用response的write的方法輸出一個(gè)腳本
    response.write"<script>alert('用戶(hù)名不能為空')</script>"
 response.end
end if
if password="" then
 response.write"<script>alert('密碼不能為空')</script>"
 response.end
end if
'----------判斷用戶(hù)是不是已經(jīng)存在了,如存在就提示,并返回。
set rs=server.cerateobject("adodb.recordset")
sql="select * from admin where admin_name='"&username&"'"
rs.open sql,conn,3,3
'如果用戶(hù)存在
if not(rs.eof and rs.bof) then
response.write"<script language=javascript>alert('用戶(hù)以存在,請(qǐng)重新選擇!');"
response.write"javascript:history.go(-1)</script>"
response.redirect"add.asp"
'添加用戶(hù)
else
rs.addnews
rs("admin_name")=username
rs("admin_pwd")=password
rs.update
response.write"<script language=javascript>alert('添加成功,請(qǐng)返回');"
response.write"javascript:history.go(-1)</script>"
response.redirect"add.asp"
end if
'---------最后不要忘記關(guān)閉數(shù)據(jù)連接,釋放資源
rs.close
set rs=nothing
%>

好了,現(xiàn)在我來(lái)做“更新”“刪除”,這里的重點(diǎn)是要取的要?jiǎng)h除的數(shù)據(jù)的編號(hào),數(shù)據(jù)編號(hào)是唯一的不可重復(fù)的,首先我們把所有的用戶(hù)有取出來(lái) ,并放在一個(gè)頁(yè)面上,先寫(xiě)的是更新哦,刪除和這個(gè)基本一樣就是SQL的語(yǔ)句不同,要注意的是新聞的id是怎么樣取得的!!!!這才是本文的重點(diǎn)!!!!!!!!!
---注意數(shù)據(jù)庫(kù)中沒(méi)有權(quán)限的字段,自己添上就行了----------呵呵--------
------------------------------------------------admin_admin.asp顯示頁(yè)---------------------------------
<html>
<head>
<title>管理系統(tǒng)</title>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td> </td>
        </tr>
      </table>
      <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000">
        <tr align="center" bgcolor="#ffaaaa"> 
          <td width="15%" height="24"> ID</td>
          <td width="15%">用戶(hù)</td>
          <td width="20%">密碼</td>
          <td width="20%">權(quán)限</td>
          <td width="15%">修改</td>
          <td width="15%">刪除</td>
        </tr>
        <%
Set rs=Server.createObject("ADODB.RecordSet") 
sql="select * from admin order by id" 
rs.Open sql,conn,1,1 
while not rs.eof
if rs("aleave")="super" then aleave="超級(jí)管理員" end if
if rs("aleave")="check" then aleave="普通管理員" end if
%>
        <tr align="center"> 
          <td height="22"><%=rs("id")%></td>
          <td><%=rs("admin")%></td>
          <td><%=rs("password")%></td>
          <td><%=aleave%></td>
          <td><a href="admin_AdminModify.asp?id=<%=rs("id")%>">修改</a></td>
          <td><a href="admin_AdminDel.asp?id=<%=rs("id")%>">刪除</a></td>
        </tr>
        <%
rs.movenext
wend
rs.close
set rs=nothing
%>
      </table> 
</body>
</html>
----------------------編號(hào)id的接受頁(yè)admin_AdminModify.asp-------------------------

<%
<!--#include file=conn.asp-->
'QueryString的方法取得用戶(hù)id
id=request.QueryString("id")
set rs=server.createobject("adodb.recordset")
sql="select * from admin where id="&id
rs.open sql,conn,1,1
if rs.eof then
response.write"<script language=javascript>alert('服務(wù)器出錯(cuò),請(qǐng)聯(lián)系管理員!');"
response.write"javascript:history.go(-1)</script>"
else
admin=rs("admin")
password=rs("password")
aleave=rs("aleave")
<table>
<form method="POST" action="admin_adminSave.asp?id=<%=id%>">

    <tr> 
      <td colspan=2 align=center><b>修 改 管 理 員 資 料</b></td>
</tr>
    <tr> 
      <td width="30%" height="22" align="right">用戶(hù)名:</td>
      <td width="70%"> 
        <input type="text" name="admin" value="<%=admin%>" size="20"></td>
</tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="30%" height="22" align="right">密碼:</td>
      <td width="70%"> 
        <input type="text" name="password" value="<%=password%>" size="20"></td>
</tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="30%" height="22" align="right">權(quán)限:</td>
      <td width="70%" height="22"> 
        <select name="aleave" style="font-size:14px" class="input">
<option value=super<%if aleave="super" then%> selected<%end if%>>超級(jí)管理員</option>
<option value=check<%if aleave="check" then%> selected<%end if%>>普通管理員</option>
</select>
</td>
</tr>
    <tr align="center" bgcolor="#FFFFFF" height="24"> 
      <td height="30" colspan=2> 
        <input type="hidden" value="edit" name="act">
<input name="cmdok" type="submit" id="cmdok" value=" 修 改 ">
         
<input name="cmdcance" type="reset" id="cmdcance" value=" 清 除 ">
</td>
</tr>
</form>
</table>
<%
end if
rs.close
set rs=nothing
%>
%>
-----------------------------------------------------------------
下面我們看看接收頁(yè)怎么寫(xiě)的吧!這頁(yè)面就是功能頁(yè)了!
<!--#include file=conn.asp-->
<%
admin=request.form("admin")
password=request.form("password")
aleave=request.form("aleave")
if admin="" or password="" then
response.write"<script language=javascript>alert('管理員名稱(chēng)和密碼都不能為空!');"
response.write"javascript:history.go(-1)</script>"
Response.End
end if
set rs=server.createObject("ADODB.RecordSet")
if request("act")="edit" and request.QueryString("id")<>"" then
id=request("id")
sql="select * from admin where id="& request.QueryString("id")
rs.open sql,conn,3,2
if not rs.eof then
rs("aleave")=aleave
rs("admin")=admin
rs("password")=encrypt(password)
rs.update
end if
rs.close
elseif request("act")="add" then
sql="select * from admin where admin_name='"&admin&"'"
rs.open sql,conn,3,2
if (rs.eof and rs.bof) then
rs.addnew
rs("aleave")=aleave
rs("admin")=admin
rs("password")=password
rs.update
end if
rs.close
end if
set rs=nothing
conn.close
set conn=nothing
response.redirect "admin_admin.asp"
%>
--------------------------------刪除------------------------------------
有了上面的基礎(chǔ)寫(xiě)刪除就更簡(jiǎn)單了,我們繼續(xù)哦
是不是很簡(jiǎn)單哦
<!--#include file="conn.asp" -->
<%
if isempty(session("admin")) or session("admin")="" then 
response.redirect"login.asp"
end if
%>
<%
set rs=server.createObject("ADODB.RecordSet")
rs.open "delete * from admin where id="&request.QueryString("id"),conn,1
set rs=nothing
response.redirect "admin_admin.asp"
%>
最后我們把今天說(shuō)的東西在總結(jié)一下,今天我們做了增,刪,改。這里,增加我們主要是用到了數(shù)據(jù)庫(kù)的操作,在寫(xiě)添加的時(shí)候用sql的insert語(yǔ)句直接添加也行,我們用的是rs.addnew的方法
喜歡怎么用就怎么用了,再就是修改和刪除,這兩個(gè)功能最總要的就是怎么樣鎖定要修改的那條記錄,這里的方法就多了,可以用QueryString的方法取得用戶(hù)id,也可以用"隱藏域的方法取的id"
我們也可以把增,刪,改寫(xiě)在一個(gè)頁(yè)面中 這樣顯得整潔一些,這個(gè)也就看個(gè)人愛(ài)好了,剛開(kāi)始學(xué)的話(huà)就好用多個(gè)頁(yè)實(shí)現(xiàn)功能,等時(shí)間長(zhǎng)了熟練了再用一個(gè)頁(yè)面寫(xiě)。下面大家就來(lái)看看我是怎么樣用隱藏域的方法來(lái)實(shí)現(xiàn)一頁(yè)完成這寫(xiě)功能的和怎么樣用"隱藏域的方法取的id"的?
-------------------------------一個(gè)頁(yè)面的增,刪,改------------------------------------
<!--#include file="conn.asp"-->
<%
info=trim(request("info"))
Set rs=Server.createObject("ADODB.Recordset")
select case info
case "add"
sql="select * from link"
rs.Open sql,conn,1,3
rs.Addnew
rs("name")=trim(request("webname"))
rs("url")=trim(request("url"))
rs.update
Response.Redirect"mody.asp"
case "mod"
sql="select * from link where id="&trim(request("id"))
rs.Open sql,conn,1,3
rs("name")=trim(request("webname"))
rs("url")=trim(request("url"))
rs.update
Response.Redirect"mody.asp"
case "del"
sql="delete * from link where id="&trim(request("id"))
rs.Open sql,conn,1,3
Response.Redirect"mody.asp"
case else
Set rs=Server.createObject("ADODB.Recordset")
sql="select * from link order by id asc"
rs.Open sql,conn,1,3
%>
<table border="1" width="500" align=center cellspacing="0">
    <tr>
      <td width="33%" align="center">網(wǎng)站名稱(chēng)</td>
      <td width="33%" align="center">網(wǎng)站地址</td>
      <td width="34%" align="center">操作</td>

      <form method="POST" action="mody.asp">
    <tr>
      <td width="33%"><input type="hidden" name="info" value="add"><input type="text" name="webname" size="20"></td>
      <td width="33%"><input type="text" name="url" size="20"></td>
      <td width="34%"><input type="submit" value="  添加  " name="B1"></td>
    </tr></form>
    <%do while not rs.eof%><form method="POST" action="mody.asp">

    <tr>
      <td width="33%"><input type="hidden" name="info" value="mod"><input type="hidden" name="id" value="<%=rs("id")%
>"><input type="text" name="webname" size="20" value="<%=rs("name")%>"></td>
      <td width="33%"><input type="text" name="url" size="20" value="<%=rs("url")%>"></td>
      <td width="34%"><input type="submit" value="修改" name="B1">    <input type="button" name="Submit" value="刪除" 
onClick="javascript:location='mody.asp?info=del&id=<%=rs("id")%>'"></td>
    </tr></form><%rs.movenext
    loop%>
  </table>
<%end select%>
<%rs.close%>

-------------------------------------------------------------------------------------------------------------------
第三天

上一節(jié)我們講解了ASP中應(yīng)用最廣泛的操作,其實(shí)那就是個(gè)網(wǎng)站的后臺(tái)的管理中(用戶(hù)的管理),新聞管理,投票管理也都是這樣做的,不用的就是他們的功能更復(fù)雜,數(shù)據(jù)更多,其中留言板就是數(shù)據(jù)的存儲(chǔ)和讀取的過(guò)程,不過(guò)是我們把這個(gè)過(guò)程復(fù)雜化了,第一天,我們講解了數(shù)據(jù)和值的傳遞過(guò)程,用<form>來(lái)將值傳遞到下一個(gè)頁(yè)面中,并用Request.form來(lái)接受,用response.write的方法來(lái)輸出,這里主要是讓大家理解值的傳遞過(guò)程,當(dāng)然我們也可以在一個(gè)頁(yè)面中傳遞。
第二天,我們講解了SQL的常用的語(yǔ)句,和在ASP中怎么樣執(zhí)行SQL語(yǔ)句,今天我們主要講一下:SESSION和cookies,回顧以下response,request。
首先,登陸模塊的程序都會(huì)用到檢測(cè)是不是用戶(hù)已經(jīng)登陸這個(gè)步驟。這就用到了SESSION和cookies,我們還是用代碼來(lái)說(shuō)明(在上面的程序中我們已經(jīng)用到了):
---------------------------------------------------
<1>這是session的寫(xiě)法:
<%
'如果用戶(hù)存在
session("aleave")=rs("aleave")
%>
------------------------------------------------
<2>這是cookies的寫(xiě)法:
<%
response.cookies("adminok")=true
%>
這樣我們就能在我們需要的地方進(jìn)行判斷了
代碼如下:
<%
  if session("aleave")="" then
      response.redirect "adminlogin.asp"
   response.end
  end if
%>
這里是cookies的驗(yàn)證判斷:
<%
if request.cookies("adminok")="" then 
response.redirect"login.asp"
end if
%>
這樣沒(méi)有session和cookies標(biāo)志的用戶(hù)是無(wú)法打開(kāi)這個(gè)頁(yè)面。
最后我們說(shuō)一下response.redirect,它是重定向的意思,后面的"login.asp"就是轉(zhuǎn)向的文件。這樣沒(méi)有登陸的管理員是無(wú)法看到后面的內(nèi)容的。
下面總結(jié)一下:
response常用的就是response.write (),response.redirect() 分別是寫(xiě)字符串和轉(zhuǎn)向的作用;
request基本就是request.form(),request.querystring() 分別是接受post,get方法傳來(lái)的信息;
這里我給大家一個(gè)常用的(Access)數(shù)據(jù)庫(kù)連接代碼(Sql的在上面的例子中有哦,我就不寫(xiě)了,以后大家只要改換“數(shù)據(jù)庫(kù)”就可以直接用了。
----------
<%
'數(shù)據(jù)庫(kù)連接程序conn.asp
strSQL="DBQ="+server.mappath("db/news.mdb")+";DRIVER={Microsoft Access Driver (*.mdb)};"
set conn=server.createobject("ADODB.CONNECTION")
conn.open strSQL
%>
為了保證數(shù)據(jù)庫(kù)的安全呢,我們可以把數(shù)據(jù)庫(kù)的擴(kuò)展名換成.asp的,也可以是asa,最好把數(shù)據(jù)庫(kù)的名字弄的復(fù)雜點(diǎn),在就是數(shù)據(jù)庫(kù)的字段也要弄的復(fù)雜點(diǎn)哦,要不然人家用軟件猜解也是很容易的,并在頁(yè)面中過(guò)濾掉非法的字符,這樣能安全點(diǎn)(網(wǎng)站有很多的防SQL注入的代碼哦,也有很多的注入工具),還是自己把網(wǎng)站弄的安全點(diǎn)吧,并且用例子來(lái)演示了怎么樣實(shí)現(xiàn)整個(gè)功能,希望大家能多看看代碼,多動(dòng)手來(lái)打打代碼,還有就是盡量用“記事本”編寫(xiě)代碼,不要上來(lái)就用一些開(kāi)發(fā)工具和代碼提示的軟件,這樣不利于我們對(duì)代碼的理解和記憶。

---------------------------------------------------------------------------------------------------
第四天:利用ASP分頁(yè)顯示實(shí)例

這里我們要說(shuō)的就是ASP中比較復(fù)雜的分頁(yè)技術(shù)了,代碼有點(diǎn)多,但是不是很復(fù)雜,大家多看幾便就能理解了,首先我們先理解一下分頁(yè)的原理:
在前幾天我們了解了Recordset對(duì)象的以上屬性和方法后,我們來(lái)考慮一下,如何運(yùn)用 它們來(lái)達(dá)到我們分頁(yè)顯示的目的。我們可以為PageSize屬性設(shè)置一個(gè)值,從而指定從記錄組中取出的構(gòu)成一個(gè)頁(yè)的行數(shù);然后通過(guò)RecordCount屬性來(lái)確定記錄的總數(shù);再用記錄總數(shù)除以PageSize就可得到所顯示的頁(yè)面總數(shù);最后通過(guò)AbsolutePage屬性就能完成對(duì)指定頁(yè)的訪(fǎng)問(wèn)。
好象很并不復(fù)雜呀,下面讓我們來(lái)看看程序該如何實(shí)現(xiàn)呢?代碼主要用到Connection、RecordSet這兩個(gè)對(duì)象,程序中的數(shù)據(jù)庫(kù)為Access庫(kù),采用OLEDB方式連接庫(kù)。
<%
Set conn = Server.createObject("ADODB.Connection")
strcon="provider=microsoft.jet.oledb.4.0;data source="& _
server.mappath("mdb.mdb")
conn.Open strcon
Set rs = Server.createObject ("ADODB.Recordset")
sql="select * from table order by id desc"
rs.Open sql, conn, 1
page=1 ' 設(shè)置變量PAGE=1
rs.PageSize = 5 '每頁(yè)顯示記錄數(shù)
if Not IsEmpty(Request("Page")) then '如果Page已經(jīng)初始化...
Page = CInt(Request("Page")) '接收Page并化為數(shù)字型賦給Page變量
if Page > rs.PageCount then '如果接收的頁(yè)數(shù)大于總頁(yè)數(shù)
rs.AbsolutePage = rs.PageCount '設(shè)置當(dāng)前顯示頁(yè)等于最后頁(yè)
elseif Page <= 0 then '如果page小于等于0
Page = 1 '設(shè)置PAGE等于第一頁(yè)
else
rs.AbsolutePage = Page '如果大于零,顯示當(dāng)前頁(yè)等于接收的頁(yè)數(shù) 
end if
End if
Page = rs.AbsolutePage 
%>
<% 
For i = 1 to rs.PageSize
if rs.EOF then 
Exit For 
end if '利用for next 循環(huán)依次讀出記錄
%>
'一個(gè)頁(yè)中顯示多少條數(shù)據(jù)Rs.pagesize=5,把5改為你想顯示的記錄數(shù),比如說(shuō)改為10,那么每頁(yè)就是顯示10頁(yè)。還需要修改的是select語(yǔ)句,就是你要檢索的數(shù)據(jù)庫(kù)了,也就是說(shuō)放到任何一個(gè)系統(tǒng)中上面的代碼只需很小的改動(dòng)都可以直接拿過(guò)來(lái)用。然后就是要循環(huán)讀出記錄的顯示內(nèi)容了,比如:
<table width=50 border=1 align=center><tr><TD><% =rs("內(nèi)容") %></td></tr></table><%rs.MoveNextnext%>
  當(dāng)然也可以改為response.write輸出。
  最后,添加"下一頁(yè),上一頁(yè)"的鏈接:
<%if request("page")>1 then%><a Href="test.asp?Page=<% = 1%>">首頁(yè)</a> <a Href="test.asp?Page=<% =request("page") -1 %>">上一
頁(yè)</a><%end if %><%if request("page")<>rs.pagecount then %><a Href="test.asp?Page=<% =request("page") + 1%>">下一頁(yè)</a> <a 
Href="test.asp?Page=<% = rs.PageCount%>">尾頁(yè)</a> <% end if %>
  上面的代碼只管復(fù)制粘貼,只需把"page.asp"改成你自己的文件名就可以了。隨便把這段代碼放在什么地方?就是你想讓它出現(xiàn)下一頁(yè)下一頁(yè)鏈接的地方吧。
  最后,關(guān)閉記錄集釋放資源:
<% 
rs.close 
Set rs = Nothing
conn.close 
set conn=nothing 
%>
--------------------------------------------------------------------------------
要克服最后一頁(yè)中的"下一頁(yè)"仍處于連接狀態(tài)有一個(gè)辦法,就是把程序中稍加改動(dòng)如下:
<%if request("page")<>rs.pagecount then %>
<a Href="test.asp?Page=<% =request("page") + 1%>">下一頁(yè)</a> 
<% else response.write"下一頁(yè)"%>
就是此時(shí)把"下一頁(yè)"設(shè)置成非連接狀態(tài)

For i = 1 to rs.PageSize
if rs.EOF then 
Exit For 
end if '利用for next 循環(huán)依次讀出記錄
使用的是Do while。
分頁(yè)代碼的示例:
-----------------------------------------------------------------------------------
<%
db="數(shù)據(jù)地址.mdb"
Set conn = Server.createObject("ADODB.Connection")
conns="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(""&db&"")
conn.Open conns
Set rs = Server.createObject("ADODB.Recordset")
sql="select * from 表名 order by id desc"
rs.open sql,conn,1,1
if rs.eof then
response.write("沒(méi)有記錄")
else
Page=Int(Abs(Request("page")))
IF not IsNumeric(Request("page")) Or IsEmpty(Request("page")) Then page=1
rs.pagesize = 10 '每頁(yè)顯示記錄數(shù)
total = rs.RecordCount
mp = rs.pagesize
rs.absolutepage = page
i=0:k=0
do while not rs.eof and mp>0:k=k+1
response.write("顯示第"&rs("id")&"條記錄")
i=i+1
mp=mp-1
rs.movenext
loop
if page>rs.pagecount then Response.redirect("index.asp?page="&rs.pagecount)
if page>1 then
response.Write("<a href='index.asp?&page=1' title='首頁(yè)'>首頁(yè)</a>")
response.Write(" <a href='index.asp?page="&page-1&"' title='上一頁(yè)'>上一頁(yè)</a> ")
end if
response.Write("第"&page&"/"&rs.pagecount&"頁(yè) 共"&total&"條記錄 本頁(yè)顯示第"&(page-1)*rs.pagesize+1&"條到第"&(page-1)
*rs.pagesize+k&"條 ")
if page<rs.pagecount then
response.Write("<a href='index.asp?page="&page+1&"' title='下一頁(yè)'>下一頁(yè)</a>")
response.Write(" <a href='index.asp?page="&rs.pagecount&"' title='尾頁(yè)'>尾頁(yè)</a>")
end if
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
這樣一個(gè)分頁(yè)的程序就算是做好了,這個(gè)程序非常簡(jiǎn)單而且便于理解,是個(gè)學(xué)習(xí)的好例子。
大家在做一個(gè)程序前應(yīng)該多想想怎么樣實(shí)現(xiàn)這個(gè)功能,然后在動(dòng)手去寫(xiě)這個(gè)代碼,這樣才能做到胸有成竹。

----------------------------------------------------------------------------------------------------------------
第五天

今天我們來(lái)說(shuō)說(shuō)ASP組件,asp的組件的功能是十分強(qiáng)大的,今天我們就來(lái)簡(jiǎn)單的說(shuō)說(shuō)。要用asp組件就要?jiǎng)?chuàng)建asp組件,在一個(gè)asp頁(yè)面中創(chuàng)建asp組件,需要使用server內(nèi)置對(duì)象的createobject的方法,我們來(lái)看例子:
<%
Set JMail = Server.createObject("JMail.Message") 
%>
上面我們創(chuàng)建了JMail組件對(duì)象的示例,并賦值給JMail。有的asp組件還要求注冊(cè),而且,每個(gè)asp組件都有自己屬性和方法,這個(gè)就要大家自己平時(shí)多看看了,用什么組件就多了解那個(gè)組件吧。
下面我們用現(xiàn)在常用的Jmail組件來(lái)詳細(xì)說(shuō)明組件的用法:
在Asp程序中用Jmail發(fā)郵件的比較多,主要是因?yàn)槎鄶?shù)虛擬主機(jī)支持他,其它的支持的較少。下面是一個(gè)Jmail發(fā)郵件的例子代碼,如果您在自己的機(jī)器上試,請(qǐng)下載Jmail.dll,下載解壓后,雙擊Setup.bat 即可完成安裝!
<%
On error resume next
Dim JMail, contentId
Set JMail = Server.createObject("JMail.Message") 
JMail.Charset = "gb2312" ' 郵件字符集,默認(rèn)為"US-ASCII"
' JMail.ISOEncodeHeaders = False ' 是否進(jìn)行ISO編碼,默認(rèn)為T(mén)rue
' 發(fā)送者信息(可用變量方式賦值)
JMail.from = "email@net118.com" ' 發(fā)送者地址
JMail.fromName = "XX" ' 發(fā)送者姓名
JMail.Subject = "郵件主題" ' 郵件主題
' 身份驗(yàn)證
JMail.MailServerUserName = "myusername" ' 身份驗(yàn)證的用戶(hù)名
JMail.MailServerPassword = "mypassword" ' 身份驗(yàn)證的密碼
' 設(shè)置優(yōu)先級(jí),范圍從1到5,越大的優(yōu)先級(jí)越高,3為普通
JMail.Priority = 3
JMail.AddHeader "Originating-IP", Request.ServerVariables("REMOTE_ADDR")
' 加入一個(gè)收件人【變量email:收件人地址】可以同一語(yǔ)句重復(fù)加入多個(gè)
JMail.AddRecipient("webmaster@net118.com")
' 加入附件【變量20051027171159.htm:附件文件的絕對(duì)地址,確保用戶(hù)IUSR_機(jī)器名有訪(fǎng)問(wèn)的權(quán)限】
' 【參數(shù)設(shè)置是(True)否(False)為Inline方式】
'contentId = JMail.AddAttachment (Server.MapPath("jmail.asp"), True)
' 郵件主體(HTML(注意信件內(nèi)鏈接附件的方式))
JMail.HTMLBody = "<html><head><META content=zh-cn http-equiv=Content-Language><meta http-
equiv=""Content-Type"" content=""text/html; charset=gb2312""><style type=text/css>A:link { 
FONT-SIZE: 14px; TEXT-DECORATION: none; color: #000000}A:visited {FONT-SIZE: 14px; TEXT-
DECORATION: none; color: #666666}A:hover {COLOR: #ff6600; FONT-SIZE: 14px; TEXT-DECORATION: 
underline}BODY {FONT-SIZE: 14px} --></style></head><body><font color=red>郵件正文
</font><br><font color=green>郵件正文</font><br><b>郵件正文</b></body></html>"
' 郵件主體(文本部分)
JMail.Body = "我們的郵件采用了HTML格式,但是您的郵件查看軟件可能不支持。。。郵件正文,郵件正
文,郵件正文"
' 發(fā)送【調(diào)用格式:objJMail.Send([username:password@]SMTPServerAddress[:Port])】
JMail.Send("smtp.163.com")
' 關(guān)閉并清除對(duì)象
JMail.Close()
Set JMail = Nothing
if err.number<>0 then
 response.write "發(fā)送發(fā)送失敗!"
else
 response.write "郵件發(fā)送成功!"
end if
%>
這樣就可以用jmail來(lái)發(fā)郵件了,能發(fā)郵件的組件還有 Cdonts組件等等。

asp 的功能遠(yuǎn)遠(yuǎn)不只這些,象用asp的fso(文件系統(tǒng)組件)等等,很多強(qiáng)大的功能都等我們?nèi)W(xué)習(xí),五天搞定ASP就是給出學(xué)的朋友領(lǐng)條路,這五天說(shuō)的都是ASP的精華,大家有空多看看,就能理解了

該文章在 2022/7/4 12:15:49 編輯過(guò)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
在线日本有码中文字幕 | 日韩一区二区中文字幕 | 亚洲欧美综合人成在线 | 又黄又爽又无遮挡国产 | 亚洲精品NV久久久久久久久久 | 亚洲综合区第二页 |