View Full Version : Using "Response.Write" to produce asp code
Gary Williams
10-19-2005, 03:15 PM
Hi All,
The following is one line of hardcoded asp script that works perfectly:
<% if session("CustomerReference") = "1234" then %>, etc
I need to be able to produce this line of code using Response.Write as follows, so I can draw the CustomerReference from a database table:
Response.Write "if session( ' " ' CustomerReference ' " ' ) = " & ors1(' " 'CustomerReference' " ') & " then <BR>"
Unfortunately, I cannot get the combination of single and double quotes correct, so the " If " statement is not produced correctly so does not work.
Help!
Regards
Gary
NancyJ
10-19-2005, 03:41 PM
...if I'm understanding what you're tring to do correctly then this should do it
Response.Write "if session(""CustomerReference"") = """ & ors1("CustomerReference") & """ then <BR>"
that should write out: if session("CustomerReference") = "1234"
this is what you wanted right?
Or are you trying to run that code using the rs value rather than a flat number...
<% if session("CustomerReference") = cstr(ors1("CustomerReference")) then %>
Gary Williams
10-19-2005, 05:07 PM
Hi Nancy,
Your code did exactly what I wanted and produced the exact line of code I needed, but my cunning plan still didn't work. Here is the problem. So my customers can display a personal message after a sale, I have an include file that contains, for example, these three entries. Hardcoded into my page, this works fine.
-----------------------------
<%
if session("CustomerReference") = "1234" then
response.write "<TR><TD align='left' bgcolor='#fafafa' VALIGN='top' colspan = 4>"
response.write "<P>My opening hours are blah, blah, ......</P>"
response.write "</TD></TR>"
end if
%>
<%
if session("CustomerReference") = "5689" then
response.write "<TR><TD align='left' bgcolor='#fafafa' VALIGN='top' colspan = 4>"
response.write "<P>My phone number is, blah, blah, ......</P>"
response.write "</TD></TR>"
end if
%>
<%
if session("CustomerReference") = "7415" then
response.write "<TR><TD align='left' bgcolor='#fafafa' VALIGN='top' colspan = 4>"
response.write "<P>My email address is blah, blah, ......</P>"
response.write "</TD></TR>"
end if
%>
--------------------------
To make life easier as more customers come on board, I now store the customers personal message messages in an access database table. I want to reduce the include file to just the following code so that the original code above, is created each time the page is run. As I enter more customers in the database, so this include file is automatically written with all customer messages, each subjected to an "if...then...end if" test .
=================
<%
Set ors1=server.createobject("ADODB.RECORDSET")
strsql = "Select * from customers"
ors1.open strsql, ocn
While not ors1.eof
Response.Write "if session(""CustomerReference"") = """ & ors1("CustomerReference") & """ then "
Response.write "<TR><TD align='left' bgcolor='#fafafa' VALIGN='top' colspan = 4>"
Response.Write "<P>" & ors1("CustomerText") & "</P>"
Response.write "</TD></TR>"
Response.Write "end if"
ors1.MoveNext
Wend
ors1.close
%>
================
This code correctly produces a copy of my original hard coded file, but the "if...then...end if" test does not work. It just produces a list of all customers messages, one after the other.
Is this what you were refering to in "Or are you trying to run that code using the rs value rather than a flat number...?"
Regards
Gary
NancyJ
10-19-2005, 05:28 PM
You want to run the code - not write it to the screen then?
While not ors1.eof
if session("CustomerReference") = cstr(ors1("CustomerReference")) then
Response.write "<TR><TD align='left' bgcolor='#fafafa' VALIGN='top' colspan = 4>"
Response.Write "<P>" & ors1("CustomerText") & "</P>"
Response.write "</TD></TR>"
end if
ors1.MoveNext
Wend
ors1.close
Gary Williams
10-19-2005, 06:07 PM
Hi Nancy,
Perfect !!!! You're a genius !
That does exactly what I wanted. The script generates the page and runs the tests, correctly pasting the customers message on the page after his product.
I see now that I should have left the test outside the response.write loop.
Many thanks
Gary
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.