Response.write multi line html for ASP? - asp-classic

I have an asp page that displays an online form after validating a user. I'm using Response.Write "form goes here" However, the form is very long (100+ lines).
Is there a way I can do Response.Write with multi-line html? I want to do something like this:
<%
If rs.rcount > 0 then
response.write "
<form>
<input type="text" id="inputEmail">
</form>"
End if
%>
Many thanks,

Use a code block, not response.write.
<%
...your VB ....
if a=b then
%>
<h1> HTML GOES HERE</h1>
<form>
<input type="text" id="inputEmail">
</form>
<%
end if
... more VB code
%>

You've got a couple of options, which one you pick depends on a few things...
(1) You can do as Diodeus suggested, and use a code block:
If rs.count > 0 Then
%>
<form>
<input type="text" id="etc" />
</form>
<%
End If
(2) You can do as Yuriy Galanter suggested, and build your form through string concatenation:
Dim sHTML;
sHTML = "<form>"
sHTML = sHTML & "<input type="text" id="etc" />"
'... etc.'
sHTML = "</form>"
If rs.rcount > 0 Then
Response.Write sHTML
End If
(3) You can do as you were initially thinking, writing out a line or three at a time:
If rs.rcount > 0 Then
Response.Write "<form>"
Response.Write "<input type="text" id="etc" />" & _
"<input type="text" id="other" />"
'The underscore above indicates that the string/command/etc. continues on the next line, whitespace is ignored.'
Response.Write "</form>"
End If
(4) You can mix and match any combination of the above
The benefit of option 1 is it's fairly fast, easy to edit, and simple to implement if you've already got the HTML ready to go.
The benefit of option 2 is you don't have to worry about context switching (not so much an issue in ASP 3, but it's something you'll see mentioned if you read and research enough), and it's (in my opinion) easier to insert variables if there are portions of your form that may change based on other business logic (or if you think that will be a need in the near future)
The benefit of option 3 is it's (again, my opinion) easier to conditionally show/hide/alter parts of the form depending on business logic (doesn't sound like an issue for you per se, but it's worth keeping in mind.)
Depending on your situation, you may find the best approach is some mix of the above. Just keep in mind that the more string concatenation you do, the worse your memory management will get.
There are also some libraries out there (like ASP-Ajaxed - full disclosure, I recently took over the project. Still working on rebuilding an official website) with templates and better managed string concatenation. Adding something like this after the fact isn't always easy, and may be overkill if you're just modifying an existing project (vs. creating a new project).

You can build your form in steps in a variable, e.g.
Dim sHTML;
sHTML = "<form>"
sHTML = sHTML & "<p><input type=""text"" id=""inputEmail""></p>"
'... etc.
Response.Write sHTML
Use HTML tags such as <br/> or <p></p> to place input elements at new lines.
If you're using ASP.NET/VB.NET you can use StringBuilder, which is much more efficient to construct dynamic strings

Related

how to get current line number in Classic ASP code

I'm trying to find a way how to get the code line number in Classic ASP
<%
Response.Write "Hello world!<br>"
Response.Write getThisLineNumber() & "<br>"
Response.Write "Goodbye world."
%>
Expected output
Hello world.
3
Goodbye world.
getThisLineNumber() is a fictional function doing what I'm looking for.
I temporarily solved my problem by
<%
Response.Write "Hello world!<br>"
Response.Write "3<br>"
Response.Write "Goodbye world."
%>
but when i add a new line after "Hello world." (2nd line), then I manually must change
Response.Write "3<br>"
to
Response.Write "4<br>"
Classic ASP does not support such a feature, it's something like Reflection in .NET.
The closer thing you can do is get line number when an error happens in Err object, this way: Err.Line.
If you want to profile an asp page, maybe ASP Profiler can help you: ASP Profiler

In asp, how to set boolean values to english?

When I run this:
<%
www = false
response.write www
response.write "UPDATE table SET domain='"&www&"' WHERE id=n"
%>
I get this:
false
UPDATE table SET domain='Falso' WHERE id=n
Notice the 'Falso' word that is in Portuguese instead of English.
Is it possible to change the boolean values to English?
I have a Windows 2008 / IIS 7 in pt-BR.
This question was asked just recently here
I don't have a good machine to test the following code, which came from a working solution. It forces the locality you need (of course you can change the locality to what you need it to be, if 1033 is not your goal)
<%
Response.Write FormatDateTime(Now) & "<br />"
oldLocale = SetLocale(1033) '1033=English(US)
Response.Write FormatDateTime(Now) & "<br />"
SetLocale oldLocale
Response.Write FormatDateTime(Now) & "<br />"
%>
I'll not be able to provide any more testing for this 'issue' anymore.
That's because I've made a decision to format my windows and install a new one now in english.. I did this because I'm running out of time, I need to put this server working as soon as possible..
But I want to thank everyone who contributed on finding the resolution for this problem =)
If anyone is having the same issue, please leave your comment, so the others can provide more options to try to solve this.
If you dont mind changing the code, you could use a function to print the boolean value. For me thats not an option, but i'm starting to believe its the only way.
Function PB(pVal)
If pVal Then
PB = "true"
Else
PB = "false"
End If
End Function
Response.Write "UPDATE table SET domain='"&PD(www)&"' WHERE id=n"
Just use string instead:
www = "false"
Then it won't be affected by regional settings.
If you want to keep working with actual boolean values, use Parameters instead of raw SQL.

Write a file containing ASP delimiters

