Abstract: This article introduces the method of dynamically including ASP files and making ASP classes (Class) instantiable in ASP.
In ASP, include/virtual is preferred for script code processing, so include cannot be used to dynamically include ASP files. We can use the Execute function to execute the required code dynamically.
method:
Execute(ASP code)
Example: (vbCrLf is a newline character)
The code copy is as follows:
Execute("ClassclsAbc"&vbCrLf&"PublicFunctionoutput"&vbCrLf&"Response.Write123"&vbCrLf&"EndFunction"&vbCrLf&"EndClass")
The code copy is as follows:
DimobjAbc
SetobjAbc=NewclsAbc
objAbc.output
SetobjAbc=Nothing
When using it, you can use the ASP code to read out from the file or database and then execute it. Note that the executed code should not contain <% and %>
Be careful not to be confused with Server.Execute. The Server.Execute parameter is an ASP virtual path. When using this function, not only can the Class class be declared dynamically, but it cannot even assign values to the variables of the main program segment.
example:
main.asp
The code copy is as follows:
DimstrAbc,objAbc
strAbc="Test"
Server.Execute("sub.asp")
Response.WritesstrAbc
SetobjAbc=NewclsAbc
objAbc.output
SetobjAbc=Nothing
sub.asp
The code copy is as follows:
strAbc="Execute"
ClassclsAbc
PublicFunctionoutput
Response.Write"Class"
EndFunction
EndClass
After main.asp is executed, only Test will be output, while objAbc cannot be instantiated.