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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top