We have lots of files containing image / static file references to resources on our site. As we are moving these files to S3 hosting, I am writing a script that reads each of our ASP files, replaces each reference so that each one points at a globally defined variable, and rewrites those changes to the file, so that we don't have to update all these files manually!
This works alright with references inside actual code.
eg.
Response.Write "<img src=""http://www.site.com/images/image.gif"">"
becomes
Response.Write "<img src=""" & s3BucketName & "/images/image.gif"">"
But when it comes to replacing code like this :
<script src="/javascript/script.js">
It's more difficult - as we have to place the ASP delimiters <%= %>
around the global var around it.
eg.
<script src="<%=s3BucketName%>/javascript/script.js">
Because the script delimiters actually stop the page where they are, and generate an error, that's what I'm finding difficult. URLencoding the <%=s3BucketName%> string, and writing, just writes the encoded text (unusable by ASP), doesn't transform into what I want.
Any ideas gratefully received.
Thanks, Adam
Ok, I've come up with a solution, seems to work. If you find a better one, please comment.
textToReplace = "src="""& chr(60) & chr(37) & chr(61) & "s3BucketName" & chr(37) & chr(62) & "/javascript/script.js"
Basically each symbol of <%= and %> is converted into chr() values.
You could use the basetag in the head section of your pages then you can leave the relative urls like your script example alone, you can combine this technique with the one peedeeaay suggests.
<base href="http://www.s3bucketdomain.com/directory/" />

databind a DropDownList control with a list of all sub directories that exist in a particular directory on the server

I am wanting to databind a DropDownList control with a list of all sub directories that exist in a particular directory on the server. The directory I want to search is in the root of the application. I am fairly new to programming and I'm not sure where to even start.
I found this code on a website:
Dim root As String = "C;\"
Dim folders() As String = Directory.GetDirectories(root)
Dim sb As New StringBuilder(2048)
Dim f As String
For Each f In folders
Dim foldername As String = Path.GetFileName(f)
sb.Append("<option>")
sb.Append(foldername)
sb.Append("</option>")
Next
Label3.Text = "<select runat=""sever"" id=""folderlist""" & sb.ToString() & "</select>"
I guess this is vb. but my tool is in asp, so is their something similar in vbscript so that I can use it.
Here is a very quick and dirty example of what you want.
There are a lot of improvements I would make before I'd consider it production ready code.
It should however server to show you some of the basic concepts you are after.
<%# Language=VBScript ENABLESESSIONSTATE = False%>
<select id="selFiles" name="selFiles" class="Select" style="width: 250px" tabindex="130">
<%
Dim fso, folder, files
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\")
Set files = folder.Files
For each folderIdx In files
Response.Write("<option>" + folderIdx.Name + "</option>")
Next
%>
</select>
One place to start looking at improvements here would be introducing your own components that will do all the complex stuff like listing files this gives you more control, allows greater modularity in you design and (probably most importantly) gives you better control of security.
The information below may be slightly off (it is from memory of an old project) but should be reasonably close and give you a start on introducing code components into your ASP classic code.
With ASP classic you create objects using code like:
<object runat="server" progid="YourObject.Class" id="oObject" VIEWASTEXT></object>
Where YourObject.Class is the programatic id of a component installed in the registry.

Can you declare a variable in ASP classic like this?

I am trying to see if this can be done in classic ASP:
Dim myVar
myVar = <<< END
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>test</title>
</head>
<BODY>
END
I can do this in PHP, but I am not positivte if it can be done in ASP.
Problem is I need to handle the HTML output via a variable, and I don't want to go through the HTML and convert " to ""
EDIT:
I've found that this is called HEREDOC syntax in PHP.
Since its been asked, what I am trying to do is store HTML type tags (which may contain ', " characters which would otherwise break
myVar = "<stuff color="red">here</stuff>"
so I would need to fix it by replacing color="red" with color=""red""
PART OF THE PROBLEM:
I don't want to have to replace " with "" for the content as I assign it, I guess a HEREDOC syntax is not available for ASP classic.
OK FINE... :P
Since everyone is asking me WHY I am going about it this way, here is why, I have to support this old ASP code, I don't want to, but suddenly the scope of this old app changes that they want the contents (which used to be an HTML page) to be emailed, SO... I wanted to HEREDOC the HTML output, pass it to the mail function and have it email. Having said that, I know its sloppy, and I know it works better the other way, however this is what the job called for, I didn't want to re-write it, I just wanted to augment the output from HTML to HTML-EMAIL...
Hope that makes more sense ;)
The easiest thing to do would be to keep the HTML in a separate file, and then open that file using a TextStream object, reading the string into a variable.
No. One language's syntax isn't going to work in a different language. You can, however, assign a string literal to the variable:
Dim myVar
myVar = _
"<html xmlns=""http://www.w3.org/1999/xhtml"" lang=""en"" xml:lang=""en"">" & vbCrLf & _
"<head>" & vbCrLf & _
" <title>test</title>" & vbCrLf & _
"</head>" & vbCrLf & _
"" & vbCrLf & _
"<BODY>" & vbCrLf
I'm pretty sure you can't do exactly what you're asking here.
It might help, however, if you explained what you're trying to do. There may be some other way.
update:
You could consider putting your HTML fragments into seperate files. This would allow you to define them without having to do any reformatting of the HTML. In a similar manner you could put them into a database or even into resource files.
I'm still not 100% clear why you want these HTML fragments defined as variables, but these approaches would work.
there is no herodoc for asp :) gogo escape - its the only useable way

Resources