By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

variable asp include

P: n/a
is there a way of having a file that's name is a variable (eg
dependant on the user name) act like a include.
i know that you cant define the file for an include asp tag using a
variable and that reading the file using "Response.Write
FSO.OpenTextFile(ppp, 1, False, False).readall" prints the file, and
treating it like a html file. can anyone suggest another way of doing
this?

Oct 15 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
http://www.devguru.com/Technologies/...r_execute.html

<j.********@find-a-part.comwrote in message news:11*********************@k35g2000prh.googlegro ups.com...
is there a way of having a file that's name is a variable (eg
dependant on the user name) act like a include.
i know that you cant define the file for an include asp tag using a
variable and that reading the file using "Response.Write
FSO.OpenTextFile(ppp, 1, False, False).readall" prints the file, and
treating it like a html file. can anyone suggest another way of doing
this?

Oct 15 '07 #2

P: n/a
j.********@find-a-part.com wrote:
is there a way of having a file that's name is a variable (eg
dependant on the user name) act like a include.
i know that you cant define the file for an include asp tag using a
variable and that reading the file using "Response.Write
FSO.OpenTextFile(ppp, 1, False, False).readall" prints the file, and
treating it like a html file. can anyone suggest another way of doing
this?
http://www.aspfaq.com/show.asp?id=2042
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Oct 15 '07 #3

P: n/a
Oops, didn't include a variable call to the function. Try this:

<html>
<body>
<p>Blah blah</p>
<%
sMyVar = Request.Querystring("var")

Select Case sMyVar
Case "1"
sMyFile = "file1.asp"
Case "2"
sMyFile = "file2.asp"
Case Else
sMyFile = "file.asp"
End Select

ExecInclude sMyFile
%>

<p>more blah</p>
</body>
</html>
Oct 19 '07 #4

P: n/a
j.********@find-a-part.com wrote on Mon, 15 Oct 2007 09:18:18 -0700:
is there a way of having a file that's name is a variable (eg dependant
on the user name) act like a include.
i know that you cant define the file for an include asp tag using a
variable and that reading the file using "Response.Write
FSO.OpenTextFile(ppp, 1, False, False).readall" prints the file, and
treating it like a html file. can anyone suggest another way of doing
this?
While Server.Execute is fine if the code in the "include" file is
self-contained, it won't work if you need to reference variables from the
"parent" code. You could pass all variables from the "parent" to the
"include" via the Session object, but with a large number of variables this
is impractical. Try Server.Execute and see if it'll fit your requirements,
if not then read on.

I had to do something similar recently and kludged a function together which
works so far with all the "include" files I've thrown at it - all my
"includes" are mixed ASP and HTML, and use variables from the "parent" page,
so Server.Execute and Server.Transfer were not suitable. My code isn't
pretty, and it's probably not very efficient either, but it works for me.
What this does is takes a filename, reads the file into a variable, and then
takes any inlined HTML and wraps it in Response.Write calls. It then
executes the resulting string which is now all ASP using the Execute call.
If the function returns False then the "include" file is empty or could not
be found. There's no error checking in the function, so you'll have to add
this yourself once you're happy it works.

To use the function, just do something like this:

<html>
<body>
<p>Blah blah</p>
<%
ExecInclude "myfile.asp"
%>
<p>more blah</p>
</body>
</html>

Here's the function:

Function ExecInclude(sFile)
dim mfo, mf, sTemp, arTemp, arTemp2, lTemp, sTemp2, lTemp2, sFile2

If InStr(1,sFile,":") = 0 Then
sFile = Server.MapPath(sFile)
End If

'first read the file into a variable use FSO
set mfo = Server.CreateObject("Scripting.FileSystemObject")

'does file exist?
If mfo.FileExists(sFile) Then
'read it
set mf = mfo.OpenTextFile(sFile, 1, false, -2)
sTemp = mf.ReadAll
mf.close
set mfo = nothing
Else
sTemp = ""
End If

If sTemp <"" Then
'sTemp contains the mixed ASP and HTML, so the next task is to
dynamically replace the inline HTML with response.write statements

arTemp = Split(sTemp,"<" & "%")
sTemp = ""

For lTemp = LBound(arTemp) to UBound(arTemp)

If InStr(1,arTemp(lTemp),"%" & ">") 0 Then
'inline asp
arTemp2 = Split(arTemp(lTemp),"%" & ">")

'everything up to the % is ASP code

sTemp2 = trim(arTemp2(0))

If Left(sTemp2,1) = "=" Then
'need to replace with response.write
sTemp2 = "Response.Write " & mid(sTemp2,2)
End If

sTemp = sTemp & sTemp2 & vbCrLf

'everything after the % is HTML
sTemp2 = arTemp2(1)

Else
'inline html only
sTemp2 = arTemp(lTemp)

End If

arTemp2 = Split(sTemp2,vbCrLf)
For lTemp2 = LBound(arTemp2) to UBound(arTemp2)
sTemp2 = Replace(arTemp2(lTemp2),"""","""""") 'replace
quotes with doubled quotes
sTemp2 = "Response.Write """ & sTemp2 & """" 'add
response.write and quoting

If lTemp2 < Ubound(arTemp2) Then
sTemp2 = sTemp2 & " & vbCrLf" 'add cr+lf if not the
last line inlined
End If

sTemp = sTemp & sTemp2 & vbCrLf 'add to running variable
Next

Next

Execute sTemp

ExecInclude = True

End If

end Function

Use this code at your own risk. It shouldn't cause any problems, but I can't
guarantee it. If anyone can suggest improvements to this I'd be happy to
hear them.

--
Dan
Oct 19 '07 #5

P: n/a
Daniel Crichton wrote:
>
Execute sTemp

Use this code at your own risk. It shouldn't cause any problems, but
I can't guarantee it. If anyone can suggest improvements to this I'd
be happy to hear them.
The basic problem with this is the use of Execute. For an explanation of why
I say this google Lippert and "Eval is Evil"

Many times, application development comes down to a choice between runtime
performance/efficientcy/resource-expenditure and developer convenience. I
typically choose the former.

I have faced this situation (needing to decide at runtime which include
files to load) but up to this point, I have been able to use encapsulation
techniques to avoid using Execute. However, at some point, a choice will
need to be made between the memory/cpu consumed by loading all the
encapsulated functionality
and the resources consumed by using Execute to avoid loading the entire
shebang. My intent in making this post is to make sure readers know that
this choice should be an intelligent one based on testing and measurements,
rather than just doing the one that makes writing the code a little easier.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Oct 19 '07 #6

P: n/a
Bob wrote on Fri, 19 Oct 2007 07:59:12 -0400:
My
intent in making this post is to make sure readers know that this
choice should be an intelligent one based on testing and measurements,
rather than just doing the one that makes writing the code a little
easier.
Oh, I agree. Sometimes it's just not possible to rewrite everything,
especially if the customer wants it finished yesterday. I always try to
avoid this sort of thing, and only use includes for common functions if
possible, but for one particular case this was the only solution I could
come up with in the specified timeframe and so far it's worked flawlessly,
and there's been no noticeable performance degradation (my use of it is on
an intranet system with a small number of users and a low transaction rate,
not a public web site).

--
Dan
Oct 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.