Link to home
Start Free TrialLog in
Avatar of swinslow
swinslowFlag for United States of America

asked on

Dynamic Include

I know this one should be simple, but I need a little help .......

I have some links that actually redirect to the same page with a querystring. What I am trying to do is read the querystring then include that file. Here is the code that I am trying.

<%
 IF Request.queryString("query") <> "" Then
 %>
    <!-- #include file="<% reponse.write Request.QueryString("query") %>" -->
 <%
  End If
%>

I have also tried ,,,,.

<%
 IF Request.queryString("query") <> "" Then
     Respnse.Write "<!-- #include file='" & <% reponse.write Request.queryString("query") & "' %> -->"
  End If
%>


Soren
     - Working hard at being lazy
ASKER CERTIFIED SOLUTION
Avatar of phuctran
phuctran

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phuctran
phuctran

You can have something like:
1. multi pages have links to page A.asp
2. in A.asp you have
   depends on QueryString("inc")
   Case 1: Redirect to page A1.asp (which use A1.inc)
   Case 2: Redirect to page A1.asp (which use A2.inc)
....

But it will be difficult to maintain later.  You have to make changes in all A1.asp, A2.asp, ...
What you can do is Server.Execute("my.asp?" & querystring).
the difference between this and #include is that in Execute, you cannot have variables defined in parent page be seen in the "executed" page and vise versa.
I am not sure if you can pass parameters to Server.Execute... Anyways, Microsoft already documented that you cannot place ASP/script codes inside an <!-- #include directive. You'll have to re-structure your approach to solve your project requirements.
Look at the FileSystemObject.

If you're trying to pull down text files (as apposed to other ASP pages that need processing), you could use that.

Assume you wanted to include this file:

<!--#include file="people/stats/person1.html"-->

And this is basically some HTML info about person1.

You could use the FSO do get this data:

<%
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextStream(Server.MapPath(Request.QueryString("query")),forReading)
Response.Write objFile.ReadAll
%>
swinslow,

To include dynamic asp page
<%
 Const ForReading = 1, ForWriting = 2
 
 includefile = Server.MapPath("excutein.asp")
 Dim fso, f
 Set fso = CreateObject("Scripting.FileSystemObject")
 Set f = fso.OpenTextFile(includefile, ForReading)
 ReadAllTextFile =  f.ReadAll

 Execute (REadAllTextFile)
%>

This is excutein.asp :

Response.Write("Why") & "<br>"
Response.Write("My") & "<br>"
Response.Write("Love") & "<br>"
Response.Write("No") & "<br>"
Response.Write("Like") & "<br>"
Response.Write("Me") & "<br>"

Regards,
Wee Siong

errm swinslow, did you at all read mine and Wee Siong's suggestions? I find it hard that you've marked phuctran's solution, when it wasn't one. True, his statement was accurate, but we've provided alternatives that will do the same thing?
Avatar of swinslow

ASKER

Maybe you are right and I appologize to all for that. But, the code I am trying to include does do more server side scripting and queries. FSO does do the include, but it completely ignores any server side coding. It will only read the oure HTML.

Soren