首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>ASP教程>资讯:ASP常用代码段之十

ASP常用代码段之十

www.jz123.cn  2010-09-29   来源:   中国建站    责任编辑(袁袁)    我要投递新闻

 1:长文章自动分页类

'原创的长文章自动分页类
Class AutoPaging
   
'*************************************************************
    '目   的:自动将长文章分页输出
    '属   性:Splitchar:分页搜索标志。以一维数组方式输入,默认array("<br>","<BR>","<Br>","<bR>")
    '      PagingString:要进行分页的字符串
    '      PagingSize:分页长度
    '      CurrentPage:当前输出页码
    '      DefaultRange:默认搜索分页标志范围
    '      StepRange:搜索步长
    '方   法:getTotalPageCount():返回总分页数
    '      OutputString:输出当前分页内容
    '*************************************************************
    public Splitchar                '分页搜索标志,数组
    public PagingString            '分页字符串
    public PagingSize                '页面最大长度
    public CurrentPage            '当前页码
    public DefaultRange            '默认起始范围
    public StepRange                '步长
    private Truncate                '2维数组m,n。m0=页码、m1=起始字符、m2=结束字符,n=总页数
    private intStart                '开始位置
    private intLen                    '截取长度
    private tmpStr
   
private Range                    '起始范围

   
private Sub Class_Initialize()
        Splitchar
=array("<br>","<BR>","<Br>","<bR>")
        PagingSize
= 2000
        DefaultRange
=1
        StepRange
= 1
        intStart
= 0
        intEnd
= 0
   
End Sub
   
   
public Function getTotalPageCount()                        '返回总页数
        if len(PagingString) mod PagingSize > 0 then
            getTotalPageCount
= int(len(PagingString) / PagingSize) + 1
       
else
            getTotalPageCount
= len(PagingString) / PagingSize
       
end if
   
End Function

   
public Sub OutputString
       
redim Truncate(2,getTotalPageCount())

       
For tmpI = 0 to getTotalPageCount()
            Truncate(
0,tmpI)=tmpI
       
Next

       
For tmpI = 1 to ubound(Truncate,2)
           
if Truncate(0,tmpI) = 1 then
                Truncate(
1,tmpI) = 1
           
else
                Truncate(
1,tmpI)=Truncate(1,tmpI - 1) + Truncate(2,tmpI - 1)
           
end if
            Range
= DefaultRange
           
Do while Range < PagingSize
                tmpStr
=right(mid(PagingString,Truncate(1,tmpI),PagingSize),Range)
               
For tmpInt=0 to ubound(Splitchar)
                   
if instr(1,tmpStr,Splitchar(tmpInt)) > 0 then
                        Truncate(
2,tmpI) = (PagingSize - (len(tmpStr) - instr(1,tmpStr,Splitchar(tmpInt)))) - 1
                       
exit do
                   
end if
               
Next
                Range
= Range + StepRange
           
Loop
           
if tmpI = Cint(CurrentPage) then
                intStart
= Truncate(1,tmpI)
                intLen
= Truncate(2,tmpI)
           
end if
       
next
       
if Cint(CurrentPage) = getTotalPageCount() then
            response.write(
mid(PagingString,intStart))            '如果当前分页为最后一页,则取剩下的所有字符
        else
            response.write(
mid(PagingString,intStart,intLen))   
       
end if
   
