asp初学者参考

来源:互联网 发布:mysql无法启动1067 编辑:程序博客网 时间:2024/06/04 20:09

ASP不难 只要用心去学就行
我这里有一些ASP的常用代码


1、Access数据库连接代码
<%
db="mydata.mdb" ‘如果放在目录中,就要写明"database/mydata.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
conn.Open connstr
%>
2、标准Sql语句写法和rs绑定输出
包括取全部记录
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select * from news"
Rs.Open SqlStr,conn,1,1 ‘运行sql语句,把数据提出到rs对象中
选取几条数据
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select top 6 * from news"
Rs.Open SqlStr,conn,1,1 ‘运行sql语句,把6条数据提出到rs对象中
选取一条指定表中id字段数值的数据
SqlStr="select * from news where id="&request("id")
set rs=conn.execute(SqlStr)‘运行sql语句,把6条数据提出到rs对象中
添加一条表单传过来的数据替换
SqlStr="insert into news(title,neirong) values(request("title"), request("neirong"))
conn.execute(SqlStr)
修改一条指定表中id字段数值的数据
SqlStr="update news set title=’"&request("title")&"’,neirong=’"&request("内容")&"’ where id="&request("id")
conn.execute(SqlStr) ‘运行sql语句
删除一条指定表中id字段数值的数据
SqlStr="delete from news where id="&request("id")
conn.execute(SqlStr) ‘运行sql语句
rs绑定输出
<%=rs("title")%>
<%=rs("content")%>
<%=rs("time")%>
.....
3、jave页面跳转和返回上一页
(1)页面跳转
<%
response.Write("<script>alert('恭喜您删除成功!!!');location.href='index.asp'</script>")
%>
(2)返回上一页
<%
<script language=javascript>alert('该学号学生不存在!!!');window.history.go(-1)</script>
%>
4、判断语句:判断表单传来的用户名和密码是否正确,并提示
If request("username")="admin" then
Response.write"恭喜,你已经登录成功"
Else
Response.write"对不起,您输入的用户名错误,请返回重输入"
End if

If request("name")="admin" and request("pass")="admin"then
Response.redirect"admin.asp"
Else
Response.redirect"login.asp"
End if
5、循环语句:循环显示6条数据库中的记录
写法1:
for n=1 to 6
response.write rs("title")&"< br>"
if not rs.eof then
exit for
else
rs.movenext
end if
next
写法二:
do while not rs.eof
response.write rs("title")&"< br>"
rs.movenext
loop
6、常用变量转换函数:
Now() 函数返回系统时间
Date() 函数返回当前系统日期.
CStr(int) 函数转化一个表达式为字符串
CInt(string) 将一个表达式转化为数字类型
Trim(request("username")) 函数去掉字符串左右的空格
Left(rs("title"),10) 函数返回字符串左边第length个字符以前的字符(含第length个字符),一般在限制新闻标题的显示长度的时候用
Len(string) 函数返回字符串的长度.
7、Recordset对象操作数据库语法
(1)打开sql语句指定的表中的数据,把这批数据放入rs对象中
取出news表中所有的数据放到rs中
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select * from news"
Rs.Open SqlStr,conn,1,1
取出news表中前6条数据放到rs中
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select top 6 * from news"
Rs.Open SqlStr,conn,1,1
(2)循环显示6条rs对象中存在的数据,列表显示
不带连接的写法
for n=1 to 6
response.write rs("title")&"< br>"
if not rs.eof then
exit for
else
rs.movenext
end if
next
带连接的写法
for n=1 to 6
response.write "< a href=show.asp?id=rs("id")>"& left(rs("title"),20)&"< /a>< br>"
if not rs.eof then
exit for
else
rs.movenext
end if
next
(3)向数据库添加一条数据代码
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select * from news"
Rs.Open SqlStr,conn,1,3 ‘注意这里的1,3代表可以写入的打开数据表
Rs.addnew
Rs("title")=trim(request("title"))
Rs("neirong")=request("neirong")
Rs("date")=now()
rs.update ‘真正写入数据库
(4)修改一条记录的代码,通过(2)中的连接传递过来了id数值
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select * from news where id="&request("id")
Rs.Open SqlStr,conn,1,3 ‘注意这里的1,3代表可以写入的打开数据表
Rs("title")=trim(request("title"))
Rs("neirong")=request("neirong")
Rs("date")=now()
rs.update ‘真正写入数据库
(5)删除数据库中一条记录,通过连接传递过来了数据得id数值
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select * from news where id="&request("id")
Rs.Open SqlStr,conn,1,3 ‘注意这里的1,3代表可以写入的打开数据表
rs.delete ‘删除该条数据
8、利用Session对象保护后台管理页面admin.asp,防止未登陆用户进入
在网站后台网页admin.asp的头部加入下面的代码,
if session(admin)< >"ok" then
response.redirect"login.asp"
response.end
end if
在网站后台登陆页的密码验证部分标准写法
AdmName=Request.Form("Name")
AdmPass=Request.Form("Pass")
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="Select * from Admin where name='"&AdmName&"' and pass='"&AdmPass&"'"
Rs.Open SqlStr,conn,1,3
if Rs.EOF AND RS.BOF then
Response.Redirect("login.asp")
response.end
else
session("admin")="ok"
Response.Redirect("admin.asp")
response.end
end if
9、分页代码
sql = "select……………………省略了sql语句
Set rs=Server.Createobject("ADODB.RECORDSET")
rs.Open sql, conn, 1, 1
if not rs.eof then
pages = 30 '定义每页显示的记录数
rs.pageSize = pages '定义每页显示的记录数
allPages = rs.pageCount '计算一共能分多少页
page = Request.QueryString("page")'通过浏览器传递的页数
’if语句属于基本的排错处理
if isEmpty(page) or Cint(page) < 1 then
page = 1
elseif Cint(page) > allPages then
page = allPages
end if
rs.AbsolutePage = page
Do while not rs.eof and pages > 0
'这里输出你要的内容………………

