asp可以像.net那样把代码进行封装,感觉很爽.下面把asp封装的问题给大家写一个比较简单和实用的出来,便于大家上手,当然首页要准备的最重要的是vb工具了我用vb6.0。
启动你的VB,选择ActiveX图标.这个图标可以在新建工程找到!VB会提供一个默认的工程名(project1)和类名(class1).我们会将这两个名字都改掉.在改名之前,请首先确认我们拥有Microsoft Active Server Pages Object Library,它在我们的程序非常有用.从菜单中选择"工程",然后在其中选择"引用",就会出现"引用"窗口
从中选择Microsoft Active Server Pages Object Library.
首先在vb中建一个类文件:工程名:exp
代码:
Dim MyApplication As Application
Dim MyRequest As Request
Dim MyResponse As Response
Dim MyServer As Server
Dim MySession As Session
Public Sub OnStartPage(PassedScriptingContext As ScriptingContext)
Set MyScriptingContext = PassedScriptingContext
Set MyApplication = MyScriptingContext.Application
Set MyRequest = MyScriptingContext.Request
Set MyResponse = MyScriptingContext.Response
Set MyServer = MyScriptingContext.Server
Set MySession = MyScriptingContext.Session
End Sub
Public Sub OnEndPage()
Set MyScriptingContext = Nothing
Set MyApplication = Nothing
Set MyRequest = Nothing
Set MyResponse = Nothing
Set MyServer = Nothing
Set MySession = Nothing
End Sub
Public Sub hellow()
MyResponse.Write ("Hello World")
End Sub
Public Sub google()
MyResponse.Write ("ssssssssssss")
End Sub
说明:ScriptingContext传送给我们的对象请我们使用.这个ScriptingContext包括了全部的ASP方法和属性.实现上,这使得我们有能力访问所有ASP的对象
当然这里还有一步就是生成exp.dll文件了,生成之后就可以用 regsvr32 d:/vb-asp/exp.dll 注册了.(很生要)
asp页面:
<%@ Language=VBScript%>
<HTML>
<HEAD>
<TITLE>Example 1</TITLE>
</HEAD> <BODY>
<%
dim Obj
Set Obj = Server.CreateObject("exp.aspdll")
Obj.hellow
response.write("<br>")
Obj.google
%>
</BODY>
</HTML>
说明:
Set Obj = Server.CreateObject("exp.aspdll") 其中,exp为工程名,aspdll为类名.Set Obj = Server.CreateObject("工程名.类名称")
测试通过.
===========================================================
对于更复杂的运用,大家可以通过这个实例向外扩展就可以了.
如:
Public Sub connstr2()
Set conn = MyServer.CreateObject("ADODB.Connection")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & MyServer.MapPath("codata.mdb")
Set rs = conn.Execute("select * from news")
Do While Not rs.EOF
MyResponse.Write (rs("news_title") & "<br>")
rs.MoveNext
Loop
rs.Close
Set conn = Nothing
End Sub
这个是用数据库连接的代码封装,当然这里要添加ADO引用的