473,388 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

variable asp include

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
6 4169
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: mark | last post by:
IF I have something like this: <title><?php print($pagetitle); ?></title> <body> <?php include("folders/my_include.php"); ?> </body> </html> and my_include.php contains the value to the...
2
by: John Aspinall | last post by:
Hi, Ive got a navigation bar for my web pages in an include (top_nav.asp). On my homepage (index.asp) and all the other site pages I have a reference to this include which includes my...
2
by: mark | last post by:
How does one call a variable defined in a class defined in file1.cpp from another file file2.cpp. Or can one call a class from a different file ? (Besides the standard way to define the variable...
3
by: Mike | last post by:
I'm new to PHP - moving over from ASP. I have a number of include files, the first of which sets the value of a variable $loginmsg. I use that variable in a subsequent include file, but get a...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
13
by: GGawaran | last post by:
First off, Hi everyone new to .asp and am trying to self teach myself. Im trying to figure out what exactly im doing wrong, or maybe what I think I can do, I really cant. The Idea....
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
3
by: leonardodiserpierodavinci | last post by:
Hi. Sorry for what is perhaps a neophyte question: is it possible to pass a variable to a PHP script from inside another PHP piece of code? For instance, the file test.php (which of course...
7
by: atwiawomo | last post by:
Hi This may be simple to solve, but I can't seem to do it :( I am trying to generate an include name on an image gallery page from a variable name. By this I mean I already have a numeric...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.