pages = pages - 1
rs.MoveNext
Loop
else
Response.Write("数据库暂无内容!")
End if
rs.Close
Set rs = Nothing
分页页码连接和跳转页码程序
< form Action="v3.asp" Method="GET">
< %
If Page < > 1 Then
Response.Write "< A HREF=?Page=1>第一页< /A>"
Response.Write "< A HREF=?Page=" & (Page-1) & ">上一页< /A>"
End If
If Page < > rs.PageCount Then
Response.Write "< A HREF=?Page=" & (Page+1) & ">下一页< /A>"
Response.Write "< A HREF=?Page=" & rs.PageCount & ">最后一页< /A>"
End If
%>
< p>输入页数:< input TYPE="TEXT" Name="Page" SIZE="3"> 页数:< font COLOR="Red">< %=Page%>/< %=rs.PageCount%>< /font>
< /p>
< /form>
10、分行列显示图片和产品名称的代码(4列x3行=12个)
< %
Set Rs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select top 12 * from myproduct"
Rs.Open SqlStr,conn,1,1
i=1
%>

< table width="90%" border="1" cellspacing="0" sellpadding="0">
< tr>
< %
do while not rs.eof
%>
< td align="center">
< img src="< %=rs("imgurl")%>" width="52" height="120">< br>
< %=rs("productname")%>
< /td>
< % if i mod 4=0 then response.write"< /tr>< tr>"
i=i+1
rs.movenext
loop
rs.close
%>
11.验证是否为空,鼠标脱离文本框读库
<script language="javascript">
function stucode()
{
if(document.form1.stcode.value=="")
{
alert('该学号不能为空!!!');
}
else
{
location.href='index.asp?stucode='+document.form1.stcode.value
}
}
</script>
<%
on error resume next
stucode=trim(request("stucode"))
if stucode<>"" then
conn.open connstr
strSql="Select * From sheet1 Where stcode='"&trim(stucode)&"'"
'response.Write strSql
set rs=conn.execute(strSql)
if rs.eof then
%>
<script language=javascript>alert('该学号学生不存在!!!');window.history.go(-1)</script>
<%
end if
end if
%>

<input name="stcode" type="text" id="stcode" onBlur="stucode()" value="<%=stucode%>">

<input name="username" type="text"<%if not rs.eof then %> value="<%=rs("username")%>"<%end if%>>

<input name="tel1" type="text" id="tel1"<%if not rs.eof then %> value="<%=rs("tel1")%>"<%end if%>>

<input name="tel2" type="text" id="tel2"<%if not rs.eof then %> value="<%=rs("tel2")%>"<%end if%>>

