Lukelrc wrote:
<select name="txtAvaila ble" rows="4" id="txtAvailabl e">
<Script Language = "vbscript" Runat = "Server">
For i = 0 to Count -1
Response.Write" <OPTION>" & ListOfFiles(i) & "</OPTION>"
next
</script></select>
I placed the vbscript in the approprate place in the body. But when i
open the page instead of writing the HTML where i placed the code, it
has appended it right at the bottom of the page. Does anyone know
where i'm going wrong or if there's a better way of doing this?
This is an order-of-execution issue (
http://aspfaq.com/show.asp?id=2045). If
you want to interlace server-side scripting with HTML, you will need <% %>
blocks.
I usually abstract as much processing as possible before writing any HTML.
In your case, I would build a single string to represent the entire array...
FilenameOptionL ist = CreateOptions(L istOfFiles,"")
....then insert it inline:
<SELECT><%=File nameOptionList% ></SELECT>
You can define the function anywhere in the script, either in <% %> blocks
or in <SCRIPT RUNAT="Server"> blocks. Here is a sample function:
=============== =============== =============== =============== ============
Function CreateOptions(b yVal A(), byVal DefaultValue)
Dim i, val
For i = 0 To UBound(A)
val = Server.HTMLEnco de(A(i))
If A(i) = DefaultValue Then
A(i) = "<OPTION VALUE=""" & val & """ SELECTED>" & _
val & "</OPTION>"
Else
A(i) = "<OPTION VALUE=""" & val & """>" & _
val & "</OPTION>"
End If
Next
CreateOptions = Join(A,vbCrLf)
End Function
------------------------------------------------------------------------
Note that this example is capable of preselecting the control:
... = CreateOptions(L istOfFiles,Requ est.Form("txtAv ailable").Item)
Lastly, it bears mentioning that in many cases, this abstraction allows you
to simply use <SELECT><%=Crea teOptions(...)% ></SELECT>
More complicated functions can be written to create OPTION lists from 2D
arrays, wherein the .value and .text properties differ (<OPTION
VALUE="TN">Tenn essee</OPTION>). Either way, this type of function is
entirely self-contained, so it can be placed into a common include for reuse
wherever needed.
If you use JScript on the server side, you can even do something like
this...
Array.prototype .toOptions = function(val) {
for (var i=0,a=[]; i<this.length; i++)
a[i] = "\r\n<OPTIO N VALUE=\"" + this[i] +
(this[i]==val ? "\" SELECTED>" : "\">") +
this[i] + "</OPTION>"
return a.join("")
}
....in which case:
<%=ListOfFiles. toOptions()%>
would suffice, but:
<%=ListOfFiles. toOptions(Reque st.Form("txtAva ilable").Item)% >
works even better, and doesn't require a different method signature.
--
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.