Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Include Pages 2

Status
Not open for further replies.

walderr

Programmer
Jul 18, 2003
32
GB
After thinking it was impossible to include .asp pages dynamically, I've managed to crack a way of doing it (this may be well known, but I thought it was good anyway!)

On the main page you wish to view, use the function
Server.Execute "Folder/"&Variable&".asp"
This will include the relevant file.

On the page you are including, if you wish to execute asp within it, you can use the following method under some circumstances:
Make sure that the variable is taken to the main page in a query string, ie the address off the main page will be in the format mainpage.asp?my_var=<%=my_var%>

On the included page, use
QueryString = Request.ServerVariables(&quot;QUERY_STRING&quot;)
This takes the variables from the url of the main page (eg my_var=<%=my_var%> in the above example). You can then chop this up so that the particular variable you want is taken for use in the include page (eg my_var).

In short:

MAIN-------------------------------
mainpage.asp?my_var=<%=my_var%>
Server.Execute &quot;Folder/&quot;&Variable&&quot;.asp&quot;

INCLUDE----------------------------
QueryString = Request.ServerVariables(&quot;QUERY_STRING&quot;)
Cut = split(QueryString,&quot;=&quot;)
Paste = ubound(Cut) - lbound(Cut) + 1
my_var = (Cut(Paste-1))
 
the long old trade off of includes. but in actuality the execute method is different then a include. comparisons to a sub or function call would be better in a respect to a execute although bringing in a SSI is not. these two are virtually the same effects but as we all know the lack of response you get form a SSI. also the execute is less resource heavy on your server!

star for the tip walderr

might I suggest a FAQ on the topic

it's asked far too often

_____________________________________________________________________
[sub]Where have all my friends gone to????[/sub]
onpnt2.gif

 
I agree with onpnt. A FAQ is in order. Until then I'm emailing this one to myself for safe keeping. Nice walderr! * ;-)
 
Thanks for the compliments! I've submitted it for the FAQs now, and modified my comments slightly to reflect your comments that it isn't technically an include, onpnt. Have fun with it!
 
note also that Server.Execute is available only in IIS5+, not to us that still use NT IIS4.

another point is that Server.Execute executes in a new context, so any variables you might have initialized on the calling page will not be available to the executed page, like they would be with #include.



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
One way around the variables issue (I think) would be to have an intermediate page to carry out the initial ASP initialisations, then use response.redirect to open the actual mainpage.asp, and have the variables in the querystring, eg:

Page1: Submit a form

Page2: Process the data and use response.redirect mainpage.asp?var1=<%=var1%>&var2=<%=var2%> etc. the user would not need to see this page

Page3:mainpage.asp with includedpage.asp executed within it using the method above. All of the variables would then be available to includepage.asp by chopping the value returned by Request.ServerVariables(&quot;QUERY_STRING&quot;).

Still doesn't solve your IIS problem I'm afraid, but I'm not up on that sort of thing I'm afraid. Anyway, it's a possible work around!
 
walderr,

good idea about passing the vars in the querystring but that won't always work. for example, if you create a scripting.dictionary or some other object that you would like to use later in the page's life.

storing them in the session object might not work either - i know that you cannot persist your own custom vbscript objects in session due to the &quot;apartment threading model&quot; that asp uses...i haven't tried tried this method though to just share across includes.

anyway, not trying to give you a hard time - just playing devil's advocate!



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
No, sure, it's useful to point out potential pitfalls to other users! I was just quite pleased with myself that I'd managed to find a potential alternative (in some cases) after being told that it was a lost cause, wherever I looked on the net, especially as I've only been doing this for about two months! But hopefully this will be of use to some people where they would have otherwise given up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top