<tr>
<td><label>
<input type="radio" name="sex" value="男"<%if not rs.eof and rs("sex")="男" then %> checked<%end if%>>
男</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="sex" value="女"<%if not rs.eof and rs("sex")="女" then %> checked<%end if%>>
女</label></td>
</tr>



<select name="classb" id="classb">
<option value="<%=rs("classb")%>"><%=rs("classb")%></option>
</select>



<select name="department" id="department">

<option value="<%=rs("department")%>"><%=rs("department")%></option>
</select>



<select name="selectfz" id="selectfz">
<%if rs("department")="计算机网络技术" then%>
<option value="网络技术支持">网络技术支持</option>
<option value="WEB网页设计">WEB网页设计</option>
<option value="网络数据库管理">网络数据库管理</option>
<%else%>
<option value="WEB网页设计">WEB网页设计</option>
<option value="网络数据库管理">网络数据库管理</option>
<option value="信息安全">信息安全</option>
<%end if%>
</select>
%>
< /tr>
< /table>
12、图片的上传方法
1、在数据添加表单中加入用来保存上传的图片地址和文件的输入框,记下表单名称和这个输入框的名称,以备后面修改时候用。
2、在需要调用上传的输入框后面加上<iframe name="ad" frameborder=0 width=80% height=30 scrolling=no src=upload.asp></iframe>
3、修改upload.asp,找到其中的<%if request.QueryString("filename")<>"" then response.write "<script>parent.form1.textfield6.value='"&request.QueryString("filename")&"'</script>"%>;修改其中的form1.textfield6为, 上面第一条中记录的表单名和输入框名称。
4、修改upfile.asp,找到其中第5行formPath="../../TempPic",然后修改=号后面的上传图片保存目录名称就ok了
13、eweb发布器使用方法
<textarea name="content" cols="50" rows="10" id="content" style="display:none"></textarea>
<iframe src="edit/ewebeditor.asp?id=content&style=s_yellow" frameborder="0" scrolling="no" width="550" HEIGHT="350"></iframe>
注意:textarea中的name名字要与iframe中的id的名字要保持一保持
14、asp中的使用数组
<%
response.Write request.form("v")&"所有值<br>"
dim v
v=split(request.form("v"),", ") '转换成组数
dim max
max=Ubound(v) '得出这个组数的维数
response.Write max&"维数组<br>"
dim sv
dim zv
for i=0 to max
sv=v(i)
response.Write sv&"数据库提交值<br>"
next
'response.Write zv&"数据库提交值<br>"
%>

15.用下拉列表框的下拉操作读库
<select name="select2" class=pageselect onChange="javascript:location.href=this.options[selectedIndex].value">
<option value="chanpin_zonghui.asp?fenleiid=0" selected>全部产品</option>
<option value="chanpin_zonghui.asp?fenleiid=1" >精品推荐</option>
<option value="chanpin_zonghui.asp?fenleiid=2" >安全阀</option>
<option value="chanpin_zonghui.asp?fenleiid=3" >过滤阀</option>
<option value="chanpin_zonghui.asp?fenleiid=4" >疏水阀</option>
<option value="chanpin_zonghui.asp?fenleiid=5" >控制阀</option>
<option value="chanpin_zonghui.asp?fenleiid=6" >调节阀</option>
<option value="chanpin_zonghui.asp?fenleiid=7" >电磁阀</option>
<option value="chanpin_zonghui.asp?fenleiid=8" >止回阀</option>
<option value="chanpin_zonghui.asp?fenleiid=9" >旋塞阀</option>
<option value="chanpin_zonghui.asp?fenleiid=10" >截止阀</option>
<option value="chanpin_zonghui.asp?fenleiid=11" >闸阀</option>
<option value="chanpin_zonghui.asp?fenleiid=12" >蝶阀</option>
<option value="chanpin_zonghui.asp?fenleiid=13" >球阀</option>
<option value="chanpin_zonghui.asp?fenleiid=14" >其它</option>
</select>

获取数据用request.QueryString("fenleiid")


16.文本框的特效
<input name="key" type="text" class="button" onFocus="this.select()" onBlur="if (value ==''){value='请输入关 键字'}" onClick="if(this.value=='请输入关键字')this.value=''"
onMouseOver="this.focus()" value="请输入关键字" size="13" maxlength="50">

原创粉丝点击