Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Populate a combo with an array

Reply
Thread Tools

Populate a combo with an array

 
 
Lukelrc
Guest
Posts: n/a
 
      05-19-2004
Hi,

I have an array - ListOfFiles - that i want use to populate an combo.

I've attempted to do it like this

<select name="txtAvailable" rows="4" id="txtAvailable">
<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?

Thanks

Luke
 
Reply With Quote
 
 
 
 
Ray at
Guest
Posts: n/a
 
      05-19-2004
When things appear on the page where you don't expect them to, this is often
because of some poorly written HTML, not code. Example:

<table border="1">
<tr>
<td>XXX</td>
YYY
</tr>
</table>

The YYY will appear at the top of the page (in IE, anyway), although one may
expect it to appear in the table.

Basically, if you're seeing your select with the options in your array, your
ASP code isn't the problem.

Ray at work

"Lukelrc" <> wrote in message
news: om...
> Hi,
>
> I have an array - ListOfFiles - that i want use to populate an combo.
>
> I've attempted to do it like this
>
> <select name="txtAvailable" rows="4" id="txtAvailable">
> <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?
>
> Thanks
>
> Luke



 
Reply With Quote
 
 
 
 
Dave Anderson
Guest
Posts: n/a
 
      05-19-2004
Lukelrc wrote:
>
> <select name="txtAvailable" rows="4" id="txtAvailable">
> <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...

FilenameOptionList = CreateOptions(ListOfFiles,"")

....then insert it inline:

