1. 用JavaScript实现URL重定向:
window.location.href='index.asp'
2.检查form表单中的元素都已填写;
<script language="javascript">
function Mycheck(form){//传过来的表单名
for(i=0;i<form.length;i++){//length为表单元素个数
if(form.elements[i].value==""){//用element定向指定的表单元素
alert(form.elements[i].name + "不能为空!");return false;//根据element的name属性得到名称
}
}
}
</script>
3.在asp中使用分页技术;
Set rs=Server.CreateObject("ADODB.Recordset")
sqlstr="select * from tab_goods order by id desc"
rs.open sqlstr,conn,1,1
If Not (rs.eof and rs.bof) Then
rs.pagesize=12 '定义每页显示的记录数
pages=clng(Request("pages")) '获得当前页数
If pages<1 Then pages=1
If pages>rs.recordcount Then pages=rs.recordcount
showpage rs,pages '执行分页子程序showpage
Sub showpage(rs,pages) '分页子程序showpage(rs,pages)
rs.absolutepage=pages '指定指针所在的当前位置
For i=1 to rs.pagesize '循环显示记录集中的记录
%>
rs.movenext '指针向下移动
If rs.eof Then exit for
Next
End Sub
%>
<%
If rs.recordcount > rs.pagesize Then
if pages<>1 then
response.Write(" <a href="&path&"?pages=1&sclassid="&sclassid&"&action="&action&">首页</a>")
response.Write(" <a href="&path&"?pages="&(pages-1)&"&sclassid="&sclassid&"&action="&action&">上一页</a>")
end if
response.Write(" 当前 <font color='#FF0000'>"&pages&"/"&rs.pagecount&"</font> 页")
if pages<>rs.pagecount then
response.Write(" <a href="&path&"?pages="&(pages+1)&"&sclassid="&sclassid&"&action="&action&">下一页</a>")
response.Write(" <a href="&path&"?pages="&rs.pagecount&"&sclassid="&sclassid&"&action="&action&">末页</a>")
end if
rs.close
Set rs=Nothing
End If
4.读取数据库中的图片信息显示
<img src="Img.asp?id=<%=rs("id")%> height="100" width="125" >
img.asp代码如下:
Set rs=Server.CreateObject("ADODB.Recordset")
id=Request.QueryString("id") '图片的唯一标识
sqlstr="select Gpicture from tab_goods where id="&id&""
rs.open sqlstr,conn,1,3
Response.ContentType="image/*"
Response.BinaryWrite rs("Gpicture").getChunk(8000000)
rs.close
Set rs=Nothing
5.退出登录
session.Abandon() '把session中的数据全部放弃
response.Redirect("index.asp") '重新定向到主页面
6.返回上次登录的界面
<script language="javascript">
history.back(1);
</script>
7.asp中的select case,根据条件引用不同的文件
<%
action=request.QueryString("action")
Select case action
case "file_edit"
%>
<!--#include file="User_edit.asp"-->
<%case "pwd_edit"%>
<!--#include file="User_passwd.asp"-->
<%case "order"%>
<!--#include file="User_order_query.asp"-->
<%case "consume"%>
<!--#include file="User_consume.asp"-->
<%End Select%>
8.关于meta参加meta百科
9.防sql注入
dim SQL_Injdata
SQL_Injdata = "'|;|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare"
SQL_inj = split(SQL_Injdata,"|")
If Request.QueryString<>"" Then
For Each SQL_Get In Request.QueryString
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.QueryString(SQL_Get),Sql_Inj(Sql_Data))>0 Then
Response.Redirect("../index.asp")
end if
next
Next
End If
10.连接数据库
Dim conn,connstr
Set conn=Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;User ID=admin;Password=;Data Source="&Server.MapPath("DataBase/db_name.mdb")&";"
conn.open connstr
11.定义转换字符函数
<%
Function Str_filter(InString)
NewStr=Replace(InString,"'","''")
NewStr=Replace(NewStr,"<","<")
NewStr=Replace(NewStr,">",">")
NewStr=Replace(NewStr,"chr(60)","<")
NewStr=Replace(NewStr,"chr(37)",">")
NewStr=Replace(NewStr,"""",""")
NewStr=Replace(NewStr,";",";;")
NewStr=Replace(NewStr,"--","-")
NewStr=Replace(NewStr,"/*"," ")
NewStr=Replace(NewStr,"%"," ")
Str_filter=NewStr
End Function
%>
12.根据时间获取的字符串
<%
Function GetOrderNo(dDate)
GetOrderNo = RIGHT("0000"+Trim(Year(dDate)),4)+RIGHT("00"+Trim(Month(dDate)),2)+RIGHT("00"+Trim(Day(dDate)),2)+RIGHT("00" + Trim(Hour(dDate)),2)+RIGHT("00"+Trim(Minute(dDate)),2)+RIGHT("00"+Trim(Second(dDate)),2)
End Function
%>
13.获取随机数函数
<%
Function randStr(num)
strings="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
str=split(strings,",")
for i=1 to num
Randomize
str1=str(int((ubound(str))*rnd))
returnstr=returnstr&str1
next
ranStr=returnstr
End Function
%>
14.验证数字
<script language="javascript">
function checkNum(Num){
var Expression=/^[1-9]+[0-9]*/d$/;
var re=new RegExp(Expression);
if(re.test(Num)==true){
return true;}
else{
return false;}
}
</script>
15.验证E-mail地址
<script language="javascript">
function checkEmail(email){
var Expression=//w+([-+.']/w+)*/./w+([-.]/w+)*/;
var re=new RegExp(Expression);
if(re.test(email)==true){
return true;}
else{
return false;}
}
</script>
16.验证网址
<script language="javascript">
function checkUrl(url){
var Expression=/http(s)?:([/w-]+/.)+[/w-]+(//[/w-.//?%&=]*)?/;
var re=new RegExp(Expression);
if(re.test(url)==true){
return true;}
else{
return false;}
}
</script>
17.验证身份证号码
<script language="javascript">
function checkCode(code) {
var Expression=//d{17}[/d|X]|/d{15}/;
var re=new RegExp(Expression);
if(re.test(code)==true){
return true;}
else{
return false;}
}
</script>
18.使用框架结构
<frameset rows="93,*,25" frameborder="NO" border="0" framespacing="0">
<frame src="frame_top.asp" name="topFrame" scrolling="NO" noresize>
<frameset cols="220,*" frameborder="NO" border="1" framespacing="0">
<frame src="mg_left.asp" name="leftFrame" scrolling="no" noresize>
<frame src="frame_right.asp" name="mainFrame">
</frameset>
<frame src="frame_bottom.asp" name="bottomFrame" scrolling="NO" noresize>
</frameset>
<noframes>
<body>
</body>
</noframes>
19.从数据库中直接执行删除操作
id=Request.Form("id")
sqlstr="delete from tab_lword where id="&id&""
conn.Execute(sqlstr)
Response.Redirect("mg_lword.asp")
20.从数据中直接执行插入操作
str1=Str_filter(Request.Form("txt_name"))
str2=Str_filter(Request.Form("txt_passwd"))
sqlstr="insert into tab_manager(Mname,Mpasswd) values('"&str1&"','"&str2&"')"
conn.Execute(sqlstr)
Response.Redirect("mg_manager.asp")
21.从数据库中直接执行修改操作
str1=Str_filter(Request.Form("txt_name2"))
str2=Str_filter(Request.Form("txt_passwd2"))
id=Request.Form("id")
sqlstr="update tab_manager set Mname='"&str1&"',Mpasswd='"&str2&"' where id="&id&""
conn.Execute(sqlstr)
Response.Redirect("mg_manager.asp")
22.实现打印功能
<style type="text/css">
@media print{
.print{display:none /*应用该样式的对象将不可见*/
}
</style>
<body>
<object id=WebBrowser classid=ClSID:8856F961-340A-11D0-A96B-00C04Fd705A2 width="0" height="0"></object>
<table>打印的内容</table>
<a href="#" onClick="document.all.WebBrowser.Execwb(7,1)" class="print">打印预览</a>
<a href="#" onClick="document.all.WebBrowser.Execwb(6,1)" class="print">打印</a>
<a href="#" onClick="document.all.WebBrowser.Execwb(6,6)" class="print">直接打印</a>
<a href="#" onClick="document.all.WebBrowser.Execwb(8,1)" class="print">页面设置</a>
23.上传图片文件
<script language="javascript">
function mycheck()
{
if(formup.file1.value=="")
{alert("商品的图片信息不能为空!");formup.file1.focus();return;false}
formup.submit();
}
</script>
<table width="452" height="27" border="0" align="center" cellpadding="0" cellspacing="0">
<form name="formup" method="post" action="UpLoad.asp" enctype="multipart/form-data">
<!--必须为 enctype="multipart/form-data"格式-->
<tr align="center" valign="middle">
<td align="left" id="upid" height="20" width="400" bgcolor="#FFFFFF">
<input name="file1" type="file" class="tx1" style="width:200" value="" size="30">
<input type="button" name="Submit" value="上传" onClick="mycheck();">
<span class="STYLE2">(注意:先进行图片上传) </span></td>
</tr>
</form>
Upload.asp文件 先将图片信息保存在一个session中,当点击总体提交时再把图片信息写入数据库
<%
'将图片保存到数据库中
imgsize=request.TotalBytes
imgData=request.BinaryRead(imgsize)
Hcrlf=chrB(13)&chrB(10)
Divider=leftB(imgdata,clng(instrB(imgData,Hcrlf))-1)
dstart=instrB(imgData,chrB(13)&chrB(10)&chrB(13)&chrB(10))+4
Dend=instrB(dstart+1,imgdata,divider)-dstart
Mydata=MidB(imgdata,dstart,dend)
Session("pic")=Mydata
%>
</table>