End Sub
End Class

  2:asp登陆窗口代码,含数据库的连接

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
dim conn
set conn=Server.createobject("adodb.connection")
conn.open "provider=microsoft.jet.oledb.4.0;data source="&server.mappath("jixie.mdb")
If err.number<>0 then
Response.write("连接数据库时产生错误!")
Response.End
End if
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数控机床结构设计系统</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 36px}
.STYLE2 {font-size: 36px; color: #FF0000; }
.STYLE6 {
font-size: 24px;
color: #000000;
}
.STYLE8 {font-size: 14px}
.STYLE9 {font-size: 16px; }
body {
background-image: url(image/001.jpg);
}
-->
</style>
</head>

<body >

 
<label>

<form id="form1" name="form1" method="post" action="">
  <div align="center">
  <h1 class="STYLE1"><br />
  <br />
  </h1>
  <h1 class="STYLE2"><span class="STYLE6">数控机床结构设计系统</span><br />
  <br />
  <span class="STYLE8">*您没登陆,不具有任何操作权限,请先登陆!</span></h1>
  </div>
</form>
<div align="center"><br />
  <form id="form3" name="form3" method="post" action="">
  <p class="STYLE9">用户名
  <input name="user" type="text" id="user" />
  </p>
  <p class="STYLE9">密 码
  <input name="pass" type="password" id="pass" />
  </p>
  <p class="STYLE9">
  <input type="submit" name="Submit" value="提交" />  
  <input type="reset" name="reset" value="重置" />
  </p>
  </form>
</div>
</label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<div align="center">
  <%
dim user,pass,power
user=request("user")
pass=request("pass")
power=request("power")
if user="" or pass="" then
response.write "用户名或密码不能为空!"
response.end
end if
if power=0 then
set rs=conn.execute("select * from dljm where name='"&user&"' and password='"&pass&"'")
else
response.write "你的密码或用户名有误,请重新登陆"
end if
%>
  <%
if not rs.eof then
if power=0 then
session("name")=user
response.redirect"1.asp"
end if
else
response.write "你的用户名或密码有误,请重新登陆"
response.end
end if

%>
</div>
</body>
</html>

    3:对非法图片的删除

lastpath="已经上传的图片路径"
sFile=server.mappath(lastpath)
set MyFile=server.CreateObject("Scripting.FileSystemObject")
set MyText=MyFile.OpenTextFile(sFile, 1) '读取文本文件
sTextAll=lcase(MyText.ReadAll)
MyText.close 
sStr=".getfolder,.createfolder,.deletefolder,.createdirectory,.deletedirectory,.saveas,wscript.shell,script,.encode.,重命名,修改,属性,文件,浏览器,新建,复制,成功,参数错误,服务器,空间,下载,http,create,delete,fso,set,select,execute,response"
sNoString=split(sStr,",") 
for i=0 to ubound(sNoString)
if instr(sTextAll,sNoString(i)) then
SET fs=server.CreateObject("Scripting.FileSystemObject")
if FS.FileExists(sFile) then
FS.DeleteFile(sFile) 
end if 
set fs=nothing 

response.Write "<script>alert('上传操作失败,非法图片'),window.history.go(-1);</script>"

   4:使用ASP重启服务器

大家知道直接使用ASP是不能够重启服务器的,这时我们需要制作一个组件来实现功能,ASP通过这个组件调用系统API,然后按照不同的重启和关机方式进行操作!
         
          下面先说COM的制作,在VB中新建一工程,当然是AceiveX   dll的!
          1)先修改工程属性,在工程属性窗口将工程名称改为system,在类模块窗口将模块名称改为contral,保存工程;
          2)然后添加一个模块,用来声明需要使用的API和常数!下面是模块中的内容!
          Declare   Function   ExitWindowsEx   Lib   "user32 "   (ByVal   uFlags   As   Long,   ByVal   dwReserved   As   Long)   As   Long
          3)在类模块中输入下列代码:
          Public   Function   reboot(atype   As   Variant)
          t&   =   ExitWindowsEx(1,   atype)
          End   Function
          该函数需要传入一个参数,用来控制启动方式,这个参数是从ASP程序中传入的,等一下就可以看到具体的使用方法!
         
          编写完上面的代码,保存工程,将工程编译为system.dll。下面就写ASP程序!
         
          ASP中的代码非常简单:
          <%
          atype=request( "atype ")  
          set   sys=server.CreateObject   ( "system.contral ")  
          sys.reboot(atype)
          %>
          其中atype可以使用0、1、2、4,0、1和4均为关机,2用来重启。写完后执行ASP就可以了

 

  5:asp自动生成html并覆盖原html

