Warning: session_start() [function.session-start]: open(/home/mybwtech/public_html/article/tmp/sess_eaa464f6f7d54616a011fa8c239d380f, O_RDWR) failed: No such file or directory (2) in /home/mybwtech/public_html/article/global.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/mybwtech/public_html/article/global.php:3) in /home/mybwtech/public_html/article/global.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/mybwtech/public_html/article/global.php:3) in /home/mybwtech/public_html/article/global.php on line 3
关于编写ASP代码的一些Tips - 龙舞天翔资源共享
我的控制台 会员登陆 免费注册 最后更新 高级搜索 返回首页 我要投稿 退出登陆 龙舞论坛
当前在线: 0
 
站内搜索
Google
www.bwtech.net

德州扑克资料
投资透视
网站优化推广&SEO
网站设计
网络编程语言与开发
课件开发制作
网络文学
小资生活
数码摄影
财经-经营管理
其他杂项
龙舞天翔资源共享 / 网络编程语言与开发 / ASP语言技术 / 关于编写ASP代码的一些Tips
关于编写ASP代码的一些Tips
2006-05-09          点击: 1314
关于编写ASP代码的一些Tips

 

 

 

 

 

 

 

 

用application和session做的简单计数器

<%
if isempty(session("conn")) then
application.lock
set counterfile=server.createobject("scripting.filesystemobject")
set temp=counterfile.opentextfile(server.mappath("count.txt"),1,true,false)
num=temp.readline
if num=empty then num=1
application("count")=num
application("count")=application("count")+1
temp.close
end if
%>
<center><h1>访客人数:<font color=red><%=application("count")%></font></h1>
<%
set counterfile=server.createobject("scripting.filesystemobject")
set temp=counterfile.createtextfile(server.mappath("count.txt"),true,false)
temp.writeline(application("count"))
temp.close
application.unlock
%>

######################################

If Then 程序语句 End if 如

<%
score=request("score")
if not isnumeric(score) then
m="请输入数字"
elseif score="" then
m="请输入数据"
elseif score>=60 and score<=100 then
m="及格"
elseif score>100 or score<0 then
m="请输入0---100之间的数"
else
m="不及格"
end if
%>


cookies:


<%
response.buffer=true ' 因是没有在服务器下载数据前就进行了交换,所以用缓冲区来装下载的数据
var=request.cookies("var")
if var="" then var=empty '因为""不可与数值运算
response.write"开始var="&var&"<br>"
var=var+10
response.write"结束var="&var&"<br>"
response.cookies("var")=var
response.cookies("va").expires="2004/3/31/" '设定有效期
%>


#######################################

称动、复制、删除文件


<%
set fs=server.createobject("scripting.filesystemobject")
source=server.mappath("1/1.txt")
target=server.mappath("1.txt")
on error resume next
fs.movefile source,target 'movefile换在deletefile\copyfile
if err.number=53 then
response.write source&"文件不存在"
response.end
elseif err.number=70 then
response.write target&"文件为只读或锁"
response.end
elseif err.nuber<>0 then
response.write"other="&err.number
response.end
end if
%>

########################################
读取文本内容

<%
set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("1/1.txt")
set txt=fs.opentextfile(file,1,true)
if not txt.atendofstream then
line=txt.readline'可换成readall
response.write line&"<br>"
end if
%>


####################################


SERVER & Request]
<%=server.htmlencode("<i>原码输出</i>")%>
<%=request.servervariables("local_addr")%>'本地IP
<%=request.servervariables("remote_addr")%>'Client 端IP

 

连接access数据库的两种方法:

