现在我们就能用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.
另一件我们得注意的是,我们得在我们的建立的类中,写上OnEndPage子函数,这个OnStartPage是相反的!OnStartPage是创建对象,OnEndPage是消毁对象.









