Discussion of dynamic Include files

Source: Internet
Author: User
Tags include
include| Dynamic We start with a 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 '-->
<%
Else
%>
<!--#include file= ' 1.htm '-->
<%
End If
%>
Please 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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.