1、<% Set CN = Server.CreateObject("ADODB.Connection"
CN.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("message.mdb")
Set rs=Server.CreateObject("ADODB.Recordset")
%>
 2、<%  Set con = Server.CreateObject("ADODB.Connection")
  DBPath = Server.MapPath("bbs.mdb")
con.Open "provider=microsoft.jet.oledb.4.0;data source="&DBPath
  Set rs=Server.CreateObject("ADODB.Recordset")
 %>

####################################
数据表打开的几种方法

1、SQL = "Select * From 数据表 Where 主编号=" & request("ID")
 RS.Open SQL,con,1,3
2、RS.Open "数据表",con,1,3
3、RS.Open "Select * from student",con,1,3

 

读取数据库数据的方面


1、<%=RS(0).name%> '读取第一栏的表头名称
2、<%=RS(1).value%> '读取第二栏的里的内容
3、<%=Rs.Fields(1).Value%> '同上
4、Rs.recordcount '总记录数
5、Rs.fields.Count '有几个栏位


###################################
如读取表中的数据:

<%
Num=0
 For Itemline=0 To Rs.recordcount-1 'recordcount-1 不会移出最后一笔记录
 Response.Write "<TR>"
   For I=0 To Rs.fields.Count-1
 Response.Write "<TD><input type=text name=" & Num & _
 " value=" & Rs(I).Value & "></TR>"
   Response.Write chr(10) '输出换行符
    Num=Num+1
 Next
  
 Response.Write "</TR>"
   Rs.MoveNext
   If Rs.Eof Then Exit For '可省略
Next  
 %>


保存数据文件
#################################


<%
Dim Item(100) '数组的多少很重要
For I=0 To 100
Item(I)=Request(I)
Next
  Set conobject = Server.CreateObject("ADODB.Connection")
  DBPath = Server.MapPath("exam.mdb")
  conobject.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&DBPath
  Set RS=Server.CreateObject("ADODB.Recordset")
   RS.Open "Select * from first",conobject,1,3
 Num=0   
For Itemline=0 To Rs.recordcount-1
 For Itemnum=1 To Rs.Fields.Count-1
    Rs.Fields(Itemnum).Value=Item(Num)
   Num=Num+1
 rs.update
 Next
   Rs.MoveNext
   If Rs.Eof Then Exit For
Next  
Response.Redirect "editexam.asp"

%>

 

###################################
判断表单是否填写完整

1、If name = "" Or email = "" Or subject = "" Or _
textmemo = "" Then
 Response.Write "<script>alert('松子说了,不填完就别想留言!');history.go(-1);</script>"
 Response.End
End If

 

增加新数据方法

1、rs.open ""&s&"",con,1,3
rs.addnew
rs("name")=request("name")
rs("depa")=request("depa")
rs.update
%>


#################################

常用函数
一、字符类
1、Asc(string)'该数值为参数字符串第一个字母的ASCII值如Asc(a)=97
2、Chr(charcode)'将指定的字符码转为字符(string) 如chr(97)等于A
3、Len(string)'计算参数中字符串的字符数目如 len(ab c)=4
4、Left(字符串,指定长度) 如left("123 4",3)=123
5、Right 同上
6、MId(字符串,开始字符,指定长度)如mid("123 4",3,1)=1
7、LTrim(字符串)'去除字串中的前置空白如Ltrim(" hello“)="hello"
8、RTrim(字符串)同上
9、Trim(字符串)'去除字符串前、后的空白
10、UCase(字符串)将字符串中所有字符转换为大写字符如Ucase("h")="H"
11、Lcase(字符串)同上
12、Space(空格数目) 如"hello"&space(1)&"jacky"=hello jacky
13、String(字符数,字符)用来产生一个重复字符组成的字符串如string(3,"ab")=aaa'两个以上字符时只重复第一个
14、Join(来源数组[,分隔字符])
15、Split (来源字符串,分隔字符串)
16、replace(源字符串,源字符串中的字符,要变成的字符串)
如 a="long" response.write (replace(a,"l","s"))=song
二、数学类
1、Abs(数值)'返回参数值的绝对值,返回值的类型与参数数值类型相同。
2、Sgn(数值)判断参数数值的正负号,参数为正数时返回1,负数返回-1,0返回0
3、Sqr(数值)求得数值的平方根,不可为负
4、Hex(数值) 转化为16进位
5、Oct (数值) 转化为8进位
三、类别转换
1、Int(数值) 返回数值的整数部分 如int(3,3)=3
2、Fix(数值)同上,只是int(-3.8)=4 而fix(-3.8)=3
3、Str(数值) 将数值转换为字符串。
4、Format(处理对象[,格式])
5、Year(date)/Month(date)/Day(date)取得年、月、日
6、Hour(time)/Minute(time)/Second(time)
7、WeekdayName(1-7)如weekdayname(1)=星期日
8、MonthName(1-12)如monthname(1)=一月
四、比较运算符:= 、<>、<、>、<=、>=
五、Not逻辑运算符:And 、Or、Not
六、指定运算符号:=
七、运算符符号:+、*、/、\(整数除法)2\5=0、Mod(除法取余数)、^乘(次方)5^2=25
八、串拉运算符:& 如abc&efg=abcdefg

从表中删除一条记录:

1、<a href=del.asp?ID=<%=RS("id")%> onclick="return confirm('你真的要删除吗?')">删除</a>
2、<%
id=request("id")
Set CN = Server.CreateObject("ADODB.Connection")
CN.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&Server.MapPath("new.mdb")
Set RS=Server.CreateObject("ADODB.Recordset")
RS.Open "new",CN,1,3
SQL="delete * From new where id="&id
cn.execute sql
response.redirect("index.asp")
%>

#################################
保持文本框格式一致的问题:

<%
function th(str)
str=replace(str,"妈的","MD")
str=replace(str,"靠","KAO")
th=str
end function

function encode(str)
 str = replace(str, ">", "&gt;")
 str = replace(str, "<", "&lt;")
 str = Replace(str, CHR(32), "&nbsp;")
 str = Replace(str, CHR(13), "")
 str = Replace(str, CHR(10) & CHR(10), "</P><P>")
 str = Replace(str, CHR(10), "<BR>")
encode=str
end function
%>

<%title=request.form("title")
content=request.form("content")%>

文章标题:<%=th(title)%><hr>
文章内容:<%=encode(th(content))%>

 


#################################
技巧:
1、response.write chr(10)'输出一个换行符号
2、本页的文件名<%=request.servervariables("path_info")%>
3、with response
<%
with response
.write "this"
.write "is"
.write "a"
.write "test"
end with
%>


 

责任编辑: 龙舞天翔

相关文章
比较详细的Asp伪静态化方法及Asp静态化探讨 - 09-15 05:03 am - 点击: 221
利用ASP实现在线生成电话图片效果脚本 - 09-15 05:00 am - 点击: 207
asp函数Response.flush 实时显示进度 - 09-15 04:58 am - 点击: 199
[ASP]利用稻香老农的无组件上传类进行多文件上传 - 04-02 12:47 pm - 点击: 1986
asp 中的 “80020009” 错误 - 04-02 12:44 pm - 点击: 2075
ASP技巧:网页在线人数统计的做法 - 10-11 03:24 am - 点击: 1517
ASP中FSO的神奇功能 - 06-01 02:48 am - 点击: 1661
强大的数组功能(asp+程序数组功能调用) - 06-01 02:39 am - 点击: 1683
asp中利用数组实现数据库记录的批量录入方法 - 06-01 02:31 am - 点击: 1617
Asp函数介紹 - 05-31 09:09 am - 点击: 1599

发表评论 查看评论 加入收藏 Email给朋友 打印本文
如果你想对该文章评分, 请先登陆, 如果你仍未注册,请点击注册链接注册成为本站会员.
平均得分 0, 共 0 人评分
1 2 3 4 5 6 7 8 9 10
Copyright © 2002 -2003 龙舞天翔资源共享
All rights reserved.
Powered by: phpArticle Version 2.0

Warning: Unknown: open(/home/mybwtech/public_html/article/tmp/sess_eaa464f6f7d54616a011fa8c239d380f, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/mybwtech/public_html/article/tmp) in Unknown on line 0