接北京北大青鸟校区提供的上篇文章:
怎样使用工程和类
现在我们有了我们自己的工程(Example1)和类名(HelloWorld).以后我们就会在ASP代码中使用它们的名字来引用这个组件.在ASP中我们就这样引用,如下:
Set ObjReference = Server.CreateObject(ProjectName.ClassName)
对于我们工程的引用就是:
Set ObjReference = Server.CreateObject(Example1.HelloWorld)
现在我们就能用ObjReference来调用我们在组件中所创建的函数,子程序.下面我们会来写一个 SayHello的子程序, 我们执行它的代码如下: (北京北大青鸟校区)
〈%
Set ObjReference = Server.CreateObject(Example1.HelloWorld)
ObjReference.SayHello
%>
为了在Helloword类中使用ASP的方法,你必须在此类中写一个OnStartPage
子函数.如下:
Public Sub OnStartPage(PassedScriptingContext As ScriptingContext)
Set MyScriptingContext = PassedScriptingContext
End Sub
现在,无论什么时候用户访问一个带有本组件的ASP文件,IIS就会把ScriptingContext传送给我们的对象请我们使用.这个ScriptingContext包括了全部的ASP方法和属性.实现上,这使得我们有能力访问所有ASP的对象.看下面的代码: (北京北大青鸟校区)
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
以后我们就能用在VB中用MyApplication 来代替ASP中的Application,同理可以代替Request,Server.....,不过我们来是要在 OnStartPage之前来申明这些变量:
Private MyScriptingContext As ScriptingContext
Private MyApplication As Application
Private MyRequest As Request
Private MyResponse As Response
Private MyServer As Server
Private MySession As Session
使用ASP的对象
我们的变量现在就能像标准的ASP对象来使用了!比如,我们经常在ASP中用Request.form()来收集提交表单的数据.现在我们在我们的VB中实现这个功能,代码如下:
用ASP中实现:
〈%
MyTempVariable = Request.Form(userName)
Response.Write (you entered & MyTempVariable & as your user name)
%>
在VB中实现:
MyTempVariable = MyRequest.Form(userName)
MyResponse.Write (you entered & MyTempVariable & as your user name)
通过使用MyResponse来代替Response,我们能够使用所有Response的方法,当然,MyResponse这个名字可以随便来取,你甚至可以就取Response. (北京北大青鸟校区提供)