<%
dim read,Curl,content




Function getHTTPPage(url)
  dim http
  set http=Server.createobject("Microsoft.XMLHTTP")
  Http.open "GET",url,false
  Http.send()
  if Http.readystate<>4 then
    exit function
  end if
  getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
  set http=nothing
  if err.number<>0 then err.Clear
End function

Function BytesToBstr(body,Cset)
  dim objstream
  set objstream = Server.CreateObject("adodb.stream")
  objstream.Type = 1
  objstream.Mode =3
  objstream.Open
  objstream.Write body
  objstream.Position = 0
  objstream.Type = 2
  objstream.Charset = Cset
  BytesToBstr = objstream.ReadText
  objstream.Close
  set objstream = nothing
End Function

Curl="http://localhost/index.asp"
read=getHTTPPage(Curl)

if read<>"" then
  content=read
  Set Fso = Server.CreateObject("Scripting.FileSystemObject")
  Filen=Server.MapPath("index.html")
  Set Site_Config=FSO.CreateTextFile(Filen,true, False)
  Site_Config.Write content
  Site_Config.Close
  Set Fso = Nothing
  Response.Write("<script>alert('已经成功生成首页!')</script>")
end if

%>

 

   6:asp生成html代码

<% 
function getHTTPPage(url) 
dim Http 
set Http=server.createobject("MSXML2.XMLHTTP") 
Http.open "GET",url,false 
Http.send() 
if Http.readystate<>4 then 
exit function 
end if 
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") 
set http=nothing 
if err.number<>0 then err.Clear 
end function 

Function BytesToBstr(body,Cset) 
dim objstream 
set objstream = Server.CreateObject("adodb.stream") 
objstream.Type = 1 
objstream.Mode =3 
objstream.Open 
objstream.Write body 
objstream.Position = 0 
objstream.Type = 2 
objstream.Charset = Cset 
BytesToBstr = objstream.ReadText 
objstream.Close 
set objstream = nothing 
End Function 

Dim Url,Html 
Url="http://www.jz123.cn
Html = getHTTPPage(Url) 

dim filename,MDBpath,fso,fout 
filename="index.htm" 
MDBpath="/" 
Set fso = Server.CreateObject("Scripting.FileSystemObject") 
Set fout = fso.CreateTextFile(server.mappath(""&filename&"")) 
fout.Write html 
fout.close 
set fout=nothing 
set fso=nothing 
if err then 
response.Write("生成首页失败") 
else 
response.Write("生成首页成功") 
end if 
%>


  7:asp站内搜索代码

body区:

<script   language= "JavaScript ">

<!--

//   下面是负责搜索的页面的全路径或相对路径

var   search_htm_url   =   "searchsource.htm ";


function   searchPage()   {

if   ((document.searchpage.keyword.value.length   ==   0)

||   (document.searchpage.keyword.value   ==   "   "))   {

alert( "请填入关键字! ");

}   else   {

sel   =   document.searchpage.and_or.selectedIndex;

location.href   =   search_htm_url   +   "? "

+   escape(document.searchpage.keyword.value)

+   (sel==0? "&and ":(sel==2? "&exact ": "&or "));

}

return   false;

}


document.write( ' <form   name= "searchpage "   onSubmit= "return   searchPage() "> '

+ '请填入关键字:   <input   type= "text "   size=22   name= "keyword ">   '

+ ' <input   type= "button "   value= "Search "   onClick= "searchPage() "> <br> '

+ ' <select   name= "and_or "   size=1> <option> 关键字与搜索 <option> 关键字或搜索   '

+ ' <option> 关键字异或搜索 </select> </form> ');

//   -->

</script>

  8:判断ASP页装载完毕

<script>
window.onload   =   function()
{
    alert( "Done ");
}
</script>


上一篇:ASP常用代码段之九 下一篇:ASP转化ACCESS为SQL数据库连接

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图