Include| News
Let's start with one case:
<%
' Get the province where the user is
Dim Province
Province = Request.Form ("province")
' The information in this province is shown below
%>
<!--#include virtual= "/province/<%= province%>"-->
Do you think the code above can work? No! Simply use ASP to achieve dynamic inclusion, and you cannot achieve the desired effect. Why? Because: the include command is executed before the ASP code, so the above code does not follow the author's wishes, first get the user's province, and then contain the information of the province!
If you really need dynamic inclusion, you can do this:
<%
Select Case Province
Case 1:%>
<!--#include file= "1.asp" à
<% Case 2:%>
<!--#include file= "2.asp" à
<% Case 3:%>
<!--#include file= "3.asp" à
<% End Select%>
It should be said that this piece of code can get the results you want. However, since your users may be from 33 provinces, do you include 33 files? In particular, SSInc.dll is not sure which file you need (in fact, province has no value at this time), so she included all the files! You can imagine how big the file is! Then, ASP. DLL will scan the ASP code in this file and execute it! So, whenever this happens, you should consider other ideas, such as databases, or use FileSystemObject.
How do I include files dynamically?
Answer
One of the biggest challenges that ASP programmers often face is dynamic include files. Because #include is handled before the ASP code executes, it seems impossible to move If/else's brain. Is that so?
Depending on the purpose of your use of include and the number of files you will include, using if/else may solve the problem. But it's definitely not going to work anytime, and it's not an effective solution because you need to do a lot of manual work.
Suppose there are two sample HTM files, 1.htm and 2.htm, for simplicity, suppose the contents of the file are as follows:
<!-- 1.htm: -->
<font color= #ff0000 >this is 1.htm</font>
<!-- 2.htm: -->
<font color= #0000ff >this is 2.htm</font>
Now let's try dynamic include:
<%
if Request.QueryString (' param ') = ' 2 ' then
%>
<!--#include file= ' 2.htm '--> &NBSP
<%
else
%>
<!--#include file= ' 1.htm '-->
<% &NBSP
end if
%>
Note that the top two #include are actually processed. You can actually run it and see the effect:
http://localhost/Test.asp?param=1
http://localhost/Test.asp?param=2
http://localhost/Test.asp
Above we are to put a querystring as a condition. You can also use the time, date, browser version, etc. as a condition. However, the more complex the conditions, the less efficient this method is. Here's another idea:
<%
If request (' param ') = ' 2 ' Then
filespec = ' 2.htm '
Else
filespec = ' 1.htm '
End If
filespec = Server.MapPath (filespec)
SCR = ' Scripting.FileSystemObject '
Set fs = Server.CreateObject (SCR)
Set F = Fs.opentextfile (filespec)
Content = F.readall
Set F = Nothing
Set fs = Nothing
Response.Write (content)
%>
In iis5.0/asp3.0, there are two new ways to support dynamic inclusion:
<%
Server.Transfer filename
Server.Execute filename
%>
If it's just IIS5.0 and ASP3.0, then ok! But IIS5.0 needs to run on Windows 2000.