Dynamic creation of ASP code

A

Arne de Booij

Hi all,

I am trying to generate ASP code on the fly as I want to be able to
control
the code from the database and am running a problem.

When I try this (simplified example of what I have but it conveys the
problem):

<%
replacetext = "response.write ""blah"""
response.write "<%" & replacetext & "%\>"
%>

I get an empty page with <%response.write "blah"%> in the source instead
of
what I want: a page that has 'blah' on it.

Is there any trick behind this?

thanks
Arne
 
D

Dave Anderson

Arne de Booij said:
replacetext = "response.write ""blah"""
response.write "<%" & replacetext & "%\>"

I get an empty page with <%response.write "blah"%> in the source
instead of what I want: a page that has 'blah' on it.

Is there any trick behind this?

1. What's wrong with just doing this?

Response.Write("""blah""")

-or-

replacetext = """blah"""
Response.Write(replacetext)

-or-

replacetext = """blah"""
...
<%=replacetext%>


2. I was unaware you could use \ to escape characters in VBScript.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
D

dave

Perhaps you need to explain more what you require.
The asp parser just isnt going to run twice...
 
P

Patrice

You just write some text (that happens to be "ASP code") to the browser
exacly as if it were HTML code. You have actually to execute the code (using
perhaps "Execute" if I remember) rather than just writing the code to the
browser.

That said, I would still double check this design. Do you mean you want to
store the ASP code inside the DB ? What is the overall goal (expected
benefit) ?

Patrice
 
G

Gottfried Mayer

Arne de Booij said:
Hi all,

I am trying to generate ASP code on the fly as I want to be able to
control
the code from the database and am running a problem.

When I try this (simplified example of what I have but it conveys the
problem):

<%
replacetext = "response.write ""blah"""
response.write "<%" & replacetext & "%\>"
%>

I get an empty page with <%response.write "blah"%> in the source instead
of
what I want: a page that has 'blah' on it.

Is there any trick behind this?

thanks
Arne

This example should work for your purpose:

<%
replacetext = "response.Write ""blah"""
execute replacetext
%>

The Response.Write - Command only dumps the text you give him to the client.
The Execute - Command actually executes the lines in the string and in this
way only returns "blah" to the client.

You can also chain several commands with the ":" - Character

for Example:

<%
StrTemp = "strEcho = ""Just Testing<br>"" & vbcrlf :" & _
" Response.Write ""Hello World.<br>"" & vbcrlf :" & _
" For iTmp = 1 to 4 : Response.Write strEcho : Next"

execute strTemp
%>

(I have seen this on one of these .vbs worms that travelled around the globe
some months ago and tried it in asp...)

HTH
Gottfried
 
P

PW

Dave Anderson said:
2. I was unaware you could use \ to escape characters in VBScript.


How do you do it ?
I was trying to send a chr(7) BEL character to a receipt printer, but
couldn't figure it out.
 
A

Arne de Booij

In reply to "1. What's wrong with just doing this?"

I might have oversimplyfied the example.

What I am trying to do is:
1. I have a flat text string (strSentence)
2. I want to replace certain parts of the string

I wrote some ASP code that automatically generated the ASP code I
wanted. For example, if I wanted to replace every 'Yes' and 'No' in
'strSentence' then it would generate

replace(replace(strSentence, "Yes", "<b>Yes</b>"), "No", "<i>No</i>))

I then want to display the result of this code ('<b>Yes</b>'), not the
code itself. And strSentence is dynamic and I want to be able to add
'replacements' through the DB, just writing down the code is not an
option.

Maybe I am approaching the problem wrongly and is there a much easier
way of doing this.
 
A

Aaron Bertrand - MVP

How do you do it ?
I was trying to send a chr(7) BEL character to a receipt printer, but
couldn't figure it out.

What did you try?

Response.Write "Str" & Chr(7) & "Str"
 
A

Aaron [SQL Server MVP]

I wrote some ASP code that automatically generated the ASP code

Why? If you're storing this in a database, you can't easily make ASP
suddenly parse and execute twice... this is not how ASP works.

You can use the execute command in VBScript, but as Michael Kersey will
surely echo, there are serious security implications involved.
 
P

Patrice

Youl'l have just to store the match and replacement string in the db. Your
code will act on these data as usual when it comes to using a DB.

It could give something like :

Set rst=GetRecordset("SELECT Match,Replacement FROM StringTable")
Do While Not rst.Eof

strSentence=Replace(strSentence,rst("Match").Value,rst("Replacement").Value)
rst.MoveNext
Loop
rst.Close
Response.Write strSentence.

You could wrap all this in a function so that you act or whatever sentence
you passed. Of course you could cache the strings in a an array or
something.

You could also create a regular expression that does this job (you'll likely
have some problems with "snow" giving "s<i>No</i>w", a regular exp could
avoid this).


Patrice
 
A

Arne de Booij

thank you all for your replies! I am going with Patrice's suggestion.

I didn't know about this (see below) before but now I do. Thanks Aaron!

If you're storing this in a database, you can't easily make ASP suddenly
parse and execute twice... this is not how ASP works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top