Fog Creek Software
g
Discussion Board




ASP dynamic include problem

I have searched Google for this and haven't found any satisfactoy answer.
I have an ASP file and I want to display contents of some other ASP files in it.
I know you can't do
<!--#include file="../include/somefile.asp?id=<%=value%>"-->
I tried the FileSystemObject and OpenTextFile etc. But I can't seem to pass the file as "somfile.asp?id=<%=value%>" and Server.Execute doesn't work either.
What other ways can I do it? Is it possible at all to include an asp file with Query Starings passed to it?
Any help, pointers are greately appreciated. Thanks in advance.

Anon
Wednesday, November 26, 2003

Depending on what you are trying to do, maybe:

(a) use an IFRAME
(b) build functions to pass that data around and then output what you want

Ankur
Wednesday, November 26, 2003

use server.execute instead of include, includes are happen before running the code (well, not exactly but sort of).

%<
server.execute "myfile.asp?param=" & cstr(thisValue)
%>

n/a
Wednesday, November 26, 2003

Thanks for suggestions so far.
I am looking into <IFRAME>, but it may not be suitable as this web application is being accessed using old browsers too.
I have been trying with different functions, but ultimately I have to call this asp file as "somefile.asp?id<%=value%>" and include the contents in another asp file. So I don't know it is possible. But I am still working on it.
Server.Execute doesn't work. It gives me an error of
"Invalid URL form or fully-qualified absolute URL was used. Use relative URLs". And I have tried to pass the file in diffrent ways, but if I include the query string parameters, I get that same error.
Thanks for the help so far.

Anon
Wednesday, November 26, 2003

And I don't know if Server.Execute can contain a Query String parameter.

Anon
Wednesday, November 26, 2003

Have you checked the MSDN documentation on Server.Execute at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iisref/htm/ref_vbom_seromexe.asp

It says that queryStrings cannot be passed with Server.Execute but all of the current request context (session, request, response etc) is availble during execution of the called page.

R1ch
Wednesday, November 26, 2003

Don't use querystring arguments.

Since the include directive essentially pastes the code of the included file into your current file, the code can see variables from the main page. You could declare and assign a variable in the main page and reference in your include file.

<%
Dim MyParam
MyParam = "Passed argument"
%>
<!--#include file="../include/somefile.asp"-->

Hector
Wednesday, November 26, 2003

I did check the MSDN documentation on Server.Execute and Query Strings. I am trying the approach of setting the varables in the main file. Thanks for all your inputs so far.

Anon
Wednesday, November 26, 2003

Thanks for the help so far.
I can do this with Server.Execute and pass the parameters by defining application variable for e.g Application("var") = "value" in the main file, and using that application variable in the called file.
Thank you once again.

Anon
Wednesday, November 26, 2003

Be very careful using the Application object that way - it has application wide scope so you could get concurrency problems which would cause values to be transfered between requests - for example, if you page 'a.asp' executing page 'b.csp' and passing values in 'var' :-

user1 requests a.asp and it sets 'var' to '1234'
user2 requests a.asp and it sets 'var' to '5678'
b.asp is processed for user1 ('var'='5678')
response is returned to user1
b.asp is processed for user2 ('var'='5678')
response is returned to user2

Instead you should be able to access variables locally declared in a.csp in b.csp - these will have request scope and so concurrency shouldn't cause problems.

r1ch
Wednesday, November 26, 2003

I had an application in ASP once that need to dynamically include code. It's not actually possible to do that in ASP as the include happens before the ASP code is interpretted.

The solution I used successfully was to use a kind of callback system. It's pretty complicated and long winded. If you are interested I will post the details here.

Matthew Lock
Wednesday, November 26, 2003

You are trying to use an ASP page like a function and its querystring as parameters, so rather put your code into a function (or many) and call that function.

Maybe that doesn't make sense for your implementation, can you explain in more detail what "somefile.asp" does?

The iframe solution might work, but it is best used when you want to do some server side stuff without reloading the page client-side.

Gavin van Lelyveld
Thursday, November 27, 2003

http://www.asp101.com/articles/michael/dynamicincludes/default.asp

Cheers!

EastIndian
Friday, November 28, 2003

Thanks for all the input so far.
rich, I thought about this problem later on. What if I use the session varibles instead of application variables?
Mathew, I certainly would be interested in that solution. Thanks.
somefile.asp gets data from database and displays dynamically. It's being called from multiple asp pages and I don't want to create different versions of somefile.asp. Hence I need to include it dynamically.
Thank you once again.

Anon
Monday, December 1, 2003

Anon - the session variables would be fine, but if your application could be stateless otherwise you're paying a slight performance/scalability penalty.  Is there some reason that you can't use locally declared variables as Hector suggested?

r1ch
Tuesday, December 2, 2003

I should probably be more specific there - session variables would be fine as long as no user made two concurrent requests, like could happen if an ASP page was serving up images or frames etc.

r1ch
Tuesday, December 2, 2003

rich,
Thank you very much for the help.
I am using Server.Execute and I beleive it's not possible to pass the locally declared variables with it.
I don't have any images or frames. It's just text information read from the database. And session variables seem to work fine. I will test it more though.
Thank you once again.

Anon
Tuesday, December 2, 2003

Page-scoped variables are NOT passed to the server.executed file.

HEXtaC
Thursday, February 26, 2004

*  Recent Topics

*  Fog Creek Home