<SELECT><%=FilenameOptionList%></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(byVal A(), byVal DefaultValue)
Dim i, val
For i = 0 To UBound(A)
val = Server.HTMLEncode(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(ListOfFiles,Request.Form("txtAvailab le").Item)


Lastly, it bears mentioning that in many cases, this abstraction allows you
to simply use <SELECT><%=CreateOptions(...)%></SELECT>

More complicated functions can be written to create OPTION lists from 2D
arrays, wherein the .value and .text properties differ (<OPTION
VALUE="TN">Tennessee</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<OPTION VALUE=\"" + this[i] +
(this[i]==val ? "\" SELECTED>" : "\">") +
this[i] + "</OPTION>"
return a.join("")
}

....in which case:

<%=ListOfFiles.toOptions()%>

would suffice, but:

<%=ListOfFiles.toOptions(Request.Form("txtAvailabl e").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.


 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      05-19-2004
Ray at <%=sLocation%> [MVP] wrote:
> When things appear on the page where you don't expect them to, this
> is often because of some poorly written HTML, not code. Example:
>
> <table border="1">
> <tr>
> <td>XXX</td>
> YYY
> </tr>
> </table>
>
> The YYY will appear at the top of the page (in IE, anyway), although
> one may expect it to appear in the table.
>
> Basically, if you're seeing your select with the options in your
> array, your ASP code isn't the problem.


That's not correct, Ray. This is clearly an execution order problem. Note
the RUNAT="server" bit. If the default language is VBScript, that block
executes last, no matter where it sits in your code.

The fix is either abstraction or <%%> blocks.



--
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.


 
Reply With Quote
 
Ray at
Guest
Posts: n/a
 
      05-19-2004

"Dave Anderson" <> wrote in message
news:...
> > Basically, if you're seeing your select with the options in your
> > array, your ASP code isn't the problem.

>
> That's not correct, Ray. This is clearly an execution order problem. Note
> the RUNAT="server" bit.


Oh yeah. Thanks Dave. Studying FAQ 2045 again now. :]

Ray at work


 
Reply With Quote
 
Luke Curtis
Guest
Posts: n/a
 
      05-20-2004
Thanks for all your help.

I have created a string as suggested FileNameoptionlist, which is fine.

Ive checked out about the execution order, but i cant see what exactly
the problem is. What is strange is that if i script:

<script language="vbscript"
Runat="server">Response.write(filenameOptionList)</script>

in either the head or the body, it produces the correct HTML but still
apended at the end - which does suggest an execution order problem, but
if i simply write:

<% response.write(Filenameoptionlist)%>

or

<% =filenameoptionlist%>

I get nothing at all!

Does anyone know why this might be?
Thanks in advance.

Luke



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

 
Reply With Quote
 
Lukelrc
Guest
Posts: n/a
 
      05-20-2004
Thanks for all your help.

I have created a string as suggested FileNameoptionlist, which is fine.

Ive checked out about the execution order, but i cant see what exactly
the problem is. What is strange is that if i script:

<script language="vbscript"
Runat="server">Response.write(filenameOptionList)</script>

in either the head or the body, it produces the correct HTML but still
apended at the end - which does suggest an execution order problem, but
if i simply write:

<% response.write(Filenameoptionlist)%>

or

<% =filenameoptionlist%>

I get nothing at all!

Does anyone know why this might be?
Thanks in advance.

Luke
 
Reply With Quote
 
Ray at
Guest
Posts: n/a
 
      05-20-2004
What about:

<% Response.WRite "Here is the filenameoptionslist: " & Filenameoptionlist
%>

Ray at work

"Lukelrc" <> wrote in message
news: om...
> Thanks for all your help.
>
> I have created a string as suggested FileNameoptionlist, which is fine.
>
> Ive checked out about the execution order, but i cant see what exactly
> the problem is. What is strange is that if i script:
>
> <script language="vbscript"
> Runat="server">Response.write(filenameOptionList)</script>
>
> in either the head or the body, it produces the correct HTML but still
> apended at the end - which does suggest an execution order problem, but
> if i simply write:
>
> <% response.write(Filenameoptionlist)%>
>
> or
>
> <% =filenameoptionlist%>
>
> I get nothing at all!
>
> Does anyone know why this might be?
> Thanks in advance.
>
> Luke



 
Reply With Quote
 
Roland Hall
Guest
Posts: n/a
 
      05-20-2004
"Lukelrc" wrote in message
news: om...
: I have an array - ListOfFiles - that i want use to populate an combo.
: I've attempted to do it like this
:
: <select name="txtAvailable" rows="4" id="txtAvailable">
: <Script Language = "vbscript" Runat = "Server">
: For i = 0 to Count -1
: Response.Write"<OPTION>" & ListOfFiles(i) & "</OPTION>"
: next
: </script></select>
:

Untested, just written here...

<%
Dim Count
Count = ubound(ListOfFiles())

sub prt(str)
Response.Write(str & vbCrLf)
end sub

sub lprt(str)
Response.Write(str & "<br />" & vbCrLf)
end sub

sub popList()
prt("<select name=""txtAvailable"" rows=""4"" id=""txtAvailable"">")
for i = 0 to Count - 1
prt("<option value=""" & ListOfFiles(i) & """>" & ListOfFiles(i) &
"</option>")
next
prt("</script>")
end sub

lprt("Check out my list of files...")
popList
%>

HTH...

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp


 
Reply With Quote
 
Lukelrc
Guest
Posts: n/a
 
      05-21-2004
I've sorted it. When i had written the script that populated the
string i had open and closed script tags several times. Putting all of
the code into one set of tags sorted it. Thanks for your help.



Luke
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Review: A.C.Ryan Backy™ Combo & SATA Combo Silverstrand Reviews & How-To's 0 06-20-2005 03:37 AM
For XML Schema gurus - populate 2 combo boxes from schema data Joyce XML 0 03-01-2005 10:31 AM
Populate a popup window with clickable records from an Access DB and upon clicking, populate a selectbox on the original webpage with the clicked record Enjoy Life ASP General 2 02-23-2005 10:48 PM
Code to populate combo Mark Goldin ASP .Net 2 08-03-2004 12:34 PM
Populate List Box base on Combo Box Selection Adrian ASP General 1 02-18-